From db261d2356a017fc4af6255842166cb9b8596e59 Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 22 May 2022 10:50:12 -0400 Subject: [PATCH] add 'Resume' state that game states go through when they wake up again this is to provide an easy way for game states to react to the specific scenario where they were previously paused and then are re-awakened and may want to perform (re-)initialization like they may have done in a previous 'Pending' state (which is only set when states are first created) --- libretrogd/src/states/mod.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libretrogd/src/states/mod.rs b/libretrogd/src/states/mod.rs index 702188c..025ae37 100644 --- a/libretrogd/src/states/mod.rs +++ b/libretrogd/src/states/mod.rs @@ -18,6 +18,7 @@ pub enum TransitionDirection { #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum State { Pending, + Resume, Active, Paused, TransitionIn, @@ -108,7 +109,7 @@ impl StateContainer { pub fn transition_in(&mut self, context: &mut ContextType) -> Result<(), StateError> { match self.current_state { - State::Pending | State::Paused => { + State::Pending | State::Paused | State::Resume => { self.change_state(State::TransitionIn, context); Ok(()) }, @@ -312,6 +313,7 @@ impl States { // otherwise, we're probably waking up a state that was paused and needs to // be resumed since it's once again on top let state = self.states.front_mut().unwrap(); + state.change_state(State::Resume, context); state.transition_in(context)?; } }, @@ -754,7 +756,8 @@ mod tests { context.take_log(), vec![ StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)), - StateChange(FIRST, TransitionIn, Paused), + StateChange(FIRST, Resume, Paused), + StateChange(FIRST, TransitionIn, Resume), Transition(FIRST, TransitionIn), Update(FIRST, TransitionIn), Render(FIRST, TransitionIn) @@ -945,7 +948,8 @@ mod tests { context.take_log(), vec![ StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)), - StateChange(FIRST, TransitionIn, Paused), + StateChange(FIRST, Resume, Paused), + StateChange(FIRST, TransitionIn, Resume), Transition(FIRST, TransitionIn), Update(FIRST, TransitionIn), Render(FIRST, TransitionIn) @@ -1387,7 +1391,8 @@ mod tests { context.take_log(), vec![ StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)), - StateChange(FIRST, TransitionIn, Paused), + StateChange(FIRST, Resume, Paused), + StateChange(FIRST, TransitionIn, Resume), Transition(FIRST, TransitionIn), Update(FIRST, TransitionIn), Render(FIRST, TransitionIn)