framebuffer addition

This commit is contained in:
Able 2022-12-04 06:19:46 -06:00
parent 17323dd5a3
commit 99b69cc14d
4 changed files with 88 additions and 8 deletions

View file

@ -1,14 +1,40 @@
use crate::{buffer::BufferUsage, color::Color3, types::BufferID, vertex::Vertex}; use crate::{
color::Color3,
framebuffer::FrameBuffer,
types::{BufferID, Position2},
vertex::Vertex,
};
pub enum DrawCommand { pub enum DrawCommand {
UpdateFrame, /// Copy one framebuffer onto another
UpdateFrame(UpdateFrameData),
Clear(Color3), Clear(Color3),
ClearBuffer(BufferID), ClearBuffer(BufferID),
AppendVertex(BufferID, Vertex), AppendVertex(BufferID, Vertex),
}
MakeBuffer {
num_vertices: usize, /// Offset framebuffer two by offset and place it onto framebuffer one
usage: BufferUsage, pub struct UpdateFrameData {
}, pub offset: Position2,
pub framebuffer_1: FrameBuffer,
pub framebuffer_2: FrameBuffer,
}
impl UpdateFrameData {
/// Run a few checks to make sure this is a sane action
pub fn sanity_check(&self) -> bool {
let size_check;
let mut x_offscreen = false;
let mut y_offscreen = false;
size_check = self.framebuffer_1.check_size(&self.framebuffer_2);
if self.offset.inner[0] > self.framebuffer_1.width as f32 {
x_offscreen = true
}
if self.offset.inner[1] > self.framebuffer_1.height as f32 {
y_offscreen = true
}
size_check && x_offscreen && y_offscreen
}
} }

View file

@ -0,0 +1,52 @@
use core::ptr;
use crate::color::Color3;
/// NOTE: Assumes the layout of RGBA
pub struct FrameBuffer {
pub width: u32,
pub height: u32,
pub data: Vec<u32>,
}
impl FrameBuffer {
pub fn new(width: u32, height: u32) -> Self {
let data = vec![0; (width * height) as usize];
FrameBuffer {
width,
height,
data,
}
}
/// WARNING: Slow
pub fn set_pixel(&mut self, x: u32, y: u32, color: u32) {
let index = (y * self.width + x) as usize;
self.data[index] = color;
}
/// WARNING: Slow
pub fn get_pixel(&self, x: u32, y: u32) -> u32 {
let index = (y * self.width + x) as usize;
self.data[index]
}
/// Quickly writes the provided color to the whole framebuffer
pub fn clear(&mut self, color: Color3) {
unsafe {
// TODO: properly clear instead of only copying red
ptr::write_bytes(self.data.as_mut_ptr(), color.r, self.data.len());
}
}
// TODO(Able): Test preformance of clear2
#[allow(dead_code)]
fn clear2(&mut self, color: u32) {
self.data.fill(color);
}
/// Check the size of one framebuffer vs the other.
/// NOTE: Just because this returns false does not mean that something is wrong
/// there are cases that this makes sense
pub fn check_size(&self, other: &FrameBuffer) -> bool {
self.width == other.width && self.height == other.height
}
}

View file

@ -1,6 +1,7 @@
pub mod buffer; pub mod buffer;
pub mod color; pub mod color;
pub mod commands; pub mod commands;
pub mod framebuffer;
pub mod types; pub mod types;
pub mod vertex; pub mod vertex;

View file

@ -9,3 +9,4 @@ pub struct Float2Array {
} }
pub type Position3 = Float3Array; pub type Position3 = Float3Array;
pub type Position2 = Float2Array;