From 02231403df6241d08ea61600713fc1565bb22956 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Mon, 27 Jul 2020 13:31:53 +0200 Subject: emscripten stuff --- src/emscripten.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/emscripten.rs (limited to 'src/emscripten.rs') diff --git a/src/emscripten.rs b/src/emscripten.rs new file mode 100644 index 0000000..3b9b12c --- /dev/null +++ b/src/emscripten.rs @@ -0,0 +1,44 @@ +#[cfg(target_os = "emscripten")] +pub mod emscripten { + use std::cell::RefCell; + use std::os::raw::{c_float, c_int, c_void}; + use std::ptr::null_mut; + + #[allow(non_camel_case_types)] + type em_callback_func = unsafe extern "C" fn(); + + extern "C" { + pub fn emscripten_set_main_loop( + func: em_callback_func, + fps: c_int, + simulate_infinite_loop: c_int, + ); + pub fn emscripten_cancel_main_loop(); + pub fn emscripten_get_now() -> c_float; + } + + thread_local!(static MAIN_LOOP_CALLBACK: RefCell<*mut c_void> = RefCell::new(null_mut())); + + pub fn set_main_loop_callback(callback: F) + where + F: FnMut(), + { + MAIN_LOOP_CALLBACK.with(|log| { + *log.borrow_mut() = &callback as *const _ as *mut c_void; + }); + + unsafe { + emscripten_set_main_loop(wrapper::, 0, 1); + } + + unsafe extern "C" fn wrapper() + where + F: FnMut(), + { + MAIN_LOOP_CALLBACK.with(|z| { + let closure = *z.borrow_mut() as *mut F; + (*closure)(); + }); + } + } +} -- cgit v1.2.3