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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#![allow(dead_code, non_camel_case_types, non_snake_case)]
#[macro_use]
pub extern crate log;
extern crate alloc;
pub mod audio;
pub mod color;
pub mod graphics;
pub mod image;
pub mod math;
pub mod time;
pub use crate::event::*;
pub use crate::prelude::*;
pub use cgmath;
pub use crevice;
pub use fontdue;
pub(crate) use crate::global::ctx;
mod event;
mod global;
mod prelude;
mod render;
use crate::audio::AudioState;
use crate::event::EventConverter;
use crate::global::Ctx;
use crate::graphics::Texture;
use crate::image::Image;
use crate::render::{OpenGLState, OpenGLWindow};
use crate::time::{Instant, Timer};
use cgmath::Vector2;
use core::time::Duration;
use winit::event::Event as WinitEvent;
use winit::event_loop::ControlFlow;
#[cfg(not(target_arch = "wasm32"))]
fn init_logger() {
match simple_logger::init_with_level(log::Level::Trace) {
Ok(_) => info!("Using the default logger: simple_logger."),
Err(_) => info!("Using the provided logger."),
}
}
#[cfg(target_arch = "wasm32")]
fn init_logger() {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
match console_log::init_with_level(log::Level::Trace) {
Ok(_) => info!("Using the default logger: console_log."),
Err(_) => info!("Using the provided logger."),
}
}
pub struct Context {
window: OpenGLWindow,
stop: bool,
control_flow: Option<ControlFlow>,
last_update: Instant,
wait_next: Instant,
wait_periodic: Option<Duration>,
}
impl Context {
pub fn start<T: 'static + FnMut(Event, &mut Context)>(
desc: WindowSettings,
event_handler_creator: fn(&mut Context) -> T,
) -> ! {
init_logger();
let audio = AudioState::init();
let event_loop = winit::event_loop::EventLoop::new();
let (graphics, window) = OpenGLState::init(&desc, &event_loop);
Ctx::init(graphics, audio);
let mut input = EventConverter::new(window.logical_size());
let mut context = Context {
window,
stop: false,
control_flow: Some(ControlFlow::Poll),
last_update: Instant::now(),
wait_next: Instant::now(),
wait_periodic: None,
};
let mut event_handler = event_handler_creator(&mut context);
let mut update_timer = Timer::new("Event::Update");
event_loop.run(move |event, _, control_flow| {
match event {
WinitEvent::WindowEvent {
event,
..
} => {
input.push(event, &mut event_handler, &mut context);
}
WinitEvent::MainEventsCleared => {
let now = Instant::now();
if now >= context.wait_next {
if let Some(duration) = context.wait_periodic {
context.wait_next += duration;
if context.wait_next < now {
context.wait_next = now;
}
context.control_flow = Some(ControlFlow::WaitUntil(context.wait_next));
}
let delta = now - context.last_update;
context.last_update = now;
update_timer.start();
event_handler(Event::Update(delta.as_secs_f32()), &mut context);
context.window.swap_buffers();
update_timer.stop();
}
}
WinitEvent::LoopDestroyed => {
context.stop = true;
}
_ => {}
}
if context.stop {
*control_flow = ControlFlow::Exit;
} else if let Some(next_control_flow) = context.control_flow {
*control_flow = next_control_flow;
context.control_flow = None;
}
});
}
pub(crate) fn window_check_resize(&mut self) {
let gpu = ctx().graphics();
gpu.resize(self.window.physical_size(), self.window.logical_size());
}
pub fn window_title(&mut self, title: &str) {
self.window.set_title(title);
}
pub fn window_logical_size(&self) -> Vector2<f32> {
self.window.logical_size()
}
pub fn window_physical_size(&self) -> Vector2<f32> {
self.window.physical_size()
}
pub fn window_display_mode(&mut self, display_mode: DisplayMode) {
self.window.set_display_mode(display_mode);
}
pub fn stop(&mut self) {
self.stop = true;
}
pub fn wait_for(&mut self, duration: Duration) {
self.wait_until(Instant::now() + duration);
}
pub fn wait_until(&mut self, instant: Instant) {
if instant > self.wait_next {
self.wait_next = instant;
self.control_flow = Some(ControlFlow::WaitUntil(self.wait_next));
}
}
pub fn wait_periodic(&mut self, duration: Option<Duration>) {
self.wait_periodic = duration;
}
}
pub fn default_texture() -> Texture {
let gpu = ctx().graphics();
gpu.default_texture()
}
pub fn max_texture_size() -> usize {
ctx().graphics().max_texture_size() as usize
}
pub fn clear(clear_mode: ClearMode) {
let gl = ctx().graphics().gl();
if let Some(clear_color) = clear_mode.color {
gl.clear_color(clear_color);
}
gl.clear(clear_mode.mode);
}