summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs12
-rw-r--r--src/state.rs15
2 files changed, 12 insertions, 15 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 71c1cf6..916a405 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -111,6 +111,7 @@ where
let gl = unsafe {
glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _)
};
+ glfw.set_swap_interval(glfw::SwapInterval::Sync(1));
(glfw, window, gl, events)
};
let glfw = std::cell::RefCell::new(rglfw);
@@ -129,8 +130,10 @@ where
G = Some(game as *mut G as *mut std::ffi::c_void);
}
+ game.initialize(ctx, st)?;
'running: loop {
if ctx.window.borrow().should_close() {
+ game.finalize(ctx, st)?;
log::info!("bye!");
break 'running;
}
@@ -182,11 +185,10 @@ where
}
#[cfg(target_arch = "wasm32")]
-pub async fn run<'a, F, G, Fut>(w: u32, h: u32, options: Options, gnew: F)
+pub fn run<'a, F, G>(w: u32, h: u32, options: Options, gnew: F)
where
- Fut: std::future::Future<Output = G>,
G: state::Game + 'static,
- F: (Fn(&'a context::Context) -> Fut),
+ F: (Fn(&'a context::Context) -> G),
{
console_log::init_with_level(log::Level::Debug).unwrap();
console_error_panic_hook::set_once();
@@ -222,7 +224,7 @@ where
let ctx = Box::leak(Box::new(context::Context::new(window, gl, w as f32, h as f32, resize)));
ctx.maximize_canvas();
- let game = Box::leak(Box::new(gnew(ctx).await));
+ let game = Box::leak(Box::new(gnew(ctx)));
let st = Box::leak(Box::new(state::State::new(&ctx)));
unsafe {
@@ -231,6 +233,7 @@ where
G = Some(game as *mut G as *mut std::ffi::c_void);
}
+ let _ = game.initialize(ctx, st);
let res = std::rc::Rc::new(std::cell::RefCell::new(Ok(())));
let result = res.clone();
event_loop.set_control_flow(winit::event_loop::ControlFlow::Wait);
@@ -318,6 +321,7 @@ where
elwt.exit();
}
});
+ let _ = game.finalize(ctx, st);
if let Err(e) = res.replace(Ok(())) {
panic!("{}", e);
}
diff --git a/src/state.rs b/src/state.rs
index 1316cf2..09f0f77 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -24,6 +24,7 @@ pub struct Response {
pub trait Game {
fn initialize(&self, ctx: &context::Context, st: &State) -> utils::Erm<()> { Ok(()) }
+ fn finalize(&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>
{
@@ -485,22 +486,14 @@ impl State {
pub fn run_update<G>(&mut self, ctx: &context::Context, game: &mut G) -> utils::Erm<()> where G: Game {
let now = now(ctx);
-
let diff = now - self.last;
- self.acc += diff;
- self.last = now;
-
// update, if enough time has accumulated since last update
- if self.acc >= DELTA_TIME {
- self.acc -= DELTA_TIME;
+ if diff >= DELTA_TIME {
+ self.last = now;
self.tick += 1;
-
game.update(ctx, self)?;
-
- // if a lot of time has elapsed (e.g. if window is unfocused and not
- // running update loop), prevent "death spiral"
- if self.acc >= DELTA_TIME { self.acc = 0.0 }
+ self.acc = 0.0;
}
Ok(())
}