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