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 | |
2a02c4bb PG |
34 | void mainInit(); |
35 | void mainClean(); | |
36 | ||
37 | void updatesInit(); | |
38 | void updatesClean(); | |
39 | ||
40 | void drawInit(); | |
41 | void drawClean(); | |
ad9f1fb6 | 42 | |
7cbd4505 PG |
43 | void sighandler( int sig ); |
44 | ||
ad9f1fb6 | 45 | void run(); |
617dcc71 | 46 | void clean(); |
ad9f1fb6 | 47 | |
ad9f1fb6 PG |
48 | void updateFPSCounters(); |
49 | ||
68b2316d | 50 | void handleInput(); |
ad9f1fb6 PG |
51 | void update(float); |
52 | void draw(); | |
53 | ||
2a02c4bb PG |
54 | int startUpdateThread(void*); |
55 | int startDrawThread(void*); | |
56 | ||
ad9f1fb6 PG |
57 | /// ***** Private Variables ***** |
58 | ||
59 | // variable used to determine if it is time to shutdown | |
60 | bool is_Running; | |
61 | ||
62 | /* Values for the main game loop | |
63 | * target_UPS := the amount of updates that is wanted in one second | |
ce53f20f | 64 | * |
ad9f1fb6 PG |
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 | */ | |
d279b77b | 71 | int target_UPS = 100; |
8baa4b2f | 72 | float update_Wait = 0; |
ad9f1fb6 | 73 | |
ce53f20f | 74 | int ups, fps; |
ad9f1fb6 | 75 | int update_Count, draw_Count; |
8baa4b2f PG |
76 | MICRO last_Second; |
77 | ||
78 | const int min_wait_milli = 20; | |
79 | const int min_wait_micro = min_wait_milli * 1000; | |
80 | const float game_step = 10; | |
ad9f1fb6 | 81 | |
2a02c4bb PG |
82 | bool updateInitialized = false; |
83 | bool drawInitialized = false; | |
d279b77b | 84 | |
617dcc71 | 85 | /// ***** MAIN Method ***** |
3c3c195d | 86 | int main(int argc, char** args) |
617dcc71 | 87 | { |
2a02c4bb | 88 | mainInit(); |
617dcc71 | 89 | run(); |
2a02c4bb | 90 | mainClean(); |
617dcc71 PG |
91 | return 0; |
92 | } | |
93 | ||
94 | /// ***** Initializers/Cleaners ***** | |
95 | ||
2a02c4bb | 96 | void mainInit() |
ad9f1fb6 | 97 | { |
7cbd4505 PG |
98 | installSignal(); |
99 | ||
2a02c4bb PG |
100 | debug::init(); |
101 | } | |
102 | ||
103 | void mainClean() | |
104 | { | |
105 | debug::clean(); | |
106 | } | |
107 | ||
108 | void updatesInit() | |
109 | { | |
110 | while(!drawInitialized) | |
111 | SDL_Delay(100); | |
3c3c195d | 112 | |
68b2316d | 113 | game::init(); |
5f3520c8 | 114 | DPF(0, "Game initialized"); |
3c3c195d | 115 | |
68b2316d | 116 | input::init(); |
5f3520c8 | 117 | DPF(0, "Input initialized"); |
3c3c195d | 118 | |
b1d92c2f | 119 | cfg::init(); |
5f3520c8 | 120 | DPF(0, "Configs initialized"); |
b1d92c2f | 121 | |
5f3520c8 | 122 | DPF(0, "Initialization Complete"); |
2a02c4bb PG |
123 | |
124 | updateInitialized = true; | |
ad9f1fb6 PG |
125 | } |
126 | ||
2a02c4bb | 127 | void updatesClean() |
44b079f8 | 128 | { |
2a02c4bb | 129 | DPF(0, "Update Thread Cleaning"); |
44b079f8 | 130 | |
b1d92c2f PG |
131 | cfg::clean(); |
132 | ||
68b2316d | 133 | input::clean(); |
617dcc71 | 134 | |
68b2316d | 135 | game::clean(); |
2a02c4bb PG |
136 | } |
137 | ||
138 | void drawInit() | |
139 | { | |
140 | graphics::init(); | |
141 | DPF(0, "Graphics initialized"); | |
142 | ||
143 | drawInitialized = true; | |
144 | ||
145 | while(!updateInitialized) | |
146 | SDL_Delay(100); | |
147 | } | |
148 | ||
149 | void drawClean() | |
150 | { | |
151 | DPF(0, "Draw Thread Cleaning"); | |
44b079f8 | 152 | |
68b2316d | 153 | graphics::clean(); |
44b079f8 PG |
154 | } |
155 | ||
617dcc71 | 156 | /// ***** Private Methods ***** |
2a02c4bb PG |
157 | SDL_Thread* updatesThread = NULL; |
158 | SDL_Thread* drawThread = NULL; | |
617dcc71 | 159 | |
ad9f1fb6 PG |
160 | void run() |
161 | { | |
162 | is_Running = true; | |
d279b77b | 163 | |
2a02c4bb PG |
164 | updatesThread = SDL_CreateThread(startUpdateThread, NULL); |
165 | drawThread = SDL_CreateThread(startDrawThread, NULL); | |
d279b77b | 166 | |
2a02c4bb PG |
167 | SDL_WaitThread(updatesThread, NULL); |
168 | SDL_WaitThread(drawThread, NULL); | |
ad9f1fb6 PG |
169 | } |
170 | ||
171 | void 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; | |
ce53f20f PG |
178 | |
179 | // NOT thread safe, but they're estimates anyways | |
180 | update_Count -= ups; | |
181 | draw_Count -= fps; | |
ad9f1fb6 PG |
182 | |
183 | last_Second = tickCountMicro(); | |
44b079f8 | 184 | |
7adc59fe PG |
185 | #ifdef FPSUPS |
186 | cout << "ups:\t" << ups << endl; | |
187 | cout << "fps:\t" << fps << endl; | |
188 | #endif | |
ad9f1fb6 PG |
189 | } |
190 | } | |
191 | ||
68b2316d | 192 | void handleInput() |
ad9f1fb6 | 193 | { |
ce53f20f | 194 | input::update(); |
f7b3b2eb | 195 | |
ce53f20f | 196 | game::handleInput(); |
ad9f1fb6 | 197 | |
ce53f20f PG |
198 | if(cfg::endGame()) |
199 | is_Running = false; | |
ad9f1fb6 PG |
200 | } |
201 | ||
202 | void update(float time_step) | |
203 | { | |
ce53f20f | 204 | game::update(time_step); |
ad9f1fb6 | 205 | update_Count++; |
ad9f1fb6 PG |
206 | } |
207 | ||
208 | void draw() | |
209 | { | |
ce53f20f | 210 | game::draw(); |
d279b77b | 211 | |
4a0863f3 PG |
212 | SDL_PumpEvents(); // has to be on the Draw thread for the Windows API |
213 | ||
ce53f20f PG |
214 | SDL_GL_SwapBuffers(); |
215 | ||
216 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
ad9f1fb6 | 217 | |
ce53f20f PG |
218 | draw_Count++; |
219 | } | |
d279b77b | 220 | |
ce53f20f PG |
221 | int startUpdateThread(void*) |
222 | { | |
2a02c4bb PG |
223 | updatesInit(); |
224 | ||
8baa4b2f PG |
225 | last_Second = tickCountMicro(); |
226 | ||
ce53f20f PG |
227 | while(is_Running) |
228 | { | |
8baa4b2f PG |
229 | MICRO time = tickCountMicro(); |
230 | handleInput(); | |
231 | update(game_step); | |
232 | ||
233 | updateFPSCounters(); | |
234 | time = tickCountMicro() - time; | |
d279b77b | 235 | |
8baa4b2f PG |
236 | float wait = (1000000.0 / target_UPS - time); |
237 | update_Wait += 0 < wait ? wait : 0; | |
d279b77b | 238 | |
8baa4b2f PG |
239 | while(min_wait_micro < update_Wait) |
240 | { | |
241 | update_Wait -= min_wait_micro; | |
242 | SDL_Delay(min_wait_milli); | |
243 | } | |
ce53f20f | 244 | } |
ad9f1fb6 | 245 | |
2a02c4bb PG |
246 | updatesClean(); |
247 | ||
ce53f20f PG |
248 | return 0; |
249 | } | |
ad9f1fb6 | 250 | |
ce53f20f PG |
251 | int startDrawThread(void*) |
252 | { | |
2a02c4bb PG |
253 | drawInit(); |
254 | ||
ce53f20f PG |
255 | while(is_Running) |
256 | { | |
257 | draw(); | |
258 | } | |
25d84412 | 259 | |
2a02c4bb PG |
260 | drawClean(); |
261 | ||
ce53f20f | 262 | return 0; |
ad9f1fb6 | 263 | } |