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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#![deny(missing_docs)]
mod display_builder;
mod display_theme;
mod sim_pixel_color;
pub use crate::display_builder::DisplayBuilder;
pub use crate::display_theme::DisplayTheme;
pub use crate::sim_pixel_color::SimPixelColor;
use embedded_graphics::drawable::Pixel;
use embedded_graphics::Drawing;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::rect::Rect;
use sdl2::render;
pub struct Display {
width: usize,
height: usize,
scale: usize,
pixel_spacing: usize,
theme: DisplayTheme,
pixels: Box<[SimPixelColor]>,
canvas: render::Canvas<sdl2::video::Window>,
event_pump: sdl2::EventPump,
}
impl Display {
pub fn run_once(&mut self) -> bool {
for event in self.event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => {
return true;
}
_ => {}
}
}
self.canvas.set_draw_color(self.theme.background_color());
self.canvas.clear();
let pitch = self.scale + self.pixel_spacing;
for (index, value) in self.pixels.iter().enumerate() {
if let Some(c) = self.theme.pixel_color(value) {
self.canvas.set_draw_color(c);
let x = (index % self.width * pitch) as i32;
let y = (index / self.width * pitch) as i32;
let r = Rect::new(x, y, self.scale as u32, self.scale as u32);
self.canvas.fill_rect(r).unwrap();
}
}
self.canvas.present();
false
}
}
impl Drawing<SimPixelColor> for Display {
fn draw<T>(&mut self, item_pixels: T)
where
T: IntoIterator<Item = Pixel<SimPixelColor>>,
{
for Pixel(coord, color) in item_pixels {
let x = coord[0] as usize;
let y = coord[1] as usize;
if x >= self.width || y >= self.height {
continue;
}
self.pixels[y * self.width + x] = color;
}
}
}