dvd_render/
grid.rs

1// TODO: add colors
2#[derive(Copy, Clone)]
3pub struct GridCell {
4	character: char
5}
6
7impl GridCell {
8	#[inline]
9	pub fn new(character: char) -> Self {
10		Self { character }
11	}
12
13	#[inline]
14	pub fn space() -> Self {
15		Self { character: ' ' }
16	}
17
18	#[inline]
19	pub fn character(&self) -> char {
20		self.character
21	}
22}
23
24#[derive(Clone)]
25pub struct Grid<const W: usize, const H: usize> {
26	cells: [[GridCell; W]; H]
27}
28
29impl<const W: usize, const H: usize> Default for Grid<W, H> {
30	fn default() -> Self {
31		Self {
32			cells: [[GridCell::space(); W]; H]
33		}
34	}
35}
36
37impl<const W: usize, const H: usize> Grid<W, H> {
38	/// panics if out of bounds
39	pub fn set(&mut self, x: usize, y: usize, c: GridCell) {
40		self.cells[y][x] = c;
41	}
42
43	pub(crate) fn chars(&self) -> Vec<char> {
44		let mut chars = vec![];
45
46		for row in self.cells {
47			for cell in row {
48				if !chars.contains(&cell.character) {
49					chars.push(cell.character);
50				}
51			}
52		}
53
54		chars
55	}
56
57	#[inline]
58	pub(crate) fn cells(&self) -> &[[GridCell; W]; H] {
59		&self.cells
60	}
61}