-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_loop.rs
158 lines (132 loc) · 5.7 KB
/
game_loop.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::window::window_state::WindowState;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{EventLoop, EventLoopBuilder, EventLoopWindowTarget};
#[cfg(target_os = "android")]
use winit::platform::android::EventLoopBuilderExtAndroid; // Necessary for with_android_app
#[cfg(target_family = "wasm")]
use winit::platform::web::EventLoopExtWebSys;
// http://gameprogrammingpatterns.com/game-loop.html
// https://zdgeier.com/wgpuintro.html
// https://sotrh.github.io/learn-wgpu/beginner/tutorial5-textures/#loading-an-image-from-a-file
/// Client game loop
pub fn game_loop() -> Result<(), String> {
// Create the main loop
debug!("Creating event loop...");
#[cfg(not(target_os = "android"))]
let event_loop: EventLoop<()> = EventLoopBuilder::new()
.build()
.expect("Could not create an event loop!");
#[cfg(target_os = "android")]
let event_loop: EventLoop<()> = EventLoopBuilder::new()
.with_android_app(crate::game::ANDROID_APP.get().unwrap().to_owned())
.build()
.expect("Could not create an event loop!");
let mut window_state: Option<WindowState> = None;
debug!("Starting event loop...");
let event_loop_closure = move |event: Event<()>, window_target: &EventLoopWindowTarget<()>| {
/* Update Order
*
* processInput() - Handles user input
* update() - Handle game physics
* render() - Handle graphics
*/
debug!("Loop...");
// ControlFlow::Poll continuously runs the event loop, even if the OS hasn't
// dispatched any events. This is ideal for games and similar applications.
window_target.set_control_flow(winit::event_loop::ControlFlow::Poll);
// TODO: Determine if this should be selected depending on menus and pause state
// ControlFlow::Wait pauses the event loop if no events are available to process.
// This is ideal for non-game applications that only update in response to user
// input, and uses significantly less power/CPU time than ControlFlow::Poll.
// window_target.set_control_flow(winit::event_loop::ControlFlow::Wait);
match event {
// The close button was pressed
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
crate::window::events::close_requested(window_target);
}
Event::Resumed => {
if let Some(window_state) = &mut window_state {
crate::window::events::resumed_window(window_state);
} else {
let window_state_future = crate::window::events::create_window(window_target);
#[cfg(not(target_family = "wasm"))]
if cfg!(not(target_family = "wasm")) {
window_state = Some(futures::executor::block_on(window_state_future));
}
#[cfg(target_family = "wasm")]
if cfg!(target_family = "wasm") {
let get_window_state = async {
window_state = Some(window_state_future.await);
};
wasm_bindgen_futures::spawn_local(get_window_state);
}
}
}
Event::Suspended => {
crate::window::events::suspended_window();
}
// Keyboard keys were pressed
Event::WindowEvent {
event: WindowEvent::KeyboardInput { event, .. },
..
} => {
crate::window::events::pressed_key(event, window_target);
}
Event::WindowEvent {
event: WindowEvent::MouseInput { state, button, .. },
..
} => {
crate::window::events::clicked_mouse(state, button, window_target);
}
Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
// Technically, this can fail if another window was resized before this one was created
if let Some(window_state) = window_state.as_ref() {
crate::window::events::resized_window(window_state, size);
}
}
// Can be used to pause the game
Event::WindowEvent {
event: WindowEvent::Focused(focused),
..
} => {
crate::window::events::changed_focus(focused);
}
// Called every time the engine needs to refresh a frame
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
} => {
if let Some(window_state) = window_state.as_ref() {
crate::window::events::requested_redraw(window_state);
}
}
// Last event to ever be executed on shutdown
Event::LoopExiting => {
crate::window::events::exiting_loop();
}
// New events are incoming
Event::NewEvents(_) => {
crate::window::events::new_events();
}
// New events are incoming
Event::AboutToWait => {
crate::window::events::about_to_wait_event();
}
// All unnamed events
_ => {
crate::window::events::unhandled_event(event);
}
}
};
#[cfg(not(target_family = "wasm"))]
let _: Result<(), winit::error::EventLoopError> = event_loop.run(event_loop_closure);
#[cfg(target_family = "wasm")]
event_loop.spawn(event_loop_closure);
Ok(())
}