summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 7b2809cfe9d8c9f11c9853f968212b29b4c26638 (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
54
55
56
57
58
59
60
61
62
63
#[macro_use]
extern crate rust_embed;
extern crate sfml;
extern crate tiled;

mod game;
mod graphics;

use std::thread;
use std::sync::{Arc, Mutex};

fn main() {
	let game_state_mutex = Arc::new(Mutex::new(game::new()));

	let game_state = game_state_mutex.clone();
	let game_thread = thread::spawn(move || {
		loop {
			// aquire state resource
			let mut game_state = match game_state.lock() {
				Ok(game_state) => game_state,
				Err(poisoned) => poisoned.into_inner(),
			};

			if game_state.running == false {
				break;
			}

			game::update(&mut game_state);
		}
	});


	let game_state = game_state_mutex.clone();
	let graphics_thread = thread::spawn(move || {
		let mut graphics = graphics::Graphics::new();

		while graphics.is_running() {
			// aquire state resource
			let game_state = match game_state.lock() {
				Ok(game_state) => game_state,
				Err(poisoned) => poisoned.into_inner(),
			};

			graphics.render(&game_state);
			graphics.update();
		}

		{
			// aquire state resource
			let mut game_state = match game_state.lock() {
				Ok(game_state) => game_state,
				Err(poisoned) => poisoned.into_inner(),
			};

			// stop game thread
			game_state.running = false;
		}
	});

	// wait for both thread to die
	graphics_thread.join().unwrap();
	game_thread.join().unwrap();
}