| 1 | /* |
| 2 | * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "game.h" |
| 19 | |
| 20 | #include <bear/debug.h> |
| 21 | using namespace bear; |
| 22 | |
| 23 | #include <vector> |
| 24 | using std::vector; |
| 25 | |
| 26 | #include "entityCreator.h" |
| 27 | #include "effectManager.h" |
| 28 | |
| 29 | #include "GameStates/GameState.h" |
| 30 | #include "GameStates/Running.h" |
| 31 | #include "GameStates/Paused.h" |
| 32 | #include "GameStates/CreatingPolygon.h" |
| 33 | |
| 34 | |
| 35 | /// ***** Private Variables ***** |
| 36 | |
| 37 | // The stack of active game states |
| 38 | static vector<GameState*> s_active_States; |
| 39 | |
| 40 | // Pointers to each possible game state |
| 41 | // inserted and removed from the s_active_States |
| 42 | static vector<GameState*> s_possible_States; |
| 43 | |
| 44 | // true if the top state requested itself to be poped |
| 45 | static bool s_bPopState; |
| 46 | // pointer to a state wishing to be added |
| 47 | static GameState* s_pPushState; |
| 48 | |
| 49 | |
| 50 | /// ***** Public Methods ***** |
| 51 | |
| 52 | void game::init() |
| 53 | { |
| 54 | s_bPopState = false; |
| 55 | s_pPushState = NULL; |
| 56 | |
| 57 | // create starting entities |
| 58 | creator::init(); |
| 59 | effect::init(); |
| 60 | |
| 61 | Running* pRunning = new Running(); |
| 62 | s_possible_States.push_back(pRunning); |
| 63 | s_possible_States.push_back(new Paused()); |
| 64 | s_possible_States.push_back(new CreatingPolygon()); |
| 65 | |
| 66 | s_active_States.push_back(pRunning); |
| 67 | |
| 68 | DPF(0, "World Created"); |
| 69 | } |
| 70 | |
| 71 | void game::clean() |
| 72 | { |
| 73 | effect::clean(); |
| 74 | creator::clean(); |
| 75 | |
| 76 | for(unsigned int i=0; i < s_possible_States.size(); i++) |
| 77 | { |
| 78 | delete s_possible_States[i]; |
| 79 | } |
| 80 | s_possible_States.clear(); |
| 81 | s_active_States.clear(); |
| 82 | } |
| 83 | |
| 84 | void game::handleInput() |
| 85 | { |
| 86 | creator::handleInput(); |
| 87 | |
| 88 | int iLast = s_active_States.size() -1; |
| 89 | |
| 90 | for(unsigned int i=0; i < s_possible_States.size(); i++) |
| 91 | { |
| 92 | GameState* pState = s_possible_States[i]; |
| 93 | |
| 94 | if(s_active_States[iLast] != pState && pState->pushMe()) |
| 95 | { |
| 96 | s_pPushState = pState; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | for( int i = 0; |
| 102 | i <= iLast; |
| 103 | i++ ) |
| 104 | { |
| 105 | if( i == iLast ) |
| 106 | { |
| 107 | if(s_active_States[i]->popMe()) |
| 108 | s_bPopState = true; |
| 109 | else |
| 110 | s_active_States[i]->handleInput(true); |
| 111 | } |
| 112 | else |
| 113 | s_active_States[i]->handleInput(false); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void game::update(float fTimeStep) |
| 118 | { |
| 119 | if(s_bPopState) |
| 120 | { |
| 121 | s_active_States.pop_back(); |
| 122 | s_bPopState = false; |
| 123 | } |
| 124 | |
| 125 | if(s_pPushState != NULL) |
| 126 | { |
| 127 | s_active_States.push_back(s_pPushState); |
| 128 | s_pPushState = NULL; |
| 129 | } |
| 130 | |
| 131 | int iLast = s_active_States.size() -1; |
| 132 | for( int i = 0; |
| 133 | i <= iLast; |
| 134 | i++ ) |
| 135 | { |
| 136 | if( i == iLast ) |
| 137 | s_active_States[i]->update(fTimeStep, true); |
| 138 | else |
| 139 | s_active_States[i]->update(fTimeStep, false); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void game::draw() |
| 144 | { |
| 145 | int iLast = s_active_States.size() -1; |
| 146 | for( int i = 0; |
| 147 | i <= iLast; |
| 148 | i++ ) |
| 149 | { |
| 150 | if( i == iLast ) |
| 151 | s_active_States[i]->draw(true); |
| 152 | else |
| 153 | s_active_States[i]->draw(false); |
| 154 | } |
| 155 | } |