diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2023-12-24 21:13:59 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2023-12-24 21:13:59 +1300 |
commit | 2322c4c2b335ca398ec039503257e20d139a699f (patch) | |
tree | 0d6b147617e401dfbf84c6456ef152016e6cd115 | |
download | buffer-2322c4c2b335ca398ec039503257e20d139a699f.zip |
First commitv1.0.0
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.toml | 12 | ||||
-rw-r--r-- | src/lib.rs | 81 |
3 files changed, 95 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d770c73 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "buffer" +version = "1.0.0" +authors = ["Ben Bridle"] +edition = "2021" +description = "Common 2-dimensional colour buffer type" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +geometry = { "git" = "git://benbridle.com/geometry", "tag" = "v1.0.0" } +colour = { "git" = "git://benbridle.com/colour", "tag" = "v1.0.0" } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1ee51b4 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,81 @@ +pub use colour::Colour; +pub type Point = geometry::Point<i32>; +pub type Dimensions = geometry::Dimensions<u32>; +pub type Rect = geometry::Rect<i32, u32>; +pub use geometry::HasDimensions; + +pub struct Buffer { + array: Vec<Colour>, + dimensions: Dimensions, +} + +impl Buffer { + pub const ZERO: Buffer = Buffer { array: Vec::new(), dimensions: Dimensions::ZERO }; + + pub fn new(dimensions: Dimensions) -> Self { + let array = vec![Colour::BLACK; dimensions.area_usize()]; + Self { array, dimensions } + } + pub fn new_with_fill(dimensions: Dimensions, colour: Colour) -> Self { + let array = vec![colour; dimensions.area_usize()]; + Self { array, dimensions } + } + pub fn fill(&mut self, colour: Colour) { + self.array.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.array.resize(dimensions.area_usize(), Colour::BLACK); + self.dimensions = dimensions; + } + pub fn as_rows(&self) -> std::slice::ChunksExact<Colour> { + self.array.chunks_exact(self.dimensions.width as usize) + } + pub fn as_slice(&self) -> &[Colour] { + &self.array + } + pub fn as_mut_slice(&mut self) -> &mut [Colour] { + &mut self.array + } + pub fn as_u32_slice(&self) -> &[u32] { + unsafe { std::mem::transmute::<&[Colour], &[u32]>(&self.array) } + } +} + +impl HasDimensions<u32> for Buffer { + fn dimensions(&self) -> Dimensions { self.dimensions } +} + +impl std::ops::Index<usize> for Buffer { + type Output = Colour; + fn index(&self, i: usize) -> &Colour { &self.array[i] } +} + +impl std::ops::IndexMut<usize> for Buffer { + fn index_mut(&mut self, i: usize) -> &mut Colour { &mut self.array[i] } +} |