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