changes to how configs react to input
[physics.git] / src / main.cpp
... / ...
CommitLineData
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 <GL/gl.h>
19#include <GL/glu.h>
20#include <SDL/SDL.h>
21
22#include "debug.h"
23#include "handleSignal.h"
24
25#include "game.h"
26#include "ticks.h"
27
28#include "graphics/graphics.h"
29#include "input/inputManager.h"
30#include "config/config.h"
31
32/// ***** Private Method Headers *****
33
34void mainInit();
35void mainClean();
36
37void updatesInit();
38void updatesClean();
39
40void drawInit();
41void drawClean();
42
43void sighandler( int sig );
44
45void run();
46void clean();
47
48void updateFPSCounters();
49
50void handleInput();
51void update(float);
52void draw();
53
54int startUpdateThread(void*);
55int startDrawThread(void*);
56
57/// ***** Private Variables *****
58
59// variable used to determine if it is time to shutdown
60bool is_Running;
61
62/* Values for the main game loop
63 * target_UPS := the amount of updates that is wanted in one second
64 *
65 * ups := updates per second for the last second
66 * fps := frames per second for the last second
67 * update_Count := counts this seconds updates
68 * draw_Count := counts this seconds draws
69 * last_Second := stores the time of the last second, used for ups and fps
70 */
71int target_UPS = 100;
72float update_Wait = 0;
73
74int ups, fps;
75int update_Count, draw_Count;
76MICRO last_Second;
77
78const int min_wait_milli = 20;
79const int min_wait_micro = min_wait_milli * 1000;
80const float game_step = 10;
81
82bool updateInitialized = false;
83bool drawInitialized = false;
84
85/// ***** MAIN Method *****
86int main(int argc, char** args)
87{
88 mainInit();
89 run();
90 mainClean();
91 return 0;
92}
93
94/// ***** Initializers/Cleaners *****
95
96void mainInit()
97{
98 installSignal();
99
100 debug::init();
101}
102
103void mainClean()
104{
105 debug::clean();
106}
107
108void updatesInit()
109{
110 while(!drawInitialized)
111 SDL_Delay(100);
112
113 game::init();
114 DPF(0, "Game initialized");
115
116 input::init();
117 DPF(0, "Input initialized");
118
119 cfg::init();
120 DPF(0, "Configs initialized");
121
122 DPF(0, "Initialization Complete");
123
124 updateInitialized = true;
125}
126
127void updatesClean()
128{
129 DPF(0, "Update Thread Cleaning");
130
131 cfg::clean();
132
133 input::clean();
134
135 game::clean();
136}
137
138void drawInit()
139{
140 graphics::init();
141 DPF(0, "Graphics initialized");
142
143 drawInitialized = true;
144
145 while(!updateInitialized)
146 SDL_Delay(100);
147}
148
149void drawClean()
150{
151 DPF(0, "Draw Thread Cleaning");
152
153 graphics::clean();
154}
155
156/// ***** Private Methods *****
157SDL_Thread* updatesThread = NULL;
158SDL_Thread* drawThread = NULL;
159
160void run()
161{
162 is_Running = true;
163
164 updatesThread = SDL_CreateThread(startUpdateThread, NULL);
165 drawThread = SDL_CreateThread(startDrawThread, NULL);
166
167 SDL_WaitThread(updatesThread, NULL);
168 SDL_WaitThread(drawThread, NULL);
169}
170
171void updateFPSCounters()
172{
173 // Check if a second has passed to recalculate UPS and FPS
174 if (tickCountMicro() - last_Second >= 1000000)
175 {
176 ups = update_Count;
177 fps = draw_Count;
178
179 // NOT thread safe, but they're estimates anyways
180 update_Count -= ups;
181 draw_Count -= fps;
182
183 last_Second = tickCountMicro();
184
185 if(cfg::showFPS())
186 {
187 cout << "fps:\t" << fps << endl;
188 }
189 if(cfg::showUPS())
190 {
191 cout << "ups:\t" << ups << endl;
192 }
193 }
194}
195
196void handleInput()
197{
198 input::update();
199
200 cfg::handleInput();
201 game::handleInput();
202
203 if(cfg::endGame())
204 is_Running = false;
205}
206
207void update(float time_step)
208{
209 game::update(time_step);
210 update_Count++;
211}
212
213void draw()
214{
215 game::draw();
216
217 SDL_PumpEvents(); // has to be on the Draw thread for the Windows API
218
219 SDL_GL_SwapBuffers();
220
221 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
222
223 draw_Count++;
224}
225
226int startUpdateThread(void*)
227{
228 updatesInit();
229
230 last_Second = tickCountMicro();
231
232 while(is_Running)
233 {
234 MICRO time = tickCountMicro();
235 handleInput();
236 update(game_step);
237
238 updateFPSCounters();
239 time = tickCountMicro() - time;
240
241 float wait = (1000000.0 / target_UPS - time);
242 update_Wait += 0 < wait ? wait : 0;
243
244 while(min_wait_micro < update_Wait)
245 {
246 update_Wait -= min_wait_micro;
247 SDL_Delay(min_wait_milli);
248 }
249 }
250
251 updatesClean();
252
253 return 0;
254}
255
256int startDrawThread(void*)
257{
258 drawInit();
259
260 while(is_Running)
261 {
262 draw();
263 }
264
265 drawClean();
266
267 return 0;
268}