summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2020-07-27 13:31:53 +0200
committerTom Barrett <tom@tombarrett.xyz>2020-07-27 13:31:53 +0200
commit02231403df6241d08ea61600713fc1565bb22956 (patch)
treebb3ebbcdc72c40ebef55c7f03b4d501893c9c2d8 /src
parente9fb787775f95dc80084c7c382937e2bfc31dd9e (diff)
emscripten stuffHEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/emscripten.rs44
-rw-r--r--src/main.rs76
2 files changed, 89 insertions, 31 deletions
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<F>(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::<F>, 0, 1);
+ }
+
+ unsafe extern "C" fn wrapper<F>()
+ where
+ F: FnMut(),
+ {
+ MAIN_LOOP_CALLBACK.with(|z| {
+ let closure = *z.borrow_mut() as *mut F;
+ (*closure)();
+ });
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 51a6ed9..edf6f2e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,6 +10,9 @@ use sdl2::event::Event;
use sdl2::gfx::primitives::DrawRenderer;
use sdl2::pixels::Color;
+#[cfg(target_os = "emscripten")]
+pub mod emscripten;
+
fn find_origin() -> (i16, i16) {
let mut rng = rand::thread_rng();
(rng.gen_range(100, 400), rng.gen_range(100, 400))
@@ -71,8 +74,8 @@ fn main() {
let mut rng = rand::thread_rng();
lines.shuffle(&mut rng);
- let a = rng.gen_range(-5.0, 5.0);
- let b = rng.gen_range(-5.0, 5.0);
+ let a = rng.gen_range(-7.0, 7.0);
+ let b = rng.gen_range(-7.0, 7.0);
let mut pixels = Vec::new();
for i in (1000..200000).step_by(10) {
@@ -85,36 +88,47 @@ fn main() {
});
}
- let mut i = 0;
- let mut run = true;
- while run {
- for event in events.poll_iter() {
- if let Event::Quit { .. } = event {
- run = false
- };
- }
+ let mut main_loop = || {
+ let mut i = 0;
+ let mut run = true;
+ while run {
+ for event in events.poll_iter() {
+ if let Event::Quit { .. } = event {
+ run = false
+ };
+ }
- if i < lines.len() {
- let line = &lines[i];
- canvas
- .line(
- line.x1,
- line.y1,
- line.x2,
- line.y2,
- Color::RGB(255, 255, 255),
- )
- .unwrap();
- }
- if i < pixels.len() {
- let pixel = &pixels[i];
- canvas
- .pixel(pixel.x, pixel.y, Color::RGB(255, 255, 255))
- .unwrap();
+ if i < lines.len() {
+ let line = &lines[i];
+ canvas
+ .line(
+ line.x1,
+ line.y1,
+ line.x2,
+ line.y2,
+ Color::RGB(255, 255, 255),
+ )
+ .unwrap();
+ }
+ if i < pixels.len() {
+ let pixel = &pixels[i];
+ canvas
+ .pixel(pixel.x, pixel.y, Color::RGB(255, 255, 255))
+ .unwrap();
+ }
+
+ canvas.present();
+ thread::sleep(Duration::from_millis(10));
+ i += 1;
}
+ };
- canvas.present();
- thread::sleep(Duration::from_millis(10));
- i += 1;
- }
+ #[cfg(target_os = "emscripten")]
+ use emscripten::emscripten;
+
+ #[cfg(target_os = "emscripten")]
+ emscripten::set_main_loop_callback(main_loop);
+
+ #[cfg(not(target_os = "emscripten"))]
+ main_loop();
}