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
use audio;
use audio::cpal;
use find_folder;
use glium::glutin;
use std;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::path::PathBuf;
use std::sync::{mpsc, Arc};
use std::thread;
use std::time::Duration;
use window::{self, Window};
use ui;
pub struct App {
pub(crate) events_loop: glutin::EventsLoop,
pub(crate) windows: RefCell<HashMap<window::Id, Window>>,
pub(super) exit_on_escape: Cell<bool>,
loop_mode: Cell<LoopMode>,
pub audio: Audio,
pub(crate) ui: ui::Arrangement,
}
pub struct Audio {
event_loop: Arc<cpal::EventLoop>,
process_fn_tx: RefCell<Option<mpsc::Sender<audio::stream::output::ProcessFnMsg>>>,
}
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 ASSETS_DIRECTORY_NAME: &'static str = "assets";
pub const DEFAULT_EXIT_ON_ESCAPE: bool = true;
pub(super) fn new(events_loop: glutin::EventsLoop) -> Self {
let windows = RefCell::new(HashMap::new());
let exit_on_escape = Cell::new(Self::DEFAULT_EXIT_ON_ESCAPE);
let loop_mode = Cell::new(LoopMode::default());
let cpal_event_loop = Arc::new(cpal::EventLoop::new());
let process_fn_tx = RefCell::new(None);
let audio = Audio { event_loop: cpal_event_loop, process_fn_tx };
let ui = ui::Arrangement::new();
App {
events_loop,
windows,
exit_on_escape,
loop_mode,
audio,
ui,
}
}
pub fn assets_path(&self) -> Result<PathBuf, find_folder::Error> {
let exe_path = std::env::current_exe()?;
find_folder::Search::ParentsThenKids(5, 3)
.of(exe_path.parent().expect("executable has no parent directory to search").into())
.for_folder(Self::ASSETS_DIRECTORY_NAME)
}
pub fn new_window<'a>(&'a self) -> window::Builder<'a, 'static> {
window::Builder::new(self)
}
pub fn window_count(&self) -> usize {
self.windows.borrow().len()
}
pub fn window(&self, id: window::Id) -> Option<std::cell::Ref<Window>> {
let windows = self.windows.borrow();
if !windows.contains_key(&id) {
None
} else {
Some(std::cell::Ref::map(windows, |ws| &ws[&id]))
}
}
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 }
}
pub fn new_ui(&self, window_id: window::Id) -> ui::Builder {
ui::Builder::new(self, window_id)
}
}
impl Audio {
pub fn output_devices(&self) -> audio::stream::output::Devices {
let endpoints = cpal::endpoints();
audio::stream::output::Devices { endpoints }
}
pub fn default_output_device(&self) -> Option<audio::stream::output::Device> {
cpal::default_endpoint()
.map(|endpoint| audio::stream::output::Device { endpoint })
}
pub fn new_output_stream<M, F, S>(&self, model: M, render: F)
-> audio::stream::output::Builder<M, F, S>
{
let process_fn_tx = if self.process_fn_tx.borrow().is_none() {
let event_loop = self.event_loop.clone();
let (tx, rx) = mpsc::channel();
let mut loop_context = audio::stream::output::LoopContext::new(rx);
thread::Builder::new()
.name("cpal::EventLoop::run thread".into())
.spawn(move || event_loop.run(move |v_id, out| loop_context.process(v_id, out)))
.expect("failed to spawn cpal::EventLoop::run thread");
*self.process_fn_tx.borrow_mut() = Some(tx.clone());
tx
} else {
self.process_fn_tx.borrow().as_ref().unwrap().clone()
};
audio::stream::output::Builder {
event_loop: self.event_loop.clone(),
process_fn_tx: process_fn_tx,
model,
render,
sample_rate: None,
channels: None,
frames_per_buffer: None,
device: None,
sample_format: PhantomData,
}
}
}
impl Proxy {
pub fn wakeup(&self) -> Result<(), glutin::EventsLoopClosed> {
self.events_loop_proxy.wakeup()
}
}