From f1566c2008f75e649474b622918eb4dd297498cd Mon Sep 17 00:00:00 2001 From: Javier Alvarez Date: Mon, 6 May 2024 20:33:50 +0200 Subject: [PATCH] Provide access to system events --- src/lib.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 729ee7e..3747cef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,9 @@ use { alloc::boxed::Box, anyhow::Error, core::{fmt, panic::PanicInfo}, - crankstart_sys::{playdate_sprite, LCDRect, LCDSprite, SpriteCollisionResponseType}, + crankstart_sys::{ + playdate_sprite, LCDRect, LCDSprite, PDSystemEvent, SpriteCollisionResponseType, + }, }; pub struct Playdate { @@ -125,6 +127,10 @@ pub trait Game { fn draw_and_update_sprites(&self) -> bool { true } + + fn handle_event(&mut self, playdate: &mut Playdate, event: PDSystemEvent) -> Result<(), Error> { + Ok(()) + } } pub type GamePtr = Box; @@ -200,6 +206,21 @@ impl GameRunner { pub fn playdate_sprite(&self) -> *const playdate_sprite { SpriteManager::get_mut().playdate_sprite } + + pub fn handle_event(&mut self, event: PDSystemEvent) { + if self.init_failed { + return; + } + + if let Some(game) = self.game.as_mut() { + if let Err(err) = game.handle_event(&mut self.playdate, event) { + log_to_console!("Error in handle_event: {err:#}") + } + } else { + log_to_console!("can't deliver event to game"); + self.init_failed = true; + } + } } #[macro_export] @@ -274,6 +295,11 @@ macro_rules! crankstart_game { GAME_RUNNER = Some(GameRunner::new(game, playdate)); } } + + if event != PDSystemEvent::kEventInit && event != PDSystemEvent::kEventInitLua { + let game_runner = unsafe { GAME_RUNNER.as_mut().expect("GAME_RUNNER") }; + game_runner.handle_event(event); + } 0 } }