mirror of
https://github.com/rust-mobile/android-activity.git
synced 2026-07-19 04:58:55 +00:00
A few minor simplifications of the *-mainloop examples
This commit is contained in:
@@ -1,21 +1,10 @@
|
||||
|
||||
use game_activity::{PollEvent, MainEvent};
|
||||
use log::Level;
|
||||
use log::trace;
|
||||
use std::time::Duration;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct AppState {
|
||||
uri: String,
|
||||
}
|
||||
use log::info;
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn android_main() {
|
||||
|
||||
android_logger::init_once(
|
||||
android_logger::Config::default().with_min_level(Level::Trace)
|
||||
android_logger::Config::default().with_min_level(log::Level::Info)
|
||||
);
|
||||
|
||||
let mut quit = false;
|
||||
@@ -24,25 +13,26 @@ extern "C" fn android_main() {
|
||||
|
||||
let app = game_activity::android_app();
|
||||
while !quit {
|
||||
app.poll_events(Some(Duration::from_millis(500)) /* timeout */, |event| {
|
||||
app.poll_events(Some(std::time::Duration::from_millis(500)) /* timeout */, |event| {
|
||||
match event {
|
||||
PollEvent::Wake => { trace!("Early wake up"); },
|
||||
PollEvent::Wake => { info!("Early wake up"); },
|
||||
PollEvent::Timeout => {
|
||||
trace!("Timed out");
|
||||
info!("Timed out");
|
||||
// Real app would probably rely on vblank sync via graphics API...
|
||||
redraw_pending = true;
|
||||
},
|
||||
PollEvent::Main(main_event) => {
|
||||
trace!("Main event: {:?}", main_event);
|
||||
info!("Main event: {:?}", main_event);
|
||||
match main_event {
|
||||
MainEvent::SaveState { saver, .. } => {
|
||||
let state = serde_json::to_vec(&AppState { uri: format!("foo://bar") }).unwrap();
|
||||
saver.store(&state);
|
||||
saver.store("foo://bar".as_bytes());
|
||||
},
|
||||
MainEvent::Pause => {},
|
||||
MainEvent::Resume { loader, .. } => {
|
||||
if let Some(state) = loader.load() {
|
||||
let _state: AppState = serde_json::from_slice(&state).unwrap();
|
||||
if let Ok(uri) = String::from_utf8(state) {
|
||||
info!("Resumed with saved state = {uri:#?}");
|
||||
}
|
||||
}
|
||||
},
|
||||
MainEvent::InitWindow { .. } => {
|
||||
@@ -69,12 +59,13 @@ extern "C" fn android_main() {
|
||||
|
||||
// Handle input
|
||||
app.input_events(|event| {
|
||||
trace!("Input Event: {event:?}")
|
||||
})
|
||||
info!("Input Event: {event:?}");
|
||||
|
||||
// Render...
|
||||
});
|
||||
|
||||
info!("Render...");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user