From 24916ae4c7a199f59aec44ba079890c174f0cb1b Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 3 Feb 2023 18:15:46 -0500 Subject: [PATCH] rename types. still probably confusing. naming is very hard. especially hard when some of your types are just split up to appease the borrow checker. ugh. --- examples/balls_v2/src/states.rs | 2 +- examples/slimed/src/states.rs | 4 +-- examples/template_complicated/src/main.rs | 2 +- libretrogd/src/base/mod.rs | 42 ++++++++++++----------- libretrogd/src/states/mod.rs | 38 ++++++++++---------- 5 files changed, 45 insertions(+), 43 deletions(-) diff --git a/examples/balls_v2/src/states.rs b/examples/balls_v2/src/states.rs index 5066b67..3dc95df 100644 --- a/examples/balls_v2/src/states.rs +++ b/examples/balls_v2/src/states.rs @@ -76,7 +76,7 @@ impl Game { pub struct SimulationState; -impl GameState for SimulationState { +impl AppState for SimulationState { fn update(&mut self, _state: State, context: &mut Game) -> Option> { if context.context.system.keyboard.is_key_up(Scancode::S) { context.do_events(); diff --git a/examples/slimed/src/states.rs b/examples/slimed/src/states.rs index 1afca5f..aabe891 100644 --- a/examples/slimed/src/states.rs +++ b/examples/slimed/src/states.rs @@ -23,7 +23,7 @@ impl MainMenuState { } } -impl GameState for MainMenuState { +impl AppState for MainMenuState { fn update(&mut self, state: State, context: &mut Game) -> Option> { if state == State::Active { if context.core.system.keyboard.is_key_pressed(Scancode::Escape) { @@ -108,7 +108,7 @@ impl GamePlayState { } } -impl GameState for GamePlayState { +impl AppState for GamePlayState { fn update(&mut self, state: State, context: &mut Game) -> Option> { if state == State::Active { if self.in_menu { diff --git a/examples/template_complicated/src/main.rs b/examples/template_complicated/src/main.rs index 9531a5f..27dd4ab 100644 --- a/examples/template_complicated/src/main.rs +++ b/examples/template_complicated/src/main.rs @@ -97,7 +97,7 @@ impl DemoState { } } -impl GameState for DemoState { +impl AppState for DemoState { fn update(&mut self, state: State, context: &mut Game) -> Option> { if state == State::Active { if context.core.system.keyboard.is_key_pressed(Scancode::Escape) { diff --git a/libretrogd/src/base/mod.rs b/libretrogd/src/base/mod.rs index 68eb302..b15597c 100644 --- a/libretrogd/src/base/mod.rs +++ b/libretrogd/src/base/mod.rs @@ -7,12 +7,12 @@ use crate::events::*; use crate::states::*; use crate::system::*; -pub trait AppState { +pub trait PrimaryState { fn system(&self) -> &System; fn system_mut(&mut self) -> &mut System; } -pub trait AppStateWithFrameTiming: AppState { +pub trait PrimaryStateWithFrameTiming: PrimaryState { fn delta(&self) -> f32; fn set_delta(&mut self, delta: f32); @@ -25,7 +25,7 @@ pub trait AppStateWithFrameTiming: AppState { } } -pub trait AppStateWithEvents: AppState { +pub trait PrimaryStateWithEvents: PrimaryState { fn event_publisher(&mut self) -> &mut EventPublisher; } @@ -33,7 +33,7 @@ pub trait SupportSystems {} pub trait SupportSystemsWithEvents: SupportSystems where - ContextType: AppStateWithEvents, + ContextType: PrimaryStateWithEvents, { fn event_listeners(&mut self) -> &mut EventListeners; @@ -43,14 +43,14 @@ where } } -pub struct App { - pub state: StateType, - pub support: SupportType, +pub struct App { + pub primary_state: PrimaryType, + pub support_systems: SupportType, } -impl App { - pub fn new(state: StateType, support: SupportType) -> Self { - App { state, support } +impl App { + pub fn new(primary_state: PrimaryType, support_systems: SupportType) -> Self { + App { primary_state, support_systems } } } @@ -63,33 +63,35 @@ pub enum MainLoopError { SystemError(#[from] SystemError), } -pub fn main_loop( - mut app: App, +pub fn main_loop( + primary_state: PrimaryType, + support_systems: SupportType, initial_state: State, ) -> Result<(), MainLoopError> where - StateType: AppStateWithFrameTiming, + PrimaryType: PrimaryStateWithFrameTiming, SupportType: SupportSystems, - State: GameState + 'static, + 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.state.system().ticks(); + let mut last_ticks = app.primary_state.system().ticks(); while is_running && !states.is_empty() { - app.state.system_mut().do_events_with(|event| match event { + app.primary_state.system_mut().do_events_with(|event| match event { SystemEvent::Quit => { is_running = false; } _ => {} }); - last_ticks = app.state.update_frame_delta(last_ticks); - states.update(&mut app.state)?; - states.render(&mut app.state); - app.state.system_mut().display()?; + last_ticks = app.primary_state.update_frame_delta(last_ticks); + states.update(&mut app)?; + states.render(&mut app); + app.primary_state.system_mut().display()?; } Ok(()) diff --git a/libretrogd/src/states/mod.rs b/libretrogd/src/states/mod.rs index d2f01b1..0a44201 100644 --- a/libretrogd/src/states/mod.rs +++ b/libretrogd/src/states/mod.rs @@ -30,8 +30,8 @@ pub enum State { /////////////////////////////////////////////////////////////////////////////////////////////////// pub enum StateChange { - Push(Box>), - Swap(Box>), + Push(Box>), + Swap(Box>), Pop(u32), } @@ -46,7 +46,7 @@ impl std::fmt::Debug for StateChange { } } -pub trait GameState { +pub trait AppState { fn update(&mut self, state: State, context: &mut ContextType) -> Option>; fn render(&mut self, state: State, context: &mut ContextType); fn transition(&mut self, state: State, context: &mut ContextType) -> bool; @@ -60,14 +60,14 @@ pub enum StateError { #[error("Operation cannot currently be performed because there is already a pending state change.")] HasPendingStateChange, - #[error("Operation cannot currently be performed because the GameState's current state ({0:?}) does not allow it.")] - GameStateInvalidState(State), + #[error("Operation cannot currently be performed because the State's current state ({0:?}) does not allow it.")] + AppStateInvalidState(State), } struct StateContainer { current_state: State, pending_state_change: Option, - state: Box>, + state: Box>, } impl std::fmt::Debug for StateContainer { @@ -80,7 +80,7 @@ impl std::fmt::Debug for StateContainer { } impl StateContainer { - pub fn new(state: Box>) -> Self { + pub fn new(state: Box>) -> Self { StateContainer { current_state: State::Dead, pending_state_change: None, @@ -110,7 +110,7 @@ impl StateContainer { } #[inline] - pub fn state(&mut self) -> &mut dyn GameState { + pub fn state(&mut self) -> &mut dyn AppState { self.state.deref_mut() } @@ -119,7 +119,7 @@ impl StateContainer { self.change_state(State::TransitionOut(to), context); Ok(()) } else { - Err(StateError::GameStateInvalidState(self.current_state)) + Err(StateError::AppStateInvalidState(self.current_state)) } } @@ -135,7 +135,7 @@ impl StateContainer { Ok(()) }, _ => { - Err(StateError::GameStateInvalidState(self.current_state)) + Err(StateError::AppStateInvalidState(self.current_state)) } } } @@ -180,7 +180,7 @@ impl StateContainer { pub struct States { states: VecDeque>, command: Option>, - pending_state: Option>>, + pending_state: Option>>, pop_count: Option, } @@ -226,7 +226,7 @@ impl States { true } - fn push_boxed_state(&mut self, boxed_state: Box>) -> Result<(), StateError> { + fn push_boxed_state(&mut self, boxed_state: Box>) -> Result<(), StateError> { if !self.can_push_or_pop() { Err(StateError::HasPendingStateChange) } else { @@ -235,7 +235,7 @@ impl States { } } - fn swap_boxed_state(&mut self, boxed_state: Box>) -> Result<(), StateError> { + fn swap_boxed_state(&mut self, boxed_state: Box>) -> Result<(), StateError> { if !self.can_push_or_pop() { Err(StateError::HasPendingStateChange) } else { @@ -244,11 +244,11 @@ impl States { } } - pub fn push(&mut self, state: impl GameState + 'static) -> Result<(), StateError> { + pub fn push(&mut self, state: impl AppState + 'static) -> Result<(), StateError> { self.push_boxed_state(Box::new(state)) } - pub fn swap(&mut self, state: impl GameState + 'static) -> Result<(), StateError> { + pub fn swap(&mut self, state: impl AppState + 'static) -> Result<(), StateError> { self.swap_boxed_state(Box::new(state)) } @@ -382,11 +382,11 @@ impl States { match self.state_of_front_state() { Some(State::Paused) => { // should never happen now. leaving here just in case ... - return Err(StateError::GameStateInvalidState(State::Paused)); + return Err(StateError::AppStateInvalidState(State::Paused)); }, Some(State::Dead) => { // should never happen now. leaving here just in case ... - return Err(StateError::GameStateInvalidState(State::Dead)); + return Err(StateError::AppStateInvalidState(State::Dead)); }, Some(State::TransitionIn) => { let state = self.states.front_mut().unwrap(); @@ -506,7 +506,7 @@ mod tests { } } - impl GameState for TestState { + impl AppState for TestState { fn update(&mut self, state: State, context: &mut TestContext) -> Option> { context.log(LogEntry::Update(self.id, state)); None @@ -1284,7 +1284,7 @@ mod tests { } } - impl GameState for SelfPushPopState { + impl AppState for SelfPushPopState { fn update(&mut self, state: State, context: &mut TestContext) -> Option> { context.log(LogEntry::Update(self.id, state)); if state == State::Active {