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