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