From b26eab6fb037ebab7f1821a3755bf69991e3716a Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 19 Feb 2023 13:15:00 -0500 Subject: [PATCH] some cleanup and naming changes to main loop, game context boilerplate maybe sort of better? --- libretrogd/src/base/mod.rs | 47 +++++++++++++++----------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/libretrogd/src/base/mod.rs b/libretrogd/src/base/mod.rs index b15597c..8a9ac0f 100644 --- a/libretrogd/src/base/mod.rs +++ b/libretrogd/src/base/mod.rs @@ -7,12 +7,10 @@ use crate::events::*; use crate::states::*; use crate::system::*; -pub trait PrimaryState { +pub trait CoreState { fn system(&self) -> &System; fn system_mut(&mut self) -> &mut System; -} -pub trait PrimaryStateWithFrameTiming: PrimaryState { fn delta(&self) -> f32; fn set_delta(&mut self, delta: f32); @@ -25,33 +23,29 @@ pub trait PrimaryStateWithFrameTiming: PrimaryState { } } -pub trait PrimaryStateWithEvents: PrimaryState { +pub trait CoreStateWithEvents: CoreState { fn event_publisher(&mut self) -> &mut EventPublisher; } pub trait SupportSystems {} -pub trait SupportSystemsWithEvents: SupportSystems -where - ContextType: PrimaryStateWithEvents, +pub trait SupportSystemsWithEvents: SupportSystems { - fn event_listeners(&mut self) -> &mut EventListeners; + type ContextType: CoreStateWithEvents; + fn event_listeners(&mut self) -> &mut EventListeners; - fn do_events(&mut self, context: &mut ContextType) { + fn do_events(&mut self, context: &mut Self::ContextType) { self.event_listeners().take_queue_from(context.event_publisher()); self.event_listeners().dispatch_queue(context); } } -pub struct App { - pub primary_state: PrimaryType, - pub support_systems: SupportType, -} +pub trait AppContext { + type CoreType: CoreState; + type SupportType: SupportSystems; -impl App { - pub fn new(primary_state: PrimaryType, support_systems: SupportType) -> Self { - App { primary_state, support_systems } - } + fn core(&mut self) -> &mut Self::CoreType; + fn support(&mut self) -> &mut Self::SupportType; } #[derive(Error, Debug)] @@ -63,35 +57,32 @@ pub enum MainLoopError { SystemError(#[from] SystemError), } -pub fn main_loop( - primary_state: PrimaryType, - support_systems: SupportType, +pub fn main_loop( + mut app: ContextType, initial_state: State, ) -> Result<(), MainLoopError> where - PrimaryType: PrimaryStateWithFrameTiming, - SupportType: SupportSystems, - State: AppState> + 'static, + ContextType: AppContext, + State: AppState + 'static, { - let mut app = App::new(primary_state, support_systems); let mut states = States::new(); states.push(initial_state)?; let mut is_running = true; - let mut last_ticks = app.primary_state.system().ticks(); + let mut last_ticks = app.core().system().ticks(); while is_running && !states.is_empty() { - app.primary_state.system_mut().do_events_with(|event| match event { + app.core().system_mut().do_events_with(|event| match event { SystemEvent::Quit => { is_running = false; } _ => {} }); - last_ticks = app.primary_state.update_frame_delta(last_ticks); + last_ticks = app.core().update_frame_delta(last_ticks); states.update(&mut app)?; states.render(&mut app); - app.primary_state.system_mut().display()?; + app.core().system_mut().display()?; } Ok(())