-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.rs
41 lines (33 loc) · 986 Bytes
/
main.rs
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
32
33
34
35
36
37
38
39
40
41
use wasm_bindgen::prelude::*;
use game_loop::game_loop;
#[wasm_bindgen(start)]
pub fn main() {
let game = Game::new();
game_loop(game, 240, 0.1, |g| {
g.game.your_update_function();
}, |g| {
g.game.your_render_function();
});
}
struct Game {
span: web_sys::Element,
num_updates: u32,
num_renders: u32,
}
impl Game {
pub fn new() -> Self {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let body = document.body().unwrap();
let span = document.create_element("span").unwrap();
body.append_child(&span).unwrap();
Self { span, num_updates: 0, num_renders: 0 }
}
pub fn your_update_function(&mut self) {
self.num_updates += 1;
}
pub fn your_render_function(&mut self) {
self.num_renders += 1;
self.span.set_inner_html(&format!("num_updates: {}, num_renders: {}", self.num_updates, self.num_renders));
}
}