summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Michelotti <michelotti.matthew@gmail.com>2018-06-02 20:46:11 -0500
committerMatthew Michelotti <michelotti.matthew@gmail.com>2018-06-02 20:52:21 -0500
commit9e8dc0f6fd7374f7466427da57443537d36b9eb5 (patch)
treea3698045e6beb9bc129d98b4d9525c4fa4ae1909
parent34e30ddea960c85df165ee13beb83476ca020dff (diff)
added restart method to gate.js, so app can be resumed after closing
-rw-r--r--gate/src/app_context.rs6
-rw-r--r--gate/src/core/sdl/event_handler.rs5
-rw-r--r--gate/src/core/sdl/mod.rs8
-rw-r--r--gate/src/core/wasm/mod.rs20
-rw-r--r--gate/src/core/wasm/wasm_exports.rs13
-rw-r--r--gate_build/src/html/gate.js24
6 files changed, 41 insertions, 35 deletions
diff --git a/gate/src/app_context.rs b/gate/src/app_context.rs
index 7774d23..a2ed560 100644
--- a/gate/src/app_context.rs
+++ b/gate/src/app_context.rs
@@ -85,7 +85,11 @@ impl<A: AppAssetId> AppContext<A> {
/// Closes the app entirely.
pub fn close(&mut self) { self.close_requested = true; }
- pub(crate) fn close_requested(&self) -> bool { self.close_requested }
+ pub(crate) fn take_close_request(&mut self) -> bool {
+ let result = self.close_requested;
+ self.close_requested = false;
+ result
+ }
}
/// Struct for audio playback.
diff --git a/gate/src/core/sdl/event_handler.rs b/gate/src/core/sdl/event_handler.rs
index e3a6712..6664f24 100644
--- a/gate/src/core/sdl/event_handler.rs
+++ b/gate/src/core/sdl/event_handler.rs
@@ -37,7 +37,7 @@ impl EventHandler {
}
pub fn process_events<AS: AppAssetId, AP: App<AS>>(&mut self, app: &mut AP, ctx: &mut AppContext<AS>,
- renderer: &Renderer<AS>) {
+ renderer: &Renderer<AS>) -> bool {
for event in self.pump.poll_iter() {
match event {
Event::Quit { .. } => ctx.close(),
@@ -74,8 +74,9 @@ impl EventHandler {
},
_ => {},
}
- if ctx.close_requested() { break; }
+ if ctx.take_close_request() { return false; }
}
+ true
}
}
diff --git a/gate/src/core/sdl/mod.rs b/gate/src/core/sdl/mod.rs
index 4072096..f85d39c 100644
--- a/gate/src/core/sdl/mod.rs
+++ b/gate/src/core/sdl/mod.rs
@@ -80,7 +80,7 @@ pub fn run<AS: AppAssetId, AP: App<AS>>(info: AppInfo, mut app: AP) {
let mut clock = AppClock::new(timer, &info);
- 'main: loop {
+ loop {
unsafe {
gl::ClearColor(0., 0., 0., 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
@@ -98,10 +98,10 @@ pub fn run<AS: AppAssetId, AP: App<AS>>(info: AppInfo, mut app: AP) {
let elapsed = clock.step();
- event_handler.process_events(&mut app, &mut ctx, &renderer);
- if ctx.close_requested() { break; }
+ let continuing = event_handler.process_events(&mut app, &mut ctx, &renderer);
+ if !continuing { break; }
app.advance(elapsed.min(::MAX_TIMESTEP), &mut ctx);
- if ctx.close_requested() { break; }
+ if ctx.take_close_request() { break; }
}
}
diff --git a/gate/src/core/wasm/mod.rs b/gate/src/core/wasm/mod.rs
index df35c28..15e1430 100644
--- a/gate/src/core/wasm/mod.rs
+++ b/gate/src/core/wasm/mod.rs
@@ -20,7 +20,6 @@ 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};
@@ -31,7 +30,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, APP_CREATED};
+use super::mark_app_created_flag;
pub struct CoreAudio;
@@ -126,11 +125,12 @@ impl<AS: AppAssetId, AP: App<AS>> TraitAppRunner for AppRunner<AS, AP> {
}
self.last_time_sec = Some(time_sec);
- if !self.ctx.close_requested() {
+ let close_requested = self.ctx.take_close_request();
+ if !close_requested {
self.app.render(self.renderer.as_mut().unwrap(), &self.ctx);
self.renderer.as_mut().unwrap().flush();
}
- !self.ctx.close_requested()
+ !close_requested
}
fn update_cursor(&mut self, cursor_x: i32, cursor_y: i32) {
@@ -147,7 +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()
+ !self.ctx.take_close_request()
}
fn music_count(&self) -> u16 { AS::Music::count() }
@@ -164,13 +164,3 @@ 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 addcfb9..49557b5 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, delete_app };
+use super::{app_runner_is_defined, app_runner_borrow, app_runner_borrow_mut };
#[no_mangle]
pub unsafe extern "C" fn gateWasmInit() {
@@ -35,9 +35,6 @@ pub unsafe extern "C" fn gateWasmOnResize(w: c_int, h: 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);
let continuing = app_runner_borrow_mut().update_and_draw(time_millis / 1000.0);
- if !continuing {
- delete_app();
- }
if continuing { 1 } else { 0 }
}
@@ -46,9 +43,6 @@ 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();
let continuing = app_runner_borrow_mut().input(code, down);
- if !continuing {
- delete_app();
- }
if continuing { 1 } else { 0 }
}
@@ -64,11 +58,8 @@ pub unsafe extern "C" fn gateWasmMouseEvent(cursor_x: c_int, cursor_y: c_int, bu
let continuing = if let Some(code) = code {
app_runner_borrow_mut().input(code, down)
} else {
- false
+ true
};
- if !continuing {
- delete_app();
- }
if continuing { 1 } else { 0 }
}
diff --git a/gate_build/src/html/gate.js b/gate_build/src/html/gate.js
index 1304d80..1d1e872 100644
--- a/gate_build/src/html/gate.js
+++ b/gate_build/src/html/gate.js
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-function gate(canvas, wasmFilePath) {
+function gate(canvas, wasmFilePath, onQuit) {
const floatSize = 4;
@@ -56,6 +56,7 @@ function gate(canvas, wasmFilePath) {
var Module = {};
Module.loadingAudioCount = 0;
Module.currentlyRunning = false;
+ Module.appQuit = false;
function setSpriteAttribPointers () {
gl.vertexAttribPointer(Module.spriteProg.attribs.vert, 2, gl.FLOAT, false, 7 * floatSize, 0);
@@ -272,7 +273,7 @@ function gate(canvas, wasmFilePath) {
quitApp();
}
}
- requestAnimationFrame(updateAndDraw);
+ requestAnimationFrame(updateAndDraw); // TODO don't request animation frames after app has stopped?
}
function handleKeyEvent(codeStr, down) {
@@ -325,6 +326,25 @@ function gate(canvas, wasmFilePath) {
function quitApp() {
Module.currentlyRunning = false;
+ Module.appQuit = true;
+ if (Module.currentMusic != null) {
+ Module.currentMusic.pause();
+ }
+ if (onQuit) {
+ onQuit();
+ }
}
+ return {
+ restart: function() {
+ if (!Module.currentlyRunning && Module.appQuit) {
+ Module.currentlyRunning = true;
+ Module.appQuit = false;
+ if (Module.currentMusic != null) {
+ Module.currentMusic.play();
+ }
+ // TODO notify app of all keys that were released after the app was quit...
+ }
+ }
+ };
}