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
use crate::display_theme::DisplayTheme;
use crate::sim_pixel_color::SimPixelColor;
use crate::Display;
pub struct DisplayBuilder {
width: usize,
height: usize,
scale: usize,
pixel_spacing: usize,
theme: DisplayTheme,
}
impl DisplayBuilder {
pub fn new() -> Self {
Self {
width: 256,
height: 256,
scale: 1,
pixel_spacing: 0,
theme: DisplayTheme::Default,
}
}
pub fn size(&mut self, width: usize, height: usize) -> &mut Self {
if width == 0 || height == 0 {
panic!("with and height must be >= 0");
}
self.width = width;
self.height = height;
self
}
pub fn scale(&mut self, scale: usize) -> &mut Self {
if scale == 0 {
panic!("scale must be >= 0");
}
self.scale = scale;
self
}
pub fn theme(&mut self, theme: DisplayTheme) -> &mut Self {
self.theme = theme;
self.scale(3);
self.pixel_spacing(1);
self
}
pub fn pixel_spacing(&mut self, pixel_spacing: usize) -> &mut Self {
self.pixel_spacing = pixel_spacing;
self
}
pub fn build(&self) -> Display {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window_width = self.width * self.scale + (self.width - 1) * self.pixel_spacing;
let window_height = self.height * self.scale + (self.height - 1) * self.pixel_spacing;
let window = video_subsystem
.window(
"graphics-emulator",
window_width as u32,
window_height as u32,
)
.position_centered()
.build()
.unwrap();
let pixels = vec![SimPixelColor(0, 0, 0); self.width * self.height];
let canvas = window.into_canvas().build().unwrap();
let event_pump = sdl_context.event_pump().unwrap();
Display {
width: self.width,
height: self.height,
scale: self.scale,
pixel_spacing: self.pixel_spacing,
theme: self.theme.clone(),
pixels: pixels.into_boxed_slice(),
canvas,
event_pump,
}
}
}