From 8412cbab7260eb50e1dfcdff64d24e9567e63b60 Mon Sep 17 00:00:00 2001 From: James Turk Date: Wed, 10 Aug 2005 21:22:33 +0000 Subject: [PATCH] State stack system --- CHANGELOG.txt | 9 ++++-- include/Application.hpp | 62 +++++++++++++++++++++++++++++++++++------ include/State.hpp | 21 ++++++++++++-- include/entrypoint.hpp | 4 +-- src/Application.cpp | 17 ++++++++++- test/Audio_test.cpp | 4 +-- test/Font_test.cpp | 4 +-- test/Image_test.cpp | 4 +-- test/Input_test.cpp | 4 +-- test/Pen_test.cpp | 4 +-- test/Texture_test.cpp | 4 +-- 11 files changed, 109 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 95480de..51c65fd 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,13 +1,18 @@ Changelog for Photon -$Id: CHANGELOG.txt,v 1.5 2005/08/09 07:30:14 cozman Exp $ +$Id: CHANGELOG.txt,v 1.6 2005/08/10 21:22:33 cozman Exp $ + : New Features - : Feature Removal -* : Minor Changes/Bugfixes ! : Major Changes +* : Minor Changes/Bugfixes + 0.0.2 + Code::Blocks and Dev-C++ support + + Addition of a State stack allowing for applications to fall back to the + previous state when done with a particular state. + * Fixed X11 fullscreen mode + * Removed ALUT dependencies by adding custom WAV loading code 0.0.1 - Released 2005-08-08 + State-based design that allows easy creation of applications by simply diff --git a/include/Application.hpp b/include/Application.hpp index 9365dfb..11d8eff 100644 --- a/include/Application.hpp +++ b/include/Application.hpp @@ -5,13 +5,13 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Application.hpp,v 1.16 2005/08/08 21:39:41 cozman Exp $ +// $Id: Application.hpp,v 1.17 2005/08/10 21:22:33 cozman Exp $ #ifndef PHOTON_APPLICATION_HPP #define PHOTON_APPLICATION_HPP #include -#include +#include #include #include @@ -297,13 +297,25 @@ public: // Group: State Management public: - // Function: setCurrentState - // Set the current Application . + // Function: setState + // Set the current Application , removing all other . // // Template Parameters: - // StateT - Class derived from to register. + // StateT - Class derived from to set as current. template - void setCurrentState(); + void setState(); + + // Function: pushState + // Push a new , does not remove old . + // + // Template Parameters: + // StateT - Class derived from to push. + template + void pushState(); + + // Function: popState + // Pop the current , returning to the prior on the stack. + void popState(); #ifdef PHOTON_USE_OPENAL // Group: AudioCore @@ -415,6 +427,9 @@ private: // input system variables static std::vector listeners_; static std::vector pressedKeys_; + + // state system + std::stack stateStack_; // Cores #ifdef PHOTON_USE_OPENAL @@ -423,11 +438,42 @@ private: }; template -void Application::setCurrentState() +void Application::setState() { - stateRender_->state_ = stateUpdate_->state_ = StatePtr(new StateT); + StatePtr newState(new StateT); + + // clear stack + while(!stateStack_.empty()) + { + // pop then resume + stateStack_.pop(); + if(!stateStack_.empty()) + { + stateStack_.top()->onResume(); + } + } + stateStack_.push(newState); // make newState the only state on stack + + stateRender_->state_ = stateUpdate_->state_ = newState; } +template +void Application::pushState() +{ + StatePtr newState(new StateT); + + // if a state is currently running, pause it + if(!stateStack_.empty()) + { + stateStack_.top()->onPause(); + } + + stateStack_.push(newState); // push newState on top of stack + + stateRender_->state_ = stateUpdate_->state_ = newState; +} + + } #endif //PHOTON_APPLICATION_HPP diff --git a/include/State.hpp b/include/State.hpp index 2919d48..12c8798 100644 --- a/include/State.hpp +++ b/include/State.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: State.hpp,v 1.1 2005/08/08 19:19:25 cozman Exp $ +// $Id: State.hpp,v 1.2 2005/08/10 21:22:33 cozman Exp $ #ifndef PHOTON_STATE_HPP #define PHOTON_STATE_HPP @@ -21,7 +21,7 @@ // Implement as many or as few of the members of State as needed (the only // necessary member being ) and make the state as current via -// . Once a state is made current it's +// . Once a state is made current it's // update and render methods will be called every frame until either a new // state is made current or the application ends. class State @@ -30,7 +30,7 @@ class State public: // Function: State // A State's constructor is called whenever the state is made active via - // . + // . State() { }; // Function: ~State @@ -51,6 +51,21 @@ public: // screen while the State is active should be drawn in render. Game logic // inside of render should be kept to a minimum for optimum performance. virtual void render()=0; + + // Function: onPause + // If a state is executing and a new state is pushed onto the stack via + // the state will be paused until a time that it + // is either unpaused or popped from the stack itself. When it is paused + // the state management system will call onPause which may perform any + // necessary work before the state goes idle. + virtual void onPause() { }; + + // Function: onResume + // If a state has been paused and is then made current again by the + // state(s) pushed on top of it being popped, the state management system + // will call onResume allowing the state to undo any work that had been + // done in . + virtual void onResume() { }; }; typedef photon::shared_ptr StatePtr; diff --git a/include/entrypoint.hpp b/include/entrypoint.hpp index d85d910..6dca1ef 100644 --- a/include/entrypoint.hpp +++ b/include/entrypoint.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: entrypoint.hpp,v 1.8 2005/08/08 19:19:25 cozman Exp $ +// $Id: entrypoint.hpp,v 1.9 2005/08/10 21:22:33 cozman Exp $ #ifndef PHOTON_ENTRYPOINT_HPP @@ -33,7 +33,7 @@ // Application::getInstance().createDisplay(800,600,32,0,0,false); // // // set current state -// Application::getInstance().setCurrentState(); +// Application::getInstance().setState(); // // // can also add any tasks here // diff --git a/src/Application.cpp b/src/Application.cpp index e9bb5da..28d784b 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Application.cpp,v 1.21 2005/08/10 05:36:58 cozman Exp $ +// $Id: Application.cpp,v 1.22 2005/08/10 21:22:33 cozman Exp $ #include "Application.hpp" @@ -389,6 +389,21 @@ double Application::getFramerate() { return 1/updateTask_->secPerFrame_; } + +// States ////////////////////////////////////////////////////////////////////// + +void Application::popState() +{ + stateStack_.pop(); // pop current state from stack + + // if there is another state, resume it it + if(!stateStack_.empty()) + { + stateStack_.top()->onResume(); + stateRender_->state_ = stateUpdate_->state_ = stateStack_.top(); + } +} + // AudioCore /////////////////////////////////////////////////////////////////// #ifdef PHOTON_USE_OPENAL diff --git a/test/Audio_test.cpp b/test/Audio_test.cpp index f4f9dc3..e6467e6 100644 --- a/test/Audio_test.cpp +++ b/test/Audio_test.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Audio_test.cpp,v 1.12 2005/08/08 21:39:41 cozman Exp $ +// $Id: Audio_test.cpp,v 1.13 2005/08/10 21:22:33 cozman Exp $ #include "photon.hpp" using namespace photon; @@ -193,7 +193,7 @@ int PhotonMain(const StrVec& args) Kernel::getInstance().addTask(TaskPtr(new FPSDisplayTask())); // register state and make active - Application::getInstance().setCurrentState(); + Application::getInstance().setState(); // run until finished Kernel::getInstance().run(); diff --git a/test/Font_test.cpp b/test/Font_test.cpp index 5432fa7..6583afc 100644 --- a/test/Font_test.cpp +++ b/test/Font_test.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Font_test.cpp,v 1.10 2005/08/08 06:37:10 cozman Exp $ +// $Id: Font_test.cpp,v 1.11 2005/08/10 21:22:33 cozman Exp $ #include "photon.hpp" using namespace photon; @@ -56,7 +56,7 @@ int PhotonMain(const StrVec& args) Kernel::getInstance().addTask(TaskPtr(new FPSDisplayTask())); // set current state - Application::getInstance().setCurrentState(); + Application::getInstance().setState(); // run until finished Kernel::getInstance().run(); diff --git a/test/Image_test.cpp b/test/Image_test.cpp index 6c7fc86..ac01091 100644 --- a/test/Image_test.cpp +++ b/test/Image_test.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Image_test.cpp,v 1.10 2005/08/08 06:37:10 cozman Exp $ +// $Id: Image_test.cpp,v 1.11 2005/08/10 21:22:33 cozman Exp $ #include "photon.hpp" using namespace photon; @@ -67,7 +67,7 @@ int PhotonMain(const StrVec& args) Kernel::getInstance().addTask(TaskPtr(new FPSDisplayTask())); // set current state - Application::getInstance().setCurrentState(); + Application::getInstance().setState(); // run until finished Kernel::getInstance().run(); diff --git a/test/Input_test.cpp b/test/Input_test.cpp index 8649837..de30fd0 100644 --- a/test/Input_test.cpp +++ b/test/Input_test.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Input_test.cpp,v 1.8 2005/08/08 19:24:31 cozman Exp $ +// $Id: Input_test.cpp,v 1.9 2005/08/10 21:22:33 cozman Exp $ #include "photon.hpp" using namespace photon; @@ -120,7 +120,7 @@ int PhotonMain(const StrVec& args) Kernel::getInstance().addTask(TaskPtr(new FPSDisplayTask())); // set current state - Application::getInstance().setCurrentState(); + Application::getInstance().setState(); // run until finished Kernel::getInstance().run(); diff --git a/test/Pen_test.cpp b/test/Pen_test.cpp index e0657a2..d15b8ce 100644 --- a/test/Pen_test.cpp +++ b/test/Pen_test.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Pen_test.cpp,v 1.7 2005/08/08 06:37:10 cozman Exp $ +// $Id: Pen_test.cpp,v 1.8 2005/08/10 21:22:33 cozman Exp $ #include "photon.hpp" using namespace photon; @@ -80,7 +80,7 @@ int PhotonMain(const StrVec& args) Kernel::getInstance().addTask(TaskPtr(new FPSDisplayTask())); // set current state - Application::getInstance().setCurrentState(); + Application::getInstance().setState(); // run until finished Kernel::getInstance().run(); diff --git a/test/Texture_test.cpp b/test/Texture_test.cpp index 87359bb..c7a6fda 100644 --- a/test/Texture_test.cpp +++ b/test/Texture_test.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Texture_test.cpp,v 1.8 2005/08/08 06:37:10 cozman Exp $ +// $Id: Texture_test.cpp,v 1.9 2005/08/10 21:22:33 cozman Exp $ #include "photon.hpp" using namespace photon; @@ -80,7 +80,7 @@ int PhotonMain(const StrVec& args) Kernel::getInstance().addTask(TaskPtr(new FPSDisplayTask())); // set current state - Application::getInstance().setCurrentState(); + Application::getInstance().setState(); // run until finished Kernel::getInstance().run();