-
Notifications
You must be signed in to change notification settings - Fork 23
/
using_winit.rs
62 lines (50 loc) · 1.62 KB
/
using_winit.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
use game_loop::game_loop;
// For convenience, game_loop re-exports winit so you don't need to add it as
// an additional dependency of your crate.
use game_loop::winit::event::{Event, WindowEvent};
use game_loop::winit::event_loop::EventLoop;
use game_loop::winit::window::{Window, WindowAttributes};
use std::sync::Arc;
fn main() {
let event_loop = EventLoop::new().unwrap();
let window = event_loop.create_window(Window::default_attributes()).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(); }
}).unwrap();
}
#[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
}
}