1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
pub use colour::Colour;
pub use geometry::HasDimensions;
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::slice::{ChunksExact, ChunksExactMut, SliceIndex};
pub type Point = geometry::Point<i32>;
pub type Dimensions = geometry::Dimensions<u32>;
pub type Rect = geometry::Rect<i32, u32>;
pub struct Buffer {
pixels: Vec<Colour>,
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;
for _ in 0..intersection.height() {
let self_row = &mut self[self_i .. self_i+inter_width];
let other_row = &other[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) -> ChunksExact<Colour> {
self.pixels.chunks_exact(self.dimensions.width as usize)
}
pub fn as_mut_rows(&mut self) -> ChunksExactMut<Colour> {
self.pixels.chunks_exact_mut(self.dimensions.width as usize)
}
pub fn as_u32_slice(&self) -> &[u32] {
unsafe { std::mem::transmute::<&[Colour], &[u32]>(&self.pixels) }
}
}
impl HasDimensions<u32> for Buffer {
fn dimensions(&self) -> Dimensions { self.dimensions }
}
impl<I: SliceIndex<[Colour]>> Index<I> for Buffer {
type Output = I::Output;
fn index(&self, index: I) -> &Self::Output {
&self.pixels[index]
}
}
impl<I: SliceIndex<[Colour]>> IndexMut<I> for Buffer {
fn index_mut(&mut self, index: I) -> &mut Self::Output {
&mut self.pixels[index]
}
}
impl Deref for Buffer {
type Target = [Colour];
fn deref(&self) -> &[Colour] {
self.pixels.as_slice()
}
}
impl 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()
}
}
|