-
Notifications
You must be signed in to change notification settings - Fork 23
/
using_tao.rs
69 lines (55 loc) · 1.8 KB
/
using_tao.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use game_loop::game_loop;
// For convenience, game_loop re-exports tao so you don't need to add it as
// an additional dependency of your crate.
use game_loop::tao::event::{Event, WindowEvent};
use game_loop::tao::event_loop::EventLoop;
use game_loop::tao::menu::{MenuBar, MenuItem};
use game_loop::tao::window::{Window, WindowBuilder};
use std::sync::Arc;
fn main() {
let mut file_menu = MenuBar::new();
file_menu.add_native_item(MenuItem::Quit);
let mut menu = MenuBar::new();
menu.add_submenu("File", true, file_menu);
let event_loop = EventLoop::new();
let window = WindowBuilder::new().with_menu(menu).build(&event_loop).unwrap();
let window = Arc::new(window);
let game = Game::new();
game_loop(event_loop, window, game, 240, 0.1, |g| {
g.game.your_update_function();
}, |g| {
g.game.your_render_function(&g.window);
}, |g, event| {
if !g.game.your_window_handler(event) { g.exit(); }
});
}
#[derive(Default)]
struct Game {
num_updates: u32,
num_renders: u32,
}
impl Game {
pub fn new() -> Self {
Self::default()
}
pub fn your_update_function(&mut self) {
self.num_updates += 1;
}
pub fn your_render_function(&mut self, window: &Window) {
self.num_renders += 1;
window.set_title(&format!("num_updates: {}, num_renders: {}", self.num_updates, self.num_renders));
}
// A very simple handler that returns false when CloseRequested is detected.
pub fn your_window_handler(&self, event: &Event<()>) -> bool {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => {
return false;
}
_ => {}
},
_ => {}
}
true
}
}