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
use glium::{self, glutin};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::time::Duration;
use window;
pub struct App {
pub(super) events_loop: glutin::EventsLoop,
pub(super) displays: RefCell<HashMap<window::Id, glium::Display>>,
pub(super) exit_on_escape: Cell<bool>,
loop_mode: Cell<LoopMode>,
}
pub struct Proxy {
events_loop_proxy: glutin::EventsLoopProxy,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LoopMode {
Rate {
update_interval: Duration,
},
Wait {
updates_following_event: usize,
update_interval: Duration,
},
}
fn update_interval(fps: f64) -> Duration {
assert!(fps > 0.0);
const NANOSEC_PER_SEC: f64 = 1_000_000_000.0;
let interval_nanosecs = NANOSEC_PER_SEC / fps;
let secs = (interval_nanosecs / NANOSEC_PER_SEC) as u64;
let nanosecs = (interval_nanosecs % NANOSEC_PER_SEC) as u32;
Duration::new(secs, nanosecs)
}
impl LoopMode {
pub const DEFAULT_RATE_FPS: f64 = 60.0;
pub const DEFAULT_UPDATES_FOLLOWING_EVENT: usize = 3;
pub fn rate_fps(fps: f64) -> Self {
let update_interval = update_interval(fps);
LoopMode::Rate { update_interval }
}
pub fn wait(updates_following_event: usize) -> Self {
let update_interval = update_interval(Self::DEFAULT_RATE_FPS);
LoopMode::Wait {
updates_following_event,
update_interval,
}
}
pub fn wait_with_max_fps(updates_following_event: usize, max_fps: f64) -> Self {
let update_interval = update_interval(max_fps);
LoopMode::Wait {
updates_following_event,
update_interval,
}
}
pub fn wait_with_interval(updates_following_event: usize, update_interval: Duration) -> Self {
LoopMode::Wait {
updates_following_event,
update_interval,
}
}
}
impl Default for LoopMode {
fn default() -> Self {
LoopMode::rate_fps(Self::DEFAULT_RATE_FPS)
}
}
impl App {
pub const DEFAULT_EXIT_ON_ESCAPE: bool = true;
pub(super) fn new(events_loop: glutin::EventsLoop) -> Self {
let displays = RefCell::new(HashMap::new());
let exit_on_escape = Cell::new(Self::DEFAULT_EXIT_ON_ESCAPE);
let loop_mode = Cell::new(LoopMode::default());
App {
events_loop,
displays,
exit_on_escape,
loop_mode,
}
}
pub fn new_window<'a>(&'a self) -> window::Builder<'a, 'static> {
window::Builder::new(self)
}
pub fn window_count(&self) -> usize {
self.displays.borrow().len()
}
pub fn exit_on_escape(&self) -> bool {
self.exit_on_escape.get()
}
pub fn set_exit_on_escape(&self, b: bool) {
self.exit_on_escape.set(b);
}
pub fn loop_mode(&self) -> LoopMode {
self.loop_mode.get()
}
pub fn set_loop_mode(&self, mode: LoopMode) {
self.loop_mode.set(mode);
}
pub fn create_proxy(&self) -> Proxy {
let events_loop_proxy = self.events_loop.create_proxy();
Proxy { events_loop_proxy }
}
}
impl Proxy {
pub fn wakeup(&self) -> Result<(), glutin::EventsLoopClosed> {
self.events_loop_proxy.wakeup()
}
}