refactored game
[physics.git] / src / game.cpp
CommitLineData
e68f847b
PG
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
a483ed75
PG
18#include "game.h"
19#include "debug.h"
20
054d658f
PG
21#include <vector>
22using std::vector;
23
054d658f 24#include "entityCreator.h"
33b8c69b 25#include "effectManager.h"
054d658f 26
ad9f1fb6
PG
27#include "GameStates/GameState.h"
28#include "GameStates/Running.h"
29#include "GameStates/Paused.h"
30#include "GameStates/CreatingPolygon.h"
31
68b2316d 32
ad9f1fb6
PG
33/// ***** Private Variables *****
34
35// The stack of active game states
c16d1683 36static vector<GameState*> s_active_States;
ad9f1fb6
PG
37
38// Pointers to each possible game state
c16d1683
PG
39// inserted and removed from the s_active_States
40static vector<GameState*> s_possible_States;
ad9f1fb6 41
033c5535 42// true if the top state requested itself to be poped
c16d1683 43static bool s_bPopState;
033c5535 44// pointer to a state wishing to be added
c16d1683 45static GameState* s_pPushState;
033c5535 46
ad9f1fb6
PG
47
48/// ***** Public Methods *****
68b2316d
PG
49
50void game::init()
ad9f1fb6 51{
c16d1683
PG
52 s_bPopState = false;
53 s_pPushState = NULL;
033c5535 54
054d658f 55 // create starting entities
054d658f 56 creator::init();
c16d1683 57 effect::init();
054d658f 58
c16d1683
PG
59 Running* pRunning = new Running();
60 s_possible_States.push_back(pRunning);
61 s_possible_States.push_back(new Paused());
62 s_possible_States.push_back(new CreatingPolygon());
a483ed75 63
c16d1683 64 s_active_States.push_back(pRunning);
33b8c69b 65
ca2d526e 66 DPF(0, "World Created");
ad9f1fb6
PG
67}
68
68b2316d 69void game::clean()
ad9f1fb6 70{
33b8c69b 71 effect::clean();
054d658f
PG
72 creator::clean();
73
c16d1683
PG
74 for(unsigned int i=0; i < s_possible_States.size(); i++)
75 {
76 delete s_possible_States[i];
77 }
78 s_possible_States.clear();
79 s_active_States.clear();
ad9f1fb6
PG
80}
81
10baa49b 82void game::handleInput()
ad9f1fb6 83{
10baa49b
PG
84 creator::handleInput();
85
c16d1683 86 int iLast = s_active_States.size() -1;
10baa49b 87
c16d1683 88 for(unsigned int i=0; i < s_possible_States.size(); i++)
c46074c1 89 {
c16d1683 90 GameState* pState = s_possible_States[i];
033c5535 91
c16d1683
PG
92 if(s_active_States[iLast] != pState && pState->pushMe())
93 {
94 s_pPushState = pState;
95 }
c46074c1 96 }
033c5535
PG
97
98
ad9f1fb6 99 for( int i = 0;
c16d1683 100 i <= iLast;
ad9f1fb6
PG
101 i++ )
102 {
c16d1683 103 if( i == iLast )
033c5535 104 {
c16d1683
PG
105 if(s_active_States[i]->popMe())
106 s_bPopState = true;
033c5535 107 else
c16d1683 108 s_active_States[i]->handleInput(true);
033c5535 109 }
ad9f1fb6 110 else
c16d1683 111 s_active_States[i]->handleInput(false);
ad9f1fb6 112 }
ad9f1fb6
PG
113}
114
c16d1683 115void game::update(float fTimeStep)
ad9f1fb6 116{
c16d1683 117 if(s_bPopState)
033c5535 118 {
c16d1683
PG
119 s_active_States.pop_back();
120 s_bPopState = false;
033c5535
PG
121 }
122
c16d1683 123 if(s_pPushState != NULL)
c46074c1 124 {
c16d1683
PG
125 s_active_States.push_back(s_pPushState);
126 s_pPushState = NULL;
c46074c1
PG
127 }
128
c16d1683 129 int iLast = s_active_States.size() -1;
ad9f1fb6 130 for( int i = 0;
c16d1683 131 i <= iLast;
ad9f1fb6
PG
132 i++ )
133 {
c16d1683
PG
134 if( i == iLast )
135 s_active_States[i]->update(fTimeStep, true);
ad9f1fb6 136 else
c16d1683 137 s_active_States[i]->update(fTimeStep, false);
ad9f1fb6
PG
138 }
139}
140
68b2316d 141void game::draw()
ad9f1fb6 142{
c16d1683 143 int iLast = s_active_States.size() -1;
ad9f1fb6 144 for( int i = 0;
c16d1683 145 i <= iLast;
ad9f1fb6
PG
146 i++ )
147 {
c16d1683
PG
148 if( i == iLast )
149 s_active_States[i]->draw(true);
ad9f1fb6 150 else
c16d1683 151 s_active_States[i]->draw(false);
ad9f1fb6
PG
152 }
153}