From 99b69cc14d585b3b5045037bddce50903feaac80 Mon Sep 17 00:00:00 2001 From: Able Date: Sun, 4 Dec 2022 06:19:46 -0600 Subject: [PATCH] framebuffer addition --- .../able_graphics_library/src/commands.rs | 42 ++++++++++++--- .../able_graphics_library/src/framebuffer.rs | 52 +++++++++++++++++++ libraries/able_graphics_library/src/lib.rs | 1 + libraries/able_graphics_library/src/types.rs | 1 + 4 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 libraries/able_graphics_library/src/framebuffer.rs diff --git a/libraries/able_graphics_library/src/commands.rs b/libraries/able_graphics_library/src/commands.rs index 607e738..d90b466 100644 --- a/libraries/able_graphics_library/src/commands.rs +++ b/libraries/able_graphics_library/src/commands.rs @@ -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 { - UpdateFrame, + /// Copy one framebuffer onto another + UpdateFrame(UpdateFrameData), Clear(Color3), ClearBuffer(BufferID), - AppendVertex(BufferID, Vertex), - - MakeBuffer { - num_vertices: usize, - usage: BufferUsage, - }, +} + +/// Offset framebuffer two by offset and place it onto framebuffer one +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 + } } diff --git a/libraries/able_graphics_library/src/framebuffer.rs b/libraries/able_graphics_library/src/framebuffer.rs new file mode 100644 index 0000000..241ecd6 --- /dev/null +++ b/libraries/able_graphics_library/src/framebuffer.rs @@ -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, +} + +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 + } +} diff --git a/libraries/able_graphics_library/src/lib.rs b/libraries/able_graphics_library/src/lib.rs index c999ccc..8ddbcf4 100644 --- a/libraries/able_graphics_library/src/lib.rs +++ b/libraries/able_graphics_library/src/lib.rs @@ -1,6 +1,7 @@ pub mod buffer; pub mod color; pub mod commands; +pub mod framebuffer; pub mod types; pub mod vertex; diff --git a/libraries/able_graphics_library/src/types.rs b/libraries/able_graphics_library/src/types.rs index 926fbf8..530c1b2 100644 --- a/libraries/able_graphics_library/src/types.rs +++ b/libraries/able_graphics_library/src/types.rs @@ -9,3 +9,4 @@ pub struct Float2Array { } pub type Position3 = Float3Array; +pub type Position2 = Float2Array;