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
#![deny(missing_docs)]
#![deny(missing_copy_implementations)]
extern crate clock_ticks;
extern crate window;
extern crate viewport;
use std::thread::sleep_ms;
use std::cmp;
use std::marker::PhantomData;
use std::cell::RefCell;
use std::rc::Rc;
use window::Window;
use viewport::Viewport;
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct RenderArgs {
pub ext_dt: f64,
pub width: u32,
pub height: u32,
pub draw_width: u32,
pub draw_height: u32,
}
impl RenderArgs {
pub fn viewport(&self) -> Viewport {
Viewport {
rect: [0, 0, self.draw_width as i32, self.draw_height as i32],
window_size: [self.width, self.height],
draw_size: [self.draw_width, self.draw_height],
}
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct AfterRenderArgs;
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct UpdateArgs {
pub dt: f64,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct IdleArgs {
pub dt: f64
}
pub trait EventMap<I> {
fn render(args: RenderArgs) -> Self;
fn after_render(args: AfterRenderArgs) -> Self;
fn update(args: UpdateArgs) -> Self;
fn input(args: I) -> Self;
fn idle(IdleArgs) -> Self;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Idle {
No,
Yes
}
#[derive(Copy, Clone, Debug)]
enum State {
Render,
SwapBuffers,
UpdateLoop(Idle),
HandleEvents,
Update,
}
pub struct WindowEvents<W, E>
where
W: Window,
E: EventMap<<W as Window>::Event>
{
window: Rc<RefCell<W>>,
state: State,
last_update: u64,
last_frame: u64,
dt_update_in_ns: u64,
dt_frame_in_ns: u64,
dt: f64,
swap_buffers: bool,
_marker_e: PhantomData<E>,
}
static BILLION: u64 = 1_000_000_000;
fn ns_to_ms(ns: u64) -> u32 {
(ns / 1_000_000) as u32
}
pub const DEFAULT_UPS: u64 = 120;
pub const DEFAULT_MAX_FPS: u64 = 60;
impl<W, E> WindowEvents<W, E>
where
W: Window,
E: EventMap<<W as Window>::Event>
{
pub fn new(window: Rc<RefCell<W>>) -> WindowEvents<W, E> {
let start = clock_ticks::precise_time_ns();
let updates_per_second = DEFAULT_UPS;
let max_frames_per_second = DEFAULT_MAX_FPS;
WindowEvents {
window: window,
state: State::Render,
last_update: start,
last_frame: start,
dt_update_in_ns: BILLION / updates_per_second,
dt_frame_in_ns: BILLION / max_frames_per_second,
dt: 1.0 / updates_per_second as f64,
swap_buffers: true,
_marker_e: PhantomData,
}
}
pub fn ups(mut self, frames: u64) -> Self {
self.dt_update_in_ns = BILLION / frames;
self.dt = 1.0 / frames as f64;
self
}
pub fn max_fps(mut self, frames: u64) -> Self {
self.dt_frame_in_ns = BILLION / frames;
self
}
pub fn swap_buffers(mut self, enable: bool) -> Self {
self.swap_buffers = enable;
self
}
}
impl<W, E> Iterator for WindowEvents<W, E>
where
W: Window,
E: EventMap<<W as Window>::Event>,
{
type Item = E;
fn next(&mut self) -> Option<E> {
loop {
self.state = match self.state {
State::Render => {
let window = self.window.borrow();
if window.should_close() { return None; }
let start_render = clock_ticks::precise_time_ns();
self.last_frame = start_render;
let size = window.size();
let draw_size = window.draw_size();
if size.width != 0 && size.height != 0 {
self.state = State::SwapBuffers;
return Some(EventMap::render(RenderArgs {
ext_dt: (start_render - self.last_update) as f64
/ BILLION as f64,
width: size.width,
height: size.height,
draw_width: draw_size.width,
draw_height: draw_size.height,
}));
}
State::UpdateLoop(Idle::No)
}
State::SwapBuffers => {
if self.swap_buffers {
self.window.borrow_mut().swap_buffers();
self.state = State::UpdateLoop(Idle::No);
return Some(EventMap::after_render(AfterRenderArgs));
} else {
State::UpdateLoop(Idle::No)
}
}
State::UpdateLoop(ref mut idle) => {
let current_time = clock_ticks::precise_time_ns();
let next_frame = self.last_frame + self.dt_frame_in_ns;
let next_update = self.last_update + self.dt_update_in_ns;
let next_event = cmp::min(next_frame, next_update);
if next_event > current_time {
if let Some(x) = self.window.borrow_mut().poll_event() {
*idle = Idle::No;
return Some(EventMap::input(x));
} else if *idle == Idle::No {
*idle = Idle::Yes;
let seconds = ((next_event - current_time) as f64) / (BILLION as f64);
return Some(EventMap::idle(IdleArgs { dt: seconds }))
}
sleep_ms(ns_to_ms(next_event - current_time));
State::UpdateLoop(Idle::No)
} else if next_event == next_frame {
State::Render
} else {
State::HandleEvents
}
}
State::HandleEvents => {
match self.window.borrow_mut().poll_event() {
None => State::Update,
Some(x) => { return Some(EventMap::input(x)); },
}
}
State::Update => {
self.state = State::UpdateLoop(Idle::No);
self.last_update += self.dt_update_in_ns;
return Some(EventMap::update(UpdateArgs{ dt: self.dt }));
}
};
}
}
}