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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#![deny(missing_docs)]
#![deny(missing_copy_implementations)]
extern crate window;
extern crate input;
extern crate viewport;
use std::thread::sleep;
use std::time::{Duration, Instant};
use std::cmp;
use window::Window;
use input::{AfterRenderArgs, Event, IdleArgs, RenderArgs, UpdateArgs};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Idle {
No,
Yes,
}
#[derive(Copy, Clone, Debug)]
struct UpdateState {
dt_update_in_ns: u64,
dt: f64,
}
#[derive(Copy, Clone, Debug)]
enum State {
Render,
SwapBuffers,
UpdateLoop(Idle),
HandleEvents(UpdateState),
Update(UpdateState),
}
#[derive(Copy, Clone, Debug)]
pub struct EventSettings {
pub max_fps: u64,
pub ups: u64,
pub swap_buffers: bool,
pub bench_mode: bool,
}
impl EventSettings {
pub fn new() -> EventSettings {
EventSettings {
max_fps: DEFAULT_MAX_FPS,
ups: DEFAULT_UPS,
swap_buffers: true,
bench_mode: false,
}
}
}
impl Default for EventSettings {
fn default() -> EventSettings {
EventSettings::new()
}
}
#[derive(Copy, Clone)]
pub struct Events {
state: State,
last_update: Instant,
last_frame: Instant,
dt_update_in_ns: u64,
dt_frame_in_ns: u64,
dt: f64,
settings: EventSettings,
}
static BILLION: u64 = 1_000_000_000;
fn ns_to_duration(ns: u64) -> Duration {
let secs = ns / BILLION;
let nanos = (ns % BILLION) as u32;
Duration::new(secs, nanos)
}
fn duration_to_secs(dur: Duration) -> f64 {
dur.as_secs() as f64 + dur.subsec_nanos() as f64 / 1_000_000_000.0
}
pub const DEFAULT_UPS: u64 = 120;
pub const DEFAULT_MAX_FPS: u64 = 60;
impl Events {
pub fn new(settings: EventSettings) -> Events {
let start = Instant::now();
Events {
state: State::Render,
last_update: start,
last_frame: start,
dt_update_in_ns: BILLION / settings.ups,
dt_frame_in_ns: BILLION / settings.max_fps,
dt: 1.0 / settings.ups as f64,
settings: settings,
}
}
pub fn next<W>(&mut self, window: &mut W) -> Option<Event<W::Event>>
where W: Window
{
loop {
if window.should_close() {
return None;
}
self.state = match self.state {
State::Render => {
if let Some(e) = window.poll_event() {
if self.settings.bench_mode {
continue;
} else {
return Some(Event::Input(e));
}
}
if window.should_close() {
return None;
}
if self.settings.bench_mode {
self.last_frame += ns_to_duration(self.dt_frame_in_ns);
} else {
self.last_frame = Instant::now();
}
let size = window.size();
let draw_size = window.draw_size();
if size.width != 0 && size.height != 0 {
self.state = State::SwapBuffers;
return Some(Event::Render(RenderArgs {
ext_dt: duration_to_secs(self.last_frame
.duration_since(self.last_update)),
width: size.width,
height: size.height,
draw_width: draw_size.width,
draw_height: draw_size.height,
}));
}
State::UpdateLoop(Idle::No)
}
State::SwapBuffers => {
if self.settings.swap_buffers {
window.swap_buffers();
}
self.state = State::UpdateLoop(Idle::No);
return Some(Event::AfterRender(AfterRenderArgs));
}
State::UpdateLoop(ref mut idle) => {
if self.settings.bench_mode {
let next_frame = self.last_frame + ns_to_duration(self.dt_frame_in_ns);
let next_update = self.last_update + ns_to_duration(self.dt_update_in_ns);
let next_event = cmp::min(next_frame, next_update);
if next_event == next_frame {
State::Render
} else {
State::HandleEvents(UpdateState {
dt_update_in_ns: self.dt_update_in_ns,
dt: self.dt,
})
}
} else {
let current_time = Instant::now();
let next_frame = self.last_frame + ns_to_duration(self.dt_frame_in_ns);
let next_update = self.last_update + ns_to_duration(self.dt_update_in_ns);
let next_event = cmp::min(next_frame, next_update);
if next_event > current_time {
if let Some(x) = window.poll_event() {
*idle = Idle::No;
return Some(Event::Input(x));
} else if *idle == Idle::No {
*idle = Idle::Yes;
let seconds = duration_to_secs(next_event - current_time);
return Some(Event::Idle(IdleArgs { dt: seconds }));
}
sleep(next_event - current_time);
State::UpdateLoop(Idle::No)
} else if next_event == next_frame {
State::Render
} else {
State::HandleEvents(UpdateState {
dt_update_in_ns: self.dt_update_in_ns,
dt: self.dt,
})
}
}
}
State::HandleEvents(update_state) => {
if self.settings.bench_mode {
match window.poll_event() {
None => State::Update(update_state),
Some(_) => State::HandleEvents(update_state),
}
} else {
match window.poll_event() {
None => State::Update(update_state),
Some(x) => {
return Some(Event::Input(x));
}
}
}
}
State::Update(update_state) => {
self.state = State::UpdateLoop(Idle::No);
self.last_update += ns_to_duration(update_state.dt_update_in_ns);
return Some(Event::Update(UpdateArgs { dt: update_state.dt }));
}
};
}
}
}
pub trait EventLoop: Sized {
fn get_event_settings(&self) -> EventSettings;
fn set_event_settings(&mut self, settings: EventSettings);
fn set_ups(&mut self, frames: u64) {
let old_settings = self.get_event_settings();
self.set_event_settings(EventSettings { ups: frames, ..old_settings });
}
fn ups(mut self, frames: u64) -> Self {
self.set_ups(frames);
self
}
fn set_max_fps(&mut self, frames: u64) {
let old_settings = self.get_event_settings();
self.set_event_settings(EventSettings { max_fps: frames, ..old_settings })
}
fn max_fps(mut self, frames: u64) -> Self {
self.set_max_fps(frames);
self
}
fn set_swap_buffers(&mut self, enable: bool) {
let old_settings = self.get_event_settings();
self.set_event_settings(EventSettings { swap_buffers: enable, ..old_settings })
}
fn swap_buffers(mut self, enable: bool) -> Self {
self.set_swap_buffers(enable);
self
}
fn set_bench_mode(&mut self, enable: bool) {
let old_settings = self.get_event_settings();
self.set_event_settings(EventSettings { bench_mode: enable, ..old_settings })
}
fn bench_mode(mut self, enable: bool) -> Self {
self.set_bench_mode(enable);
self
}
}
impl EventLoop for EventSettings {
fn get_event_settings(&self) -> Self {
*self
}
fn set_event_settings(&mut self, settings: Self) {
*self = settings;
}
}
impl EventLoop for Events {
fn get_event_settings(&self) -> EventSettings {
self.settings
}
fn set_event_settings(&mut self, settings: EventSettings) {
*self = Events::new(settings);
}
}