diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 454c649..4ed73c9 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -23,18 +23,23 @@ pub use self::winit::{WinitPlatform, WinitPlatformProvider}; /// It handles starting and ticking the system, and provides a PlatformProvider /// to the system for screen/keyboard/etc. access. pub trait Platform { + /// Return a reference to a provider for systems to interact with this platform. fn provider(&self) -> Arc; } /// A platform which can be run synchronously. pub trait SyncPlatform: Platform { + /// Run the given system within this platform. fn run(&mut self, system: Box); } /// A platform which can be run asynchronously. #[async_trait(?Send)] pub trait AsyncPlatform: Platform { + /// Set up this platform for use. async fn setup(&mut self); + + /// Execute one "step" of this platform. A step is implementation-defined. async fn tick(&mut self, system: &mut Box); } diff --git a/src/platform/winit/mod.rs b/src/platform/winit/mod.rs index 4ee4f0d..021c053 100644 --- a/src/platform/winit/mod.rs +++ b/src/platform/winit/mod.rs @@ -183,7 +183,12 @@ impl SyncPlatform for WinitPlatform { key_state.lock().unwrap().release(key); } }, - WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, + WindowEvent::CloseRequested => { + if let Err(msg) = system.cleanup() { + println!("Error during cleanup: {}", msg); + } + *control_flow = ControlFlow::Exit; + } _ => (), }, _ => (), diff --git a/src/systems/mod.rs b/src/systems/mod.rs index fe9c605..79b2f61 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -35,4 +35,9 @@ pub trait System { /// Render the current state of the system to the given framebuffer. fn render(&mut self, framebuffer: &mut [u8], window: WindowConfig); + + /// Clean up any resources used by this system. + fn cleanup(&mut self) -> Result<(), &str> { + Ok(()) + } } diff --git a/src/wasm.rs b/src/wasm.rs index 18326a1..cf6ef38 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -141,7 +141,7 @@ impl Noentiendo { let interval_id = { let system = system.clone(); - let handler: Box ()> = Box::new(move || { + let handler: Box = Box::new(move || { if platform_ready.get() { let platform = platform.clone(); let system = system.clone();