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