diff options
| -rw-r--r-- | examples/fox.rs | 8 | ||||
| -rw-r--r-- | src/lib.rs | 3 | ||||
| -rw-r--r-- | src/state.rs | 15 | ||||
| -rw-r--r-- | src/utils.rs | 2 |
4 files changed, 19 insertions, 9 deletions
diff --git a/examples/fox.rs b/examples/fox.rs index 33df561..bfa97f0 100644 --- a/examples/fox.rs +++ b/examples/fox.rs @@ -26,16 +26,16 @@ impl TestGame { } impl state::Game for TestGame { - fn update(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { + fn update(&mut self, ctx: &context::Context, st: &mut state::State) -> Erm<()> { st.move_camera( ctx, &glam::Vec3::new(0.0, 0.0, -1.0), &glam::Vec3::new(0.0, 0.0, 1.0), &glam::Vec3::new(0.0, 1.0, 0.0), ); - Some(()) + Ok(()) } - fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { + fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Erm<()> { // if let Some(n) = self.fox.nodes_by_name.get("J_Bip_C_Neck").and_then(|i| self.fox.nodes.get_mut(*i)) { // n.transform *= glam::Mat4::from_rotation_z(0.05); // } @@ -62,7 +62,7 @@ impl state::Game for TestGame { glam::Vec3::new(0.0, 1.0, 0.0), ], ); - Some(()) + Ok(()) } } @@ -13,6 +13,9 @@ pub mod audio; pub mod net; pub mod save; +pub use utils::Erm; +pub use anyhow::Context as ErmContext; + #[cfg(target_arch = "wasm32")] use winit::platform::web::EventLoopExtWebSys; diff --git a/src/state.rs b/src/state.rs index 4d23af6..7b2de9e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -4,7 +4,7 @@ use bimap::BiHashMap; use enum_map::{enum_map, Enum, EnumMap}; use serde::{Serialize, Deserialize}; -use crate::{context, framebuffer, shader, audio}; +use crate::{audio, context, framebuffer, shader, utils}; const DELTA_TIME: f64 = 1.0 / 60.0; @@ -23,6 +23,7 @@ pub struct Response { } pub trait Game { + fn initialize(&self, ctx: &context::Context, st: &State) -> utils::Erm<()> { Ok(()) } fn initialize_audio(&self, ctx: &context::Context, st: &State, actx: &audio::Context) -> HashMap<String, audio::Audio> { @@ -32,8 +33,8 @@ pub trait Game { fn mouse_move(&mut self, ctx: &context::Context, st: &mut State, x: i32, y: i32) {} fn mouse_press(&mut self, ctx: &context::Context, st: &mut State) {} fn request_return(&mut self, ctx: &context::Context, st: &mut State, res: Response) {} - fn update(&mut self, ctx: &context::Context, st: &mut State) -> Option<()> { Some(()) } - fn render(&mut self, ctx: &context::Context, st: &mut State) -> Option<()> { Some(()) } + fn update(&mut self, ctx: &context::Context, st: &mut State) -> utils::Erm<()> { Ok(()) } + fn render(&mut self, ctx: &context::Context, st: &mut State) -> utils::Erm<()> { Ok(()) } } #[derive(Debug, Enum, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -494,7 +495,9 @@ impl State { if self.acc >= DELTA_TIME { self.acc -= DELTA_TIME; self.tick += 1; - game.update(ctx, self); + + // TODO: handle failure here in a nicer way + game.update(ctx, self).expect("game update failed"); // if a lot of time has elapsed (e.g. if window is unfocused and not // running update loop), prevent "death spiral" @@ -504,7 +507,9 @@ impl State { pub fn run_render<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game { self.render_framebuffer.bind(&ctx); - game.render(ctx, self); + + // TODO: handle failure here in a nicer way + game.render(ctx, self).expect("game render failed"); self.screen.bind(&ctx); ctx.clear_color(glam::Vec4::new(0.0, 0.0, 0.0, 0.0)); diff --git a/src/utils.rs b/src/utils.rs index 8c73d37..f5ce271 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,7 @@ use serde::{Serialize, Deserialize}; +pub type Erm<T> = anyhow::Result<T>; + #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub enum Cardinal { North, |
