Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide access to system events #90

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<T> = Box<T>;
Expand Down Expand Up @@ -200,6 +206,21 @@ impl<T: 'static + Game> GameRunner<T> {
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]
Expand Down Expand Up @@ -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
}
}
Expand Down
Loading