From 9853c48fb2e17fdb261e92a353ab6fe4fbf1f797 Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Thu, 11 Jun 2020 17:23:52 -0500 Subject: [PATCH] updated pixel buffer methods and added a prelude module --- src/lib.rs | 44 ++++++++++++++++++++++++++++++-------------- src/prelude.rs | 3 +++ 2 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 src/prelude.rs diff --git a/src/lib.rs b/src/lib.rs index 9babfbf..11f7d49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,8 @@ +#![allow(dead_code)] + // modules pub mod color; +pub mod prelude; // namespacing use color::Color; @@ -18,7 +21,7 @@ pub type Result = std::result::Result; // pixel buffer for internal use struct PixelBuffer { - buffer: Vec, + data: Vec, width: usize, height: usize, } @@ -26,19 +29,31 @@ struct PixelBuffer { impl PixelBuffer { // create a new pixel buffer fn new(width: usize, height: usize) -> PixelBuffer { - PixelBuffer { width, height, buffer: vec![0; width * height] } + PixelBuffer { width, height, data: vec![0; height * width] } } -} -impl std::convert::AsRef> for PixelBuffer { - fn as_ref(&self) -> &Vec { - &self.buffer + // index pixel buffer !! will panic if given x/y are out of bounds + fn at(&self, x: usize, y: usize) -> &u32 { + assert!(x < self.width); + assert!(y < self.height); + &self.data[y * self.width + x] + } + + // index pixel buffer mut !! will panic if given x/y are out of bounds + fn at_mut(&mut self, x: usize, y: usize) -> &mut u32 { + assert!(x < self.width); + assert!(y < self.height); + &mut self.data[y * self.width + x] + } + + // returns the buffer as a slice + fn as_ref(&self) -> &[u32] { + &self.data } -} -impl std::convert::AsMut> for PixelBuffer { - fn as_mut(&mut self) -> &mut Vec { - &mut self.buffer + // returns a mutable reference to a slice + fn as_mut_ref(&mut self) -> &mut [u32] { + &mut self.data } } @@ -61,13 +76,14 @@ impl Context { /// render the internal buffer to the screen pub fn present(&mut self) -> Result<()> { - self.window.update_with_buffer(self.pixel_buffer.as_ref(), self.width, self.height)?; + self.window.update_with_buffer(&self.pixel_buffer.as_ref(), self.width, self.height)?; Ok(()) } /// clears the pixel buffer - pub fn clear(&mut self) { - self.pixel_buffer.as_mut().iter_mut().for_each(|pixel| *pixel = color::BLACK.as_u32()); + pub fn clear(&mut self, color: Option) { + let color = color.unwrap_or(color::BLACK); + self.pixel_buffer.as_mut_ref().iter_mut().for_each(|pixel| *pixel = color.as_u32()); } /// checks if window is open @@ -77,7 +93,7 @@ impl Context { /// set a pixel pub fn set_pixel(&mut self, x: usize, y: usize, color: Color) { - self.pixel_buffer[(x, y)] = color.as_u32(); + *self.pixel_buffer.at_mut(x, y) = color.as_u32(); } /// get mouse position diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 0000000..1d84fe1 --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1,3 @@ +pub use crate::color::Color; +pub use crate::Context; +pub use crate::Result;