pub use colour::Colour; pub type Point = geometry::Point; pub type Dimensions = geometry::Dimensions; pub type Rect = geometry::Rect; pub use geometry::HasDimensions; pub struct Buffer { pixels: Vec, dimensions: Dimensions, } impl Buffer { pub const ZERO: Buffer = Buffer { pixels: Vec::new(), dimensions: Dimensions::ZERO }; pub fn new(dimensions: Dimensions) -> Self { let pixels = vec![Colour::BLACK; dimensions.area_usize()]; Self { pixels, dimensions } } pub fn new_with_fill(dimensions: Dimensions, colour: Colour) -> Self { let pixels = vec![colour; dimensions.area_usize()]; Self { pixels, dimensions } } pub fn fill(&mut self, colour: Colour) { self.pixels.iter_mut().for_each(|c| *c = colour); } pub fn copy_into(&mut self, coords: Point, other: &Buffer) { let self_rect = Rect::from(self.dimensions); let other_rect = Rect::construct(coords, other.dimensions); let intersection = self_rect.intersect(other_rect); if intersection.dimensions().is_zero() { return } // Find where the origin of the intersection rect sits within both buffers. let other_point = intersection.origin - coords; let self_point = intersection.origin; let mut other_i = (other.width() as usize * other_point.y as usize) + other_point.x as usize; let mut self_i = (self.width() as usize * self_point.y as usize) + self_point.x as usize; let inter_width = intersection.width() as usize; let other_width = other.width() as usize; let self_width = self.width() as usize; let other_slice = other.as_slice(); let self_slice = self.as_mut_slice(); for _ in 0..intersection.height() { let self_row = &mut self_slice[self_i .. self_i+inter_width]; let other_row = &other_slice[other_i .. other_i+inter_width]; self_row.copy_from_slice(other_row); other_i += other_width; self_i += self_width; } } pub fn resize(&mut self, dimensions: Dimensions) { self.pixels.resize(dimensions.area_usize(), Colour::BLACK); self.dimensions = dimensions; } pub fn as_rows(&self) -> std::slice::ChunksExact { self.pixels.chunks_exact(self.dimensions.width as usize) } pub fn as_slice(&self) -> &[Colour] { &self.pixels } pub fn as_mut_slice(&mut self) -> &mut [Colour] { &mut self.pixels } pub fn as_u32_slice(&self) -> &[u32] { unsafe { std::mem::transmute::<&[Colour], &[u32]>(&self.pixels) } } } impl HasDimensions for Buffer { fn dimensions(&self) -> Dimensions { self.dimensions } } impl std::ops::Index for Buffer { type Output = Colour; fn index(&self, i: usize) -> &Colour { &self.pixels[i] } } impl std::ops::IndexMut for Buffer { fn index_mut(&mut self, i: usize) -> &mut Colour { &mut self.pixels[i] } } impl std::ops::Deref for Buffer { type Target = [Colour]; fn deref(&self) -> &[Colour] { self.pixels.as_slice() } } impl std::ops::DerefMut for Buffer { fn deref_mut(&mut self) -> &mut [Colour] { self.pixels.as_mut_slice() } } impl<'a> IntoIterator for &'a Buffer { type Item = &'a Colour; type IntoIter = std::slice::Iter<'a, Colour>; fn into_iter(self) -> Self::IntoIter { self.pixels.iter() } } impl<'a> IntoIterator for &'a mut Buffer { type Item = &'a mut Colour; type IntoIter = std::slice::IterMut<'a, Colour>; fn into_iter(self) -> Self::IntoIter { self.pixels.iter_mut() } }