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
use crate::common::Dimensions;
/// #### Description
/// A display buffer is the buffer that holds all of the information
/// about what characters to display to the screen
///
#[derive(Debug)]
pub struct DisplayBuffer {
/// #### Description
/// The dimensions of the buffer
///
dimensions: Dimensions,
/// #### Description
/// The heap allocated buffer of characters
///
buffer: Box<[Box<[char]>]>
}
impl DisplayBuffer {
/// #### Description
/// Creates a new display buffer with the given dimensions
///
/// #### Arguments
/// * `dimensions`: [Dimensions] - The dimensions of the display buffer
///
/// #### Returns
/// [DisplayBuffer] - A display buffer with the specified attributes
///
pub fn new(dimensions: Dimensions) -> Self {
// Generate the buffer
let mut buffer_vec = vec![];
for _ in 0..dimensions.get_height() {
let mut row = vec![];
for _ in 0..dimensions.get_width() {
row.push(' ');
}
buffer_vec.push(row.into_boxed_slice());
}
let buffer = buffer_vec.into_boxed_slice();
return Self {dimensions, buffer};
}
/// #### Description
/// Draws the buffer to the terminal
///
pub fn draw(&self) {
// Combine buffer into a single string
let output = self.buffer.iter().map(|row| {
let mut row_str = row.iter().collect::<String>();
row_str.push('\n');
return row_str;
}).collect::<String>();
// write the string to the terminal
println!("{}", output);
}
/// #### Description
/// Retrieves the width of the display buffer
///
/// #### Returns
/// [u32] - The width of the display buffer
///
pub fn get_width(&self) -> u32 {
return self.dimensions.get_width();
}
/// #### Description
/// Retrieves the height of the display buffer
///
/// #### Returns
/// [u32] - The height of the display buffer
///
pub fn get_height(&self) -> u32 {
return self.dimensions.get_height();
}
/// #### Description
/// Writes a character to the display buffer
///
/// If the coordinates are out of the bounds of the buffer,
/// it ignores the write
///
/// #### Arguments
/// * `c`: [char] - The character to insert
/// * `x`: [u32] - The x position to write to
/// * `y`: [u32] - The y position to write to
///
pub fn set_char(&mut self, c: char, x: i32, y: i32) {
// Guard for detecting if the coordinates are in bounds
if self.dimensions.get_width() <= x as u32 ||
self.dimensions.get_height() <= y as u32 ||
x < 0 ||
y < 0
{
return;
}
else {
self.buffer[y as usize][x as usize] = c;
}
}
/// #### Description
/// Clears the display buffer to a specified character
///
/// #### Arguments
/// * `c`: [char] - The character to clear with
///
pub fn clear(&mut self, c: char) {
for y in 0..self.dimensions.get_height() {
for x in 0..self.dimensions.get_width() {
self.buffer[y as usize][x as usize] = c;
}
}
}
}