created DPF
[physics.git] / src / main.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
ad9f1fb6
PG
18#include <GL/gl.h>
19#include <GL/glu.h>
20#include <SDL/SDL.h>
21
f7b3b2eb 22#include "debug.h"
7cbd4505 23#include "handleSignal.h"
f7b3b2eb 24
ad9f1fb6
PG
25#include "game.h"
26#include "ticks.h"
44b079f8
PG
27
28#include "graphics/graphics.h"
f7b3b2eb 29#include "input/inputManager.h"
b1d92c2f 30#include "config/config.h"
ad9f1fb6 31
ad9f1fb6 32/// ***** Private Method Headers *****
617dcc71 33
ad9f1fb6
PG
34void init();
35
7cbd4505
PG
36void sighandler( int sig );
37
ad9f1fb6 38void run();
617dcc71 39void clean();
ad9f1fb6
PG
40
41void blockUpdate();
42void updateFPSCounters();
43
68b2316d 44void handleInput();
ad9f1fb6
PG
45void update(float);
46void draw();
47
ad9f1fb6
PG
48/// ***** Private Variables *****
49
50// variable used to determine if it is time to shutdown
51bool is_Running;
52
53/* Values for the main game loop
54 * target_UPS := the amount of updates that is wanted in one second
55 * last_Update := stores the time of the last update
56 * update_Sum := used to determine the updates needs for this run
57
58 * ups := updates per second for the last second
59 * fps := frames per second for the last second
60 * update_Count := counts this seconds updates
61 * draw_Count := counts this seconds draws
62 * last_Second := stores the time of the last second, used for ups and fps
63 */
d279b77b 64int target_UPS = 100;
ad9f1fb6
PG
65long int last_Block_Update;
66float update_Sum = 0;
67
9bbcda11 68int ups=100, fps=100;
ad9f1fb6
PG
69int update_Count, draw_Count;
70long int last_Second;
71
72
25d84412
PG
73float target_time_steps_per_second = 1000;
74float times;
75float time_steps_Count;
d279b77b
PG
76
77
78// experiment with rolling averages
79float rUpdate = 100;
80float rDraw = 100;
25d84412 81float rInput = 100;
d279b77b 82float rRun = 100;
25d84412 83
d279b77b
PG
84float num = 10;
85
9bbcda11 86float total = 0;
d279b77b 87
617dcc71 88/// ***** MAIN Method *****
3c3c195d 89int main(int argc, char** args)
617dcc71
PG
90{
91 init();
92 run();
93 clean();
94 return 0;
95}
96
97/// ***** Initializers/Cleaners *****
98
ad9f1fb6
PG
99void init()
100{
7cbd4505
PG
101 installSignal();
102
68b2316d 103 graphics::init();
5f3520c8 104 DPF(0, "Graphics initialized");
3c3c195d 105
68b2316d 106 game::init();
5f3520c8 107 DPF(0, "Game initialized");
3c3c195d 108
68b2316d 109 input::init();
5f3520c8 110 DPF(0, "Input initialized");
3c3c195d 111
b1d92c2f 112 cfg::init();
5f3520c8 113 DPF(0, "Configs initialized");
b1d92c2f 114
5f3520c8 115 DPF(0, "Initialization Complete");
ad9f1fb6
PG
116}
117
617dcc71 118void clean()
44b079f8
PG
119{
120#ifdef DEBUGGING
121 cout << "Cleaning up" << endl;
122#endif
123
b1d92c2f
PG
124 cfg::clean();
125
68b2316d 126 input::clean();
617dcc71 127
68b2316d 128 game::clean();
44b079f8 129
68b2316d 130 graphics::clean();
44b079f8
PG
131}
132
617dcc71
PG
133/// ***** Private Methods *****
134
ad9f1fb6
PG
135void run()
136{
137 is_Running = true;
138 last_Block_Update = tickCountMicro();
139 last_Second = tickCountMicro();
140
141 while(is_Running)
142 {
d279b77b
PG
143 long int time;
144
145 time = tickCountMicro();
146 blockUpdate();
147 updateFPSCounters();
148 draw();
149 time = tickCountMicro() - time;
150
151 rRun = (rRun * (num-1) + time) / num;
152
25d84412
PG
153 //cout << "total:\t" << total << endl;
154 //cout << "rR:\t" << rRun << endl;
155 //total = 0;
ad9f1fb6
PG
156 }
157}
158
ad9f1fb6
PG
159void blockUpdate()
160{
161 long int start = tickCountMicro();
2ea1d2d4 162 long int diff = start - last_Block_Update;
ad9f1fb6 163
25d84412 164 //cout << "Block" << endl;
d279b77b 165
ad9f1fb6 166 // Calculate the updates that should be run for the next draw
9bbcda11 167 update_Sum += (float)(diff * target_UPS) / 1000000;
ad9f1fb6
PG
168
169 // insures the float to int cast is done once.
170 int iupdate_sum = (int)update_Sum;
171
172 // TODO the main run loop needs to be tested and pruned
173 if (iupdate_sum > 0)
174 {
9bbcda11
PG
175 // Calculate a time step that spreads the updates out as much as
176 // possible used because really quick updates are nearly wasted
2ea1d2d4
PG
177
178 //float time_step = ((float)diff) / iupdate_sum / 1000;
179 //float time_step = 1000 / (100000 / rUpdate) / iupdate_sum;
9bbcda11 180 //float time_step = 1000 / ups / iupdate_sum;
2ea1d2d4 181 float time_step = 10;
ad9f1fb6
PG
182
183 // run the updates
2ea1d2d4 184 for (int i = 0; i < iupdate_sum; i++)
ad9f1fb6 185 {
68b2316d 186 handleInput();
25d84412 187 update(time_step);
ad9f1fb6 188 }
9bbcda11
PG
189
190 // remove the updates that were run from the sum
ad9f1fb6
PG
191 update_Sum -= iupdate_sum;
192 last_Block_Update = tickCountMicro();
193 }
194}
195
196void updateFPSCounters()
197{
198 // Check if a second has passed to recalculate UPS and FPS
199 if (tickCountMicro() - last_Second >= 1000000)
200 {
201 ups = update_Count;
202 fps = draw_Count;
25d84412 203 times = time_steps_Count;
ad9f1fb6
PG
204 update_Count = 0;
205 draw_Count = 0;
25d84412 206 time_steps_Count = 0;
ad9f1fb6
PG
207
208 last_Second = tickCountMicro();
44b079f8 209
7adc59fe
PG
210#ifdef FPSUPS
211 cout << "ups:\t" << ups << endl;
212 cout << "fps:\t" << fps << endl;
25d84412 213 cout << "times:\t" << times << endl;
7adc59fe 214#endif
ad9f1fb6
PG
215 }
216}
217
68b2316d 218void handleInput()
ad9f1fb6 219{
25d84412
PG
220 long int time;
221
222 time = tickCountMicro();
223 input::update();
f7b3b2eb 224
25d84412 225 game::handleInput();
ad9f1fb6 226
25d84412
PG
227 if(cfg::endGame())
228 is_Running = false;
229 time = tickCountMicro() - time;
230
231 rInput = (rInput*(num-1) + time) /num;
232
233 total += rInput;
ad9f1fb6
PG
234}
235
236void update(float time_step)
237{
d279b77b
PG
238 long int time;
239
ad9f1fb6 240 update_Count++;
25d84412 241 time_steps_Count += time_step;
ad9f1fb6 242
d279b77b
PG
243 time = tickCountMicro();
244 game::update(time_step);
245 time = tickCountMicro() - time;
246
247 rUpdate = (rUpdate * (num-1) + time) / num;
248
25d84412
PG
249 //cout << "ts:\t" << time_step << endl;
250 //cout << "ru:\t" << rUpdate << endl;
251
252 total += rUpdate;
ad9f1fb6
PG
253}
254
255void draw()
256{
d279b77b
PG
257 long int time;
258
ad9f1fb6
PG
259 draw_Count++;
260
d279b77b
PG
261 time = tickCountMicro();
262 game::draw();
263
264 SDL_GL_SwapBuffers();
265
266 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
267
9bbcda11 268 //SDL_Delay(10);
d279b77b 269 time = tickCountMicro() - time;
ad9f1fb6 270
d279b77b 271 rDraw = (rDraw*(num-1) + time) /num;
ad9f1fb6 272
25d84412
PG
273 //cout << "rd:\t" << rDraw << endl;
274
275 total += rDraw;
ad9f1fb6 276}