diff options
author | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-06-02 16:27:02 -0500 |
---|---|---|
committer | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-06-02 16:27:02 -0500 |
commit | 57f590920cdbe00e61f234fd4b50ca0f956fc45a (patch) | |
tree | f03f43ad434ccf697c940e3d3cefbac5d066648a | |
parent | 78e2ef70ba2213ea7e4ac407f99a14e52d15af2f (diff) |
sending App-quit message through to javascript
-rw-r--r-- | gate/src/app_context.rs | 1 | ||||
-rw-r--r-- | gate/src/core/wasm/mod.rs | 29 | ||||
-rw-r--r-- | gate/src/core/wasm/wasm_exports.rs | 30 | ||||
-rw-r--r-- | gate_build/src/html/gate.js | 19 |
4 files changed, 60 insertions, 19 deletions
diff --git a/gate/src/app_context.rs b/gate/src/app_context.rs index be70b5b..7774d23 100644 --- a/gate/src/app_context.rs +++ b/gate/src/app_context.rs @@ -85,7 +85,6 @@ impl<A: AppAssetId> AppContext<A> { /// Closes the app entirely. pub fn close(&mut self) { self.close_requested = true; } - #[cfg(not(target_arch = "wasm32"))] pub(crate) fn close_requested(&self) -> bool { self.close_requested } } diff --git a/gate/src/core/wasm/mod.rs b/gate/src/core/wasm/mod.rs index 541f920..df35c28 100644 --- a/gate/src/core/wasm/mod.rs +++ b/gate/src/core/wasm/mod.rs @@ -20,6 +20,7 @@ use std::cell::{self, RefCell}; use std::mem; use std::io::Cursor; use std::os::raw::c_int; +use std::sync::atomic::Ordering; use ::{App, AppContext}; use asset_id::{AppAssetId, IdU16}; @@ -30,7 +31,7 @@ use renderer::atlas::Atlas; use renderer::render_buffer::RenderBuffer; use renderer::core_renderer::CoreRenderer; use self::wasm_imports::*; -use super::mark_app_created_flag; +use super::{mark_app_created_flag, APP_CREATED}; pub struct CoreAudio; @@ -55,9 +56,9 @@ impl CoreAudio { trait TraitAppRunner { fn init(&mut self); fn resize(&mut self, dims: (u32, u32)); - fn update_and_draw(&mut self, time_sec: f64); + fn update_and_draw(&mut self, time_sec: f64) -> bool; fn update_cursor(&mut self, cursor_x: i32, cursor_y: i32); - fn input(&mut self, key: KeyCode, down: bool); + fn input(&mut self, key: KeyCode, down: bool) -> bool; fn music_count(&self) -> u16; fn sound_count(&self) -> u16; } @@ -118,22 +119,25 @@ impl<AS: AppAssetId, AP: App<AS>> TraitAppRunner for AppRunner<AS, AP> { self.ctx.set_dims(renderer.app_dims(), renderer.native_px()); } - fn update_and_draw(&mut self, time_sec: f64) { + fn update_and_draw(&mut self, time_sec: f64) -> bool { let elapsed = self.last_time_sec.map(|x| time_sec - x).unwrap_or(0.0).max(0.0).min(0.1); if elapsed > 0.0 { self.app.advance(elapsed.min(::MAX_TIMESTEP), &mut self.ctx); } self.last_time_sec = Some(time_sec); - self.app.render(self.renderer.as_mut().unwrap(), &self.ctx); - self.renderer.as_mut().unwrap().flush(); + if !self.ctx.close_requested() { + self.app.render(self.renderer.as_mut().unwrap(), &self.ctx); + self.renderer.as_mut().unwrap().flush(); + } + !self.ctx.close_requested() } fn update_cursor(&mut self, cursor_x: i32, cursor_y: i32) { self.ctx.set_cursor(self.renderer.as_ref().unwrap().to_app_pos(cursor_x, cursor_y)); } - fn input(&mut self, key: KeyCode, down: bool) { + fn input(&mut self, key: KeyCode, down: bool) -> bool { if down { if self.held_keys.insert(key) { self.app.key_down(key, &mut self.ctx); @@ -143,6 +147,7 @@ impl<AS: AppAssetId, AP: App<AS>> TraitAppRunner for AppRunner<AS, AP> { self.app.key_up(key, &mut self.ctx); } } + !self.ctx.close_requested() } fn music_count(&self) -> u16 { AS::Music::count() } @@ -159,3 +164,13 @@ pub fn run<AS: 'static + AppAssetId, AP: 'static + App<AS>>(info: AppInfo, app: held_keys: HashSet::new(), })); } + +fn delete_app() { + *APP_RUNNER.r.borrow_mut() = None; + unmark_app_created_flag(); +} + +fn unmark_app_created_flag() { + let previously_created = APP_CREATED.swap(false, Ordering::Relaxed); + assert!(previously_created); +} diff --git a/gate/src/core/wasm/wasm_exports.rs b/gate/src/core/wasm/wasm_exports.rs index 4d9ee22..addcfb9 100644 --- a/gate/src/core/wasm/wasm_exports.rs +++ b/gate/src/core/wasm/wasm_exports.rs @@ -19,7 +19,7 @@ use std::os::raw::{c_int, c_char}; use ::input::KeyCode; use ::renderer::shaders; -use super::{app_runner_is_defined, app_runner_borrow, app_runner_borrow_mut}; +use super::{app_runner_is_defined, app_runner_borrow, app_runner_borrow_mut, delete_app }; #[no_mangle] pub unsafe extern "C" fn gateWasmInit() { @@ -32,20 +32,28 @@ pub unsafe extern "C" fn gateWasmOnResize(w: c_int, h: c_int) { } #[no_mangle] -pub unsafe extern "C" fn gateWasmUpdateAndDraw(time_millis: f64, cursor_x: c_int, cursor_y: c_int) { +pub unsafe extern "C" fn gateWasmUpdateAndDraw(time_millis: f64, cursor_x: c_int, cursor_y: c_int) -> c_int { app_runner_borrow_mut().update_cursor(cursor_x as i32, cursor_y as i32); - app_runner_borrow_mut().update_and_draw(time_millis / 1000.0); + let continuing = app_runner_borrow_mut().update_and_draw(time_millis / 1000.0); + if !continuing { + delete_app(); + } + if continuing { 1 } else { 0 } } #[no_mangle] -pub unsafe extern "C" fn gateWasmKeyEvent(code: c_int, down: bool) { +pub unsafe extern "C" fn gateWasmKeyEvent(code: c_int, down: bool) -> c_int { assert!(code >= 0 && code <= 255); let code = KeyCode::from_u8(code as u8).unwrap(); - app_runner_borrow_mut().input(code, down); + let continuing = app_runner_borrow_mut().input(code, down); + if !continuing { + delete_app(); + } + if continuing { 1 } else { 0 } } #[no_mangle] -pub unsafe extern "C" fn gateWasmMouseEvent(cursor_x: c_int, cursor_y: c_int, button: c_int, down: bool) { +pub unsafe extern "C" fn gateWasmMouseEvent(cursor_x: c_int, cursor_y: c_int, button: c_int, down: bool) -> c_int { app_runner_borrow_mut().update_cursor(cursor_x as i32, cursor_y as i32); let code = match button { 0 => Some(KeyCode::MouseLeft), @@ -53,9 +61,15 @@ pub unsafe extern "C" fn gateWasmMouseEvent(cursor_x: c_int, cursor_y: c_int, bu 2 => Some(KeyCode::MouseRight), _ => None, }; - if let Some(code) = code { - app_runner_borrow_mut().input(code, down); + let continuing = if let Some(code) = code { + app_runner_borrow_mut().input(code, down) + } else { + false + }; + if !continuing { + delete_app(); } + if continuing { 1 } else { 0 } } #[no_mangle] diff --git a/gate_build/src/html/gate.js b/gate_build/src/html/gate.js index 6c0ed43..489fe4f 100644 --- a/gate_build/src/html/gate.js +++ b/gate_build/src/html/gate.js @@ -266,14 +266,20 @@ function tryStart2 () { function updateAndDraw(now) { resizeCanvas(); - Module.gateWasmUpdateAndDraw(now, cursorPos.x, cursorPos.y); + const continuing = Module.gateWasmUpdateAndDraw(now, cursorPos.x, cursorPos.y); + if (!continuing) { + quitApp(); + } requestAnimationFrame(updateAndDraw); } function handleKeyEvent(codeStr, down) { const code = keycodes[codeStr]; if (code != undefined) { - Module.gateWasmKeyEvent(code, down); + const continuing = Module.gateWasmKeyEvent(code, down); + if (!continuing) { + quitApp(); + } } } @@ -285,7 +291,10 @@ function handleMouseMotion(evt) { function handleMouseEvent(evt, down) { cursorPos.x = evt.clientX; cursorPos.y = evt.clientY; - Module.gateWasmMouseEvent(cursorPos.x, cursorPos.y, evt.button, down) + const continuing = Module.gateWasmMouseEvent(cursorPos.x, cursorPos.y, evt.button, down) + if (!continuing) { + quitApp(); + } } function resizeCanvas() { @@ -305,3 +314,7 @@ function readCStr(ptr) { for (endPtr = ptr; memory[endPtr] !== 0; endPtr++); return new TextDecoder("UTF-8").decode(memory.subarray(ptr, endPtr)); } + +function quitApp() { + alert("FIXME implement quitting App"); +} |