aboutsummaryrefslogtreecommitdiff
path: root/src/game_loop.rs
diff options
context:
space:
mode:
authorJoris2020-01-28 09:55:58 +0100
committerJoris2020-01-29 10:12:31 +0100
commit1b6a7e0d00703e3da2e1620b5a2b2cba027161de (patch)
tree5143f784e1529d3b6c04116c84f09c426bb257b0 /src/game_loop.rs
parent197b6fa7aa810147d63209408c3a378ec552d0f4 (diff)
Implement game of life
Diffstat (limited to 'src/game_loop.rs')
-rw-r--r--src/game_loop.rs31
1 files changed, 31 insertions, 0 deletions
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<dyn FnMut(i32)>));
+
+ set_timeout(g.borrow().as_ref().unwrap(), update_period);
+}
+
+fn set_timeout(f: &Closure<dyn FnMut(i32)>, timeout: i32) {
+ web_sys::window().unwrap()
+ .set_timeout_with_callback_and_timeout_and_arguments_0(f.as_ref().unchecked_ref(), timeout)
+ .unwrap();
+}