X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=blobdiff_plain;f=src%2Fgame.cpp;h=3cab823206e6ec53108cd6ad5c1cb5a8f857bb9d;hb=f32a9b7c8eab3536ad354f85ee65c41d5b5da006;hp=3b7a34b8c785f8c2c5f0a1f04257e96d694b7d43;hpb=033c553524d4a0407f6fabd0bcebf1d460519cf5;p=physics.git diff --git a/src/game.cpp b/src/game.cpp index 3b7a34b..3cab823 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -16,7 +16,8 @@ */ #include "game.h" -#include "debug.h" + +#include #include using std::vector; @@ -33,122 +34,121 @@ using std::vector; /// ***** Private Variables ***** // The stack of active game states -vector active_States; +static vector s_active_States; // Pointers to each possible game state -// inserted and removed from the active_States -Running* running; -Paused* paused; -CreatingPolygon* creating_Polygon; +// inserted and removed from the s_active_States +static vector s_possible_States; // true if the top state requested itself to be poped -bool pop_State; +static bool s_bPopState; // pointer to a state wishing to be added -GameState* push_State; +static GameState* s_pPushState; /// ***** Public Methods ***** void game::init() { - running = new Running(); - paused = new Paused(); - creating_Polygon = new CreatingPolygon(); - - pop_State = false; - push_State = NULL; + s_bPopState = false; + s_pPushState = NULL; // create starting entities creator::init(); + effect::init(); - active_States.push_back(running); + Running* pRunning = new Running(); + s_possible_States.push_back(pRunning); + s_possible_States.push_back(new Paused()); + s_possible_States.push_back(new CreatingPolygon()); - effect::init(); + s_active_States.push_back(pRunning); -#ifdef DEBUGGING - cout << "World Created" << endl; -#endif + DPF(0, "World Created"); } void game::clean() { effect::clean(); - creator::clean(); - delete creating_Polygon; - delete paused; - delete running; + for(unsigned int i=0; i < s_possible_States.size(); i++) + { + delete s_possible_States[i]; + } + s_possible_States.clear(); + s_active_States.clear(); } -void game::input() +void game::handleInput() { - if(running->pushMe()) - push_State = running; + creator::handleInput(); - if(paused->pushMe()) - push_State = paused; + int iLast = s_active_States.size() -1; - if(creating_Polygon->pushMe()) - push_State = creating_Polygon; + for(unsigned int i=0; i < s_possible_States.size(); i++) + { + GameState* pState = s_possible_States[i]; + + if(s_active_States[iLast] != pState && pState->pushMe()) + { + s_pPushState = pState; + } + } - int last = active_States.size() -1; for( int i = 0; - i <= last; + i <= iLast; i++ ) { - if( i == last ) + if( i == iLast ) { - if(active_States[i]->popMe()) - pop_State = true; + if(s_active_States[i]->popMe()) + s_bPopState = true; else - active_States[i]->handleInput(true); + s_active_States[i]->handleInput(true); } else - active_States[i]->handleInput(false); + s_active_States[i]->handleInput(false); } } -void game::update(float time_step) +void game::update(float fTimeStep) { - if(push_State != NULL) + if(s_bPopState) { - // don't want to pop and push same state, pop wins arbitrary - if(!pop_State) - active_States.push_back(push_State); - - push_State = NULL; + s_active_States.pop_back(); + s_bPopState = false; } - if(pop_State) + if(s_pPushState != NULL) { - active_States.pop_back(); - pop_State = false; + s_active_States.push_back(s_pPushState); + s_pPushState = NULL; } - int last = active_States.size() -1; + int iLast = s_active_States.size() -1; for( int i = 0; - i <= last; + i <= iLast; i++ ) { - if( i == last ) - active_States[i]->update(time_step, true); + if( i == iLast ) + s_active_States[i]->update(fTimeStep, true); else - active_States[i]->update(time_step, false); + s_active_States[i]->update(fTimeStep, false); } } void game::draw() { - int last = active_States.size() -1; + int iLast = s_active_States.size() -1; for( int i = 0; - i <= last; + i <= iLast; i++ ) { - if( i == last ) - active_States[i]->draw(true); + if( i == iLast ) + s_active_States[i]->draw(true); else - active_States[i]->draw(false); + s_active_States[i]->draw(false); } }