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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
extern crate sdl2;
use crate::game_of_life::{PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH, SQUARE_SIZE};
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::mouse::MouseButton;
use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect};
use sdl2::render::{Canvas, Texture, TextureCreator};
use sdl2::video::{Window, WindowContext};
mod game_of_life {
pub const SQUARE_SIZE: u32 = 16;
pub const PLAYGROUND_WIDTH: u32 = 49;
pub const PLAYGROUND_HEIGHT: u32 = 40;
#[derive(Copy, Clone)]
pub enum State {
Paused,
Playing,
}
pub struct GameOfLife {
playground: [bool; (PLAYGROUND_WIDTH * PLAYGROUND_HEIGHT) as usize],
state: State,
}
impl GameOfLife {
pub fn new() -> GameOfLife {
let mut playground = [false; (PLAYGROUND_WIDTH * PLAYGROUND_HEIGHT) as usize];
// let's make a nice default pattern !
for i in 1..(PLAYGROUND_HEIGHT - 1) {
playground[(1 + i * PLAYGROUND_WIDTH) as usize] = true;
playground[((PLAYGROUND_WIDTH - 2) + i * PLAYGROUND_WIDTH) as usize] = true;
}
for j in 2..(PLAYGROUND_WIDTH - 2) {
playground[(PLAYGROUND_WIDTH + j) as usize] = true;
playground[((PLAYGROUND_HEIGHT - 2) * PLAYGROUND_WIDTH + j) as usize] = true;
}
GameOfLife {
playground: playground,
state: State::Paused,
}
}
pub fn get(&self, x: i32, y: i32) -> Option<bool> {
if x >= 0 && y >= 0 && (x as u32) < PLAYGROUND_WIDTH && (y as u32) < PLAYGROUND_HEIGHT {
Some(self.playground[(x as u32 + (y as u32) * PLAYGROUND_WIDTH) as usize])
} else {
None
}
}
pub fn get_mut(&mut self, x: i32, y: i32) -> Option<&mut bool> {
if x >= 0 && y >= 0 && (x as u32) < PLAYGROUND_WIDTH && (y as u32) < PLAYGROUND_HEIGHT {
Some(&mut self.playground[(x as u32 + (y as u32) * PLAYGROUND_WIDTH) as usize])
} else {
None
}
}
pub fn toggle_state(&mut self) {
self.state = match self.state {
State::Paused => State::Playing,
State::Playing => State::Paused,
}
}
pub fn state(&self) -> State {
self.state
}
pub fn update(&mut self) {
let mut new_playground = self.playground;
for (u, square) in new_playground.iter_mut().enumerate() {
let u = u as u32;
let x = u % PLAYGROUND_WIDTH;
let y = u / PLAYGROUND_WIDTH;
let mut count: u32 = 0;
for i in -1..2 {
for j in -1..2 {
if !(i == 0 && j == 0) {
let peek_x: i32 = (x as i32) + i;
let peek_y: i32 = (y as i32) + j;
if let Some(true) = self.get(peek_x, peek_y) {
count += 1;
}
}
}
}
if count > 3 || count < 2 {
*square = false;
} else if count == 3 {
*square = true;
} else if count == 2 {
*square = *square;
}
}
self.playground = new_playground;
}
}
impl<'a> IntoIterator for &'a GameOfLife {
type Item = &'a bool;
type IntoIter = ::std::slice::Iter<'a, bool>;
fn into_iter(self) -> ::std::slice::Iter<'a, bool> {
self.playground.iter()
}
}
}
fn dummy_texture<'a>(
canvas: &mut Canvas<Window>,
texture_creator: &'a TextureCreator<WindowContext>,
) -> Result<(Texture<'a>, Texture<'a>), String> {
enum TextureColor {
Yellow,
White,
}
let mut square_texture1 = texture_creator
.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE)
.map_err(|e| e.to_string())?;
let mut square_texture2 = texture_creator
.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE)
.map_err(|e| e.to_string())?;
// let's change the textures we just created
{
let textures = vec![
(&mut square_texture1, TextureColor::Yellow),
(&mut square_texture2, TextureColor::White),
];
canvas
.with_multiple_texture_canvas(textures.iter(), |texture_canvas, user_context| {
texture_canvas.set_draw_color(Color::RGB(0, 0, 0));
texture_canvas.clear();
match *user_context {
TextureColor::Yellow => {
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
if (i + j) % 4 == 0 {
texture_canvas.set_draw_color(Color::RGB(255, 255, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 9 == 0 {
texture_canvas.set_draw_color(Color::RGB(200, 200, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
}
}
}
TextureColor::White => {
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
// drawing pixel by pixel isn't very effective, but we only do it once and store
// the texture afterwards so it's still alright!
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and error to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
}
}
}
};
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
// drawing pixel by pixel isn't very effective, but we only do it once and store
// the texture afterwards so it's still alright!
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and serror to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
}
}
})
.map_err(|e| e.to_string())?;
}
Ok((square_texture1, square_texture2))
}
pub fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
// the window is the representation of a window in your operating system,
// however you can only manipulate properties of that window, like its size, whether it's
// fullscreen, ... but you cannot change its content without using a Canvas or using the
// `surface()` method.
let window = video_subsystem
.window(
"rust-sdl2 demo: Game of Life",
SQUARE_SIZE * PLAYGROUND_WIDTH,
SQUARE_SIZE * PLAYGROUND_HEIGHT,
)
.position_centered()
.build()
.map_err(|e| e.to_string())?;
// the canvas allows us to both manipulate the property of the window and to change its content
// via hardware or software rendering. See CanvasBuilder for more info.
let mut canvas = window
.into_canvas()
.target_texture()
.present_vsync()
.build()
.map_err(|e| e.to_string())?;
println!("Using SDL_Renderer \"{}\"", canvas.info().name);
canvas.set_draw_color(Color::RGB(0, 0, 0));
// clears the canvas with the color we set in `set_draw_color`.
canvas.clear();
// However the canvas has not been updated to the window yet, everything has been processed to
// an internal buffer, but if we want our buffer to be displayed on the window, we need to call
// `present`. We need to call this everytime we want to render a new frame on the window.
canvas.present();
// this struct manages textures. For lifetime reasons, the canvas cannot directly create
// textures, you have to create a `TextureCreator` instead.
let texture_creator: TextureCreator<_> = canvas.texture_creator();
// Create a "target" texture so that we can use our Renderer with it later
let (square_texture1, square_texture2) = dummy_texture(&mut canvas, &texture_creator)?;
let mut game = game_of_life::GameOfLife::new();
let mut event_pump = sdl_context.event_pump()?;
let mut frame: u32 = 0;
'running: loop {
// get the inputs here
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => break 'running,
Event::KeyDown {
keycode: Some(Keycode::Space),
repeat: false,
..
} => {
game.toggle_state();
}
Event::MouseButtonDown {
x,
y,
mouse_btn: MouseButton::Left,
..
} => {
let x = (x as u32) / SQUARE_SIZE;
let y = (y as u32) / SQUARE_SIZE;
match game.get_mut(x as i32, y as i32) {
Some(square) => {
*square = !(*square);
}
None => unreachable!(),
};
}
_ => {}
}
}
// update the game loop here
if frame >= 30 {
game.update();
frame = 0;
}
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.clear();
for (i, unit) in (&game).into_iter().enumerate() {
let i = i as u32;
let square_texture = if frame >= 15 {
&square_texture1
} else {
&square_texture2
};
if *unit {
canvas.copy(
square_texture,
None,
Rect::new(
((i % PLAYGROUND_WIDTH) * SQUARE_SIZE) as i32,
((i / PLAYGROUND_WIDTH) * SQUARE_SIZE) as i32,
SQUARE_SIZE,
SQUARE_SIZE,
),
)?;
}
}
canvas.present();
if let game_of_life::State::Playing = game.state() {
frame += 1;
};
}
Ok(())
}