pub enum MainLoopEvent {
Continue,
Terminate,
}
#[cfg(target_os = "emscripten")]
use super::emscripten::*;
pub trait MainLoop {
fn main_loop(&mut self) -> MainLoopEvent;
}
pub fn run<T: 'static + MainLoop>(mut looper: T) {
#[cfg(not(target_os = "emscripten"))]
let quit_cell = std::cell::RefCell::new(false);
#[cfg(not(target_os = "emscripten"))]
let should_quit = || *quit_cell.borrow();
let quit = || {
#[cfg(not(target_os = "emscripten"))]
{
*quit_cell.borrow_mut() = true;
}
};
#[cfg(target_os = "emscripten")]
let loop_closure;
#[cfg(not(target_os = "emscripten"))]
let mut loop_closure;
loop_closure = move || match looper.main_loop() {
MainLoopEvent::Continue => MainLoopEvent::Continue,
MainLoopEvent::Terminate => {
quit();
MainLoopEvent::Terminate
}
};
#[cfg(target_os = "emscripten")]
emscripten::set_main_loop_callback(loop_closure);
#[cfg(not(target_os = "emscripten"))]
loop {
loop_closure();
if should_quit() {
break;
}
}
}