Skip to content

Commit

Permalink
Add cleanup method to System trait (currently only invoked for Winit)
Browse files Browse the repository at this point in the history
Co-authored-by: Ava Silver <ava@avasilver.dev>
  • Loading branch information
breqdev and ava-silver committed Dec 30, 2023
1 parent 6e3285e commit bca14bf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn PlatformProvider>;
}

/// A platform which can be run synchronously.
pub trait SyncPlatform: Platform {
/// Run the given system within this platform.
fn run(&mut self, system: Box<dyn System>);
}

/// 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<dyn System>);
}

Expand Down
7 changes: 6 additions & 1 deletion src/platform/winit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
_ => (),
},
_ => (),
Expand Down
5 changes: 5 additions & 0 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
2 changes: 1 addition & 1 deletion src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Noentiendo {

let interval_id = {
let system = system.clone();
let handler: Box<dyn FnMut() -> ()> = Box::new(move || {
let handler: Box<dyn FnMut()> = Box::new(move || {
if platform_ready.get() {
let platform = platform.clone();
let system = system.clone();
Expand Down

0 comments on commit bca14bf

Please sign in to comment.