blob: 4d32b95a919ff1a8b33dc0c18ea4531064c61ab1 (
plain)
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
|
#[macro_use]
extern crate rust_embed;
mod game;
mod graphics;
use std::thread;
use std::sync::{Arc, Mutex};
fn main() {
let state_mutex = Arc::new(Mutex::new(game::new()));
let state = state_mutex.clone();
let game_thread = thread::spawn(move || {
loop {
// aquire state resource
let mut state = match state.lock() {
Ok(state) => state,
Err(poisoned) => poisoned.into_inner(),
};
if state.running == false {
break;
}
game::update(&mut state);
}
});
let state = state_mutex.clone();
let graphics_thread = thread::spawn(move || {
let mut window = graphics::start();
while window.is_open() {
graphics::render(&mut window);
graphics::update(&mut window);
}
// aquire state resource
let mut state = match state.lock() {
Ok(state) => state,
Err(poisoned) => poisoned.into_inner(),
};
// stop game thread
state.running = false;
});
// wait for both thread to die
graphics_thread.join().unwrap();
game_thread.join().unwrap();
}
|