From 1b6a7e0d00703e3da2e1620b5a2b2cba027161de Mon Sep 17 00:00:00 2001 From: Joris Date: Tue, 28 Jan 2020 09:55:58 +0100 Subject: Implement game of life --- src/game_loop.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/game_loop.rs (limited to 'src/game_loop.rs') diff --git a/src/game_loop.rs b/src/game_loop.rs new file mode 100644 index 0000000..34dd477 --- /dev/null +++ b/src/game_loop.rs @@ -0,0 +1,31 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use wasm_bindgen::prelude::Closure; +use wasm_bindgen::JsCast; + +use crate::game::Game; + +pub fn run(game: Game, update_period: i32) { + game.render(); + + let f = Rc::new(RefCell::new(None)); + let g = f.clone(); + + *g.borrow_mut() = Some(Closure::wrap(Box::new(move |_| { + + game.update(); + game.render(); + + set_timeout(f.borrow().as_ref().unwrap(), update_period); + + }) as Box)); + + set_timeout(g.borrow().as_ref().unwrap(), update_period); +} + +fn set_timeout(f: &Closure, timeout: i32) { + web_sys::window().unwrap() + .set_timeout_with_callback_and_timeout_and_arguments_0(f.as_ref().unchecked_ref(), timeout) + .unwrap(); +} -- cgit v1.2.3