aboutsummaryrefslogtreecommitdiff
path: root/src/game.rs
blob: 4b527d27c0a864953489886fb2d748988ce5de4e (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
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);
    }
}