diff --git a/api/system/Cargo.toml b/api/system/Cargo.toml index 66d5fe82..988600d5 100644 --- a/api/system/Cargo.toml +++ b/api/system/Cargo.toml @@ -50,6 +50,11 @@ required-features = ["sys/entry-point", "sys/lang-items"] name = "set-serial-message-callback" crate-type = ["dylib", "staticlib"] path = "examples/set-serial-message-callback.rs" + +[[example]] +name = "update-state-in-serial-message-callback" +crate-type = ["dylib", "staticlib"] +path = "examples/update-state-in-serial-message-callback.rs" required-features = ["sys/entry-point", "sys/lang-items"] [package.metadata.playdate] diff --git a/api/system/examples/README.md b/api/system/examples/README.md index 0758d4a7..8fb74cd8 100644 --- a/api/system/examples/README.md +++ b/api/system/examples/README.md @@ -11,6 +11,8 @@ cargo playdate run -p=playdate-system --example=handler-boxed --features=sys/lan cargo playdate run -p=playdate-system --example=handler-pinned --features=sys/lang-items,sys/entry-point cargo playdate run -p=playdate-system --example=set-serial-message-callback --features=sys/lang-items,sys/entry-point + +cargo playdate run -p=playdate-system --example=update-state-in-serial-message-callback --features=sys/lang-items,sys/entry-point ``` More information how to use [cargo-playdate][] in help: `cargo playdate --help`. diff --git a/api/system/examples/update-state-in-serial-message-callback.rs b/api/system/examples/update-state-in-serial-message-callback.rs new file mode 100644 index 00000000..d492d691 --- /dev/null +++ b/api/system/examples/update-state-in-serial-message-callback.rs @@ -0,0 +1,65 @@ +#![no_std] +extern crate alloc; + +#[macro_use] +extern crate sys; +extern crate playdate_system as system; + +use alloc::string::String; +use core::ptr::NonNull; + +use sys::EventLoopCtrl; +use sys::ffi::*; +use system::System; +use system::event::SystemEventExt as _; +use system::prelude::*; + +struct State { + latest_message: Option, +} + +impl State { + fn new() -> Self { Self { latest_message: None } } + + /// System event handler + fn event(&'static mut self, event: SystemEvent) -> EventLoopCtrl { + match event { + SystemEvent::Init => { + System::Default().set_serial_message_callback(Some(|msg| { + self.latest_message = Some(msg); + })); + + self.set_update_handler(); + + println!("Game init complete"); + }, + _ => {}, + } + EventLoopCtrl::Continue + } +} + +impl Update for State { + fn update(&mut self) -> UpdateCtrl { + if let Some(latest_message) = self.latest_message.clone() { + println!("Latest message: {:#?}", latest_message); + } + UpdateCtrl::Continue + } +} + +/// Entry point +#[no_mangle] +pub fn event_handler(_api: NonNull, event: SystemEvent, _sim_key_code: u32) -> EventLoopCtrl { + pub static mut STATE: Option = None; + + if unsafe { STATE.is_none() } { + let state = State::new(); + unsafe { STATE = Some(state) } + } + + unsafe { STATE.as_mut().expect("impossible").event(event) } +} + +// Needed for debug build +ll_symbols!();