aboutsummaryrefslogtreecommitdiff
path: root/src/game.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.rs
parent197b6fa7aa810147d63209408c3a378ec552d0f4 (diff)
Implement game of life
Diffstat (limited to 'src/game.rs')
-rw-r--r--src/game.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/game.rs b/src/game.rs
new file mode 100644
index 0000000..4b527d2
--- /dev/null
+++ b/src/game.rs
@@ -0,0 +1,31 @@
+use std::cell::RefCell;
+use std::rc::Rc;
+
+use crate::canvas::Canvas;
+use crate::state::State;
+
+pub struct Game {
+ canvas: Canvas,
+ state: Rc<RefCell<State>>,
+}
+
+impl Game {
+ pub fn new(canvas_id: &str, width: u32, height: u32) -> Game {
+ let canvas = Canvas::new(canvas_id, width, height);
+ let state = Rc::new(RefCell::new(State::new(width, height)));
+
+ Game {
+ canvas,
+ state,
+ }
+ }
+
+ pub fn update(&self) {
+ let next_state = self.state.borrow().next();
+ *self.state.borrow_mut() = next_state;
+ }
+
+ pub fn render(&self) {
+ self.state.borrow().draw(&self.canvas);
+ }
+}