diff options
author | Nao Pross <naopross@thearcway.org> | 2018-12-17 02:07:27 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-12-17 02:07:27 +0100 |
commit | b747ea18619926e1cff0252e0e7b2985b7868faa (patch) | |
tree | b4fec63f4317ab196b1940a42bfda77debb017b9 /src | |
parent | Add sfml and tiled dependencies (diff) | |
download | Subconscious-rs-b747ea18619926e1cff0252e0e7b2985b7868faa.tar.gz Subconscious-rs-b747ea18619926e1cff0252e0e7b2985b7868faa.zip |
Add game state object shared across threads
Diffstat (limited to '')
-rw-r--r-- | src/game.rs | 15 | ||||
-rw-r--r-- | src/graphics.rs | 25 | ||||
-rw-r--r-- | src/main.rs | 41 |
3 files changed, 56 insertions, 25 deletions
diff --git a/src/game.rs b/src/game.rs index 36ed4b8..ab04b21 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,4 +1,15 @@ +pub struct State { + pub running: bool +} + + + +pub fn new() -> State { + return State { + running: true, + }; +} + +pub fn update(state: &mut State) { -pub fn start() { - }
\ No newline at end of file diff --git a/src/graphics.rs b/src/graphics.rs index ca98946..003f182 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -1,6 +1,5 @@ extern crate sfml; - use sfml::window::{ ContextSettings, Event, @@ -13,20 +12,10 @@ use sfml::graphics::{ Color, }; - -struct State { - running: bool -} - - -pub fn start() { +pub fn start() -> RenderWindow { let default_window_size = (1280, 720); let default_framerate = 80; - let mut state = State { - running: true - }; - let context_settings = ContextSettings { antialiasing_level: 0, ..Default::default() @@ -42,22 +31,18 @@ pub fn start() { window.set_framerate_limit(default_framerate); window.set_vertical_sync_enabled(true); - - while state.running { - render(&mut window); - update(&mut window, &mut state); - } + return window; } -fn render(window: &mut RenderWindow) { +pub fn render(window: &mut RenderWindow) { window.clear(&Color::BLACK); window.display(); } -fn update(window: &mut RenderWindow, state: &mut State) { +pub fn update(window: &mut RenderWindow) { while let Some(ev) = window.poll_event() { match ev { - Event::Closed => state.running = false, + Event::Closed => window.close(), _ => {}, } } diff --git a/src/main.rs b/src/main.rs index 2679ac4..e2353e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,48 @@ mod game; mod graphics; +use std::thread; +use std::sync::{Arc, Mutex}; fn main() { - game::start(); + let state_ = Arc::new(Mutex::new(game::new())); - let graphics_thread = std::thread::spawn(move || { - graphics::start(); + let state = state_.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_.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(), + }; + + state.running = false; }); graphics_thread.join().unwrap(); + game_thread.join().unwrap(); } |