summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Michelotti <michelotti.matthew@gmail.com>2018-05-05 22:47:37 -0500
committerMatthew Michelotti <michelotti.matthew@gmail.com>2018-05-05 22:47:37 -0500
commit0112623e1a3ef406b85614fcd0aad934ee881ea0 (patch)
tree186badabe32bb5428a5f46b9a020a36c99df8cc2
parentae875e37a70346922c4c7b43841378a18439453b (diff)
moved AppContext to a separate file
-rw-r--r--gate/src/app_context.rs86
-rw-r--r--gate/src/core/sdl/event_handler.rs2
-rw-r--r--gate/src/core/sdl/mod.rs4
-rw-r--r--gate/src/lib.rs74
4 files changed, 93 insertions, 73 deletions
diff --git a/gate/src/app_context.rs b/gate/src/app_context.rs
new file mode 100644
index 0000000..40c09ce
--- /dev/null
+++ b/gate/src/app_context.rs
@@ -0,0 +1,86 @@
+// Copyright 2017-2018 Matthew D. Michelotti
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use std::marker::PhantomData;
+
+use asset_id::{AppAssetId, IdU16};
+use core::CoreAudio;
+
+/// Context passed to methods in `App`.
+pub struct AppContext<A: AppAssetId> {
+ /// Audio playback.
+ pub audio: Audio<A>,
+ dims: (f64, f64),
+ cursor: (f64, f64),
+ close_requested: bool,
+}
+
+impl<A: AppAssetId> AppContext<A> {
+ pub(crate) fn new(audio: CoreAudio, dims: (f64, f64)) -> AppContext<A> {
+ AppContext {
+ audio: Audio { core: audio, phantom: PhantomData },
+ dims,
+ cursor: (0., 0.),
+ close_requested: false,
+ }
+ }
+
+ pub(crate) fn set_cursor(&mut self, cursor: (f64, f64)) {
+ self.cursor = cursor;
+ self.bound_cursor();
+ }
+
+ pub(crate) fn set_dims(&mut self, dims: (f64, f64)) {
+ self.dims = dims;
+ self.bound_cursor();
+ }
+
+ fn bound_cursor(&mut self) {
+ let (half_width, half_height) = (self.dims.0 * 0.5, self.dims.1 * 0.5);
+ self.cursor = (
+ self.cursor.0.max(-half_width).min(half_width),
+ self.cursor.1.max(-half_height).min(half_height),
+ );
+ }
+
+ /// Returns the app (width, height), which are restricted by the app height and the
+ /// aspect ratio range specified in `AppInfo`.
+ pub fn dims(&self) -> (f64, f64) { self.dims }
+
+ /// Returns the mouse cursor (x, y) position in app coordinates.
+ ///
+ /// The x coordinate lies in the range `-0.5 * self.dims().0` to `0.5 * self.dims().0`.
+ /// The y coordinate lies in the range `-0.5 * self.dims().1` to `0.5 * self.dims().1`.
+ pub fn cursor(&self) -> (f64, f64) { self.cursor }
+
+ /// 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 }
+}
+
+/// Struct for audio playback.
+pub struct Audio<A: AppAssetId> { core: CoreAudio, phantom: PhantomData<A> }
+
+impl<A: AppAssetId> Audio<A> {
+ /// Plays the given sound effect once.
+ pub fn play_sound(&mut self, sound: A::Sound) { self.core.play_sound(sound.id_u16()); }
+
+ /// Continually loops the given music, replacing the currently playing music, if any.
+ pub fn loop_music(&mut self, music: A::Music) { self.core.loop_music(music.id_u16()); }
+
+ /// Stops the currently playing music, if any.
+ pub fn stop_music(&mut self) { self.core.stop_music(); }
+}
diff --git a/gate/src/core/sdl/event_handler.rs b/gate/src/core/sdl/event_handler.rs
index 1d67720..7470477 100644
--- a/gate/src/core/sdl/event_handler.rs
+++ b/gate/src/core/sdl/event_handler.rs
@@ -74,7 +74,7 @@ impl EventHandler {
},
_ => {},
}
- if ctx.close_requested { break; }
+ if ctx.close_requested() { break; }
}
}
}
diff --git a/gate/src/core/sdl/mod.rs b/gate/src/core/sdl/mod.rs
index 7debc1c..adef424 100644
--- a/gate/src/core/sdl/mod.rs
+++ b/gate/src/core/sdl/mod.rs
@@ -101,9 +101,9 @@ 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; }
+ if ctx.close_requested() { break; }
app.advance(elapsed, &mut ctx);
- if ctx.close_requested { break; }
+ if ctx.close_requested() { break; }
}
}
diff --git a/gate/src/lib.rs b/gate/src/lib.rs
index d55a541..be80fb9 100644
--- a/gate/src/lib.rs
+++ b/gate/src/lib.rs
@@ -59,18 +59,17 @@ extern crate byteorder;
pub mod asset_id;
pub mod renderer;
pub mod app_info;
+mod app_context;
mod input;
mod core;
-pub use core::*;
+pub use core::*; // exports WASM FFI function
+pub use app_context::{AppContext, Audio};
pub use input::KeyCode;
pub use app_info::AppInfo;
-use std::marker::PhantomData;
-
-use core::CoreAudio;
-use asset_id::{AppAssetId, IdU16};
+use asset_id::AppAssetId;
use renderer::Renderer;
/// Invoke this in a `main` method to run the `App`.
@@ -96,68 +95,3 @@ pub trait App<A: AppAssetId> {
/// Render the app in its current state.
fn render(&mut self, renderer: &mut Renderer<A>, ctx: &AppContext<A>);
}
-
-/// Context passed to methods in `App`.
-pub struct AppContext<A: AppAssetId> {
- /// Audio playback.
- pub audio: Audio<A>,
- dims: (f64, f64),
- cursor: (f64, f64),
- close_requested: bool,
-}
-
-impl<A: AppAssetId> AppContext<A> {
- fn new(audio: CoreAudio, dims: (f64, f64)) -> AppContext<A> {
- AppContext {
- audio: Audio { core: audio, phantom: PhantomData },
- dims,
- cursor: (0., 0.),
- close_requested: false,
- }
- }
-
- fn set_cursor(&mut self, cursor: (f64, f64)) {
- self.cursor = cursor;
- self.bound_cursor();
- }
-
- fn set_dims(&mut self, dims: (f64, f64)) {
- self.dims = dims;
- self.bound_cursor();
- }
-
- fn bound_cursor(&mut self) {
- let (half_width, half_height) = (self.dims.0 * 0.5, self.dims.1 * 0.5);
- self.cursor = (
- self.cursor.0.max(-half_width).min(half_width),
- self.cursor.1.max(-half_height).min(half_height),
- );
- }
-
- /// Returns the app (width, height), which are restricted by the app height and the
- /// aspect ratio range specified in `AppInfo`.
- pub fn dims(&self) -> (f64, f64) { self.dims }
-
- /// Returns the mouse cursor (x, y) position in app coordinates.
- ///
- /// The x coordinate lies in the range `-0.5 * self.dims().0` to `0.5 * self.dims().0`.
- /// The y coordinate lies in the range `-0.5 * self.dims().1` to `0.5 * self.dims().1`.
- pub fn cursor(&self) -> (f64, f64) { self.cursor }
-
- /// Closes the app entirely.
- pub fn close(&mut self) { self.close_requested = true; }
-}
-
-/// Struct for audio playback.
-pub struct Audio<A: AppAssetId> { core: CoreAudio, phantom: PhantomData<A> }
-
-impl<A: AppAssetId> Audio<A> {
- /// Plays the given sound effect once.
- pub fn play_sound(&mut self, sound: A::Sound) { self.core.play_sound(sound.id_u16()); }
-
- /// Continually loops the given music, replacing the currently playing music, if any.
- pub fn loop_music(&mut self, music: A::Music) { self.core.loop_music(music.id_u16()); }
-
- /// Stops the currently playing music, if any.
- pub fn stop_music(&mut self) { self.core.stop_music(); }
-}