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 | ||
f32a9b7c | 18 | #include <pg/debug.h> |
6029825f | 19 | #include <pg/ticks.h> |
b85b89ba | 20 | using namespace pg; |
f32a9b7c | 21 | |
7cc180e4 PG |
22 | #include <iostream> |
23 | using std::cout; | |
24 | using std::endl; | |
25 | ||
ad9f1fb6 PG |
26 | #include <GL/gl.h> |
27 | #include <GL/glu.h> | |
28 | #include <SDL/SDL.h> | |
29 | ||
7cbd4505 | 30 | #include "handleSignal.h" |
f7b3b2eb | 31 | |
ad9f1fb6 | 32 | #include "game.h" |
44b079f8 PG |
33 | |
34 | #include "graphics/graphics.h" | |
f7b3b2eb | 35 | #include "input/inputManager.h" |
b1d92c2f | 36 | #include "config/config.h" |
ad9f1fb6 | 37 | |
ad9f1fb6 | 38 | /// ***** Private Method Headers ***** |
617dcc71 | 39 | |
e2b1ddcd PG |
40 | static void mainInit(); |
41 | static void mainClean(); | |
2a02c4bb | 42 | |
e2b1ddcd PG |
43 | static void updatesInit(); |
44 | static void updatesClean(); | |
2a02c4bb | 45 | |
e2b1ddcd PG |
46 | static void drawInit(); |
47 | static void drawClean(); | |
ad9f1fb6 | 48 | |
e2b1ddcd | 49 | static void run(); |
7cbd4505 | 50 | |
e2b1ddcd | 51 | static void updateFPSCounters(); |
ad9f1fb6 | 52 | |
e2b1ddcd PG |
53 | static void handleInput(); |
54 | static void update(float); | |
55 | static void draw(); | |
ad9f1fb6 | 56 | |
e2b1ddcd PG |
57 | static int startUpdateThread(void*); |
58 | static int startDrawThread(void*); | |
2a02c4bb | 59 | |
ad9f1fb6 PG |
60 | /// ***** Private Variables ***** |
61 | ||
62 | // variable used to determine if it is time to shutdown | |
e2b1ddcd PG |
63 | static bool s_bIsRunning; |
64 | ||
65 | // Minimum possible wait times by used library | |
66 | static const int s_iMinWaitMilli = 20; | |
67 | static const int s_iMinWaitMicro = s_iMinWaitMilli * 1000; | |
ad9f1fb6 PG |
68 | |
69 | /* Values for the main game loop | |
ce53f20f | 70 | * |
e2b1ddcd PG |
71 | * s_iTargetUPS := the amount of updates that is wanted in one second |
72 | * s_fAccUpdateWait := the accumulated wait time for the update sleeps | |
73 | * | |
74 | * s_iUPS := updates per second for the last second | |
75 | * s_iFPS := frames per second for the last second | |
76 | * s_iUpdateCount := counts this seconds updates | |
77 | * s_iDrawCount := counts this seconds draws | |
78 | * s_micLastSecond := stores the time of the last second, used for ups and fps | |
ad9f1fb6 | 79 | */ |
e2b1ddcd PG |
80 | static int s_iTargetUPS = 100; |
81 | static float s_fAccUpdateWait = 0; | |
ad9f1fb6 | 82 | |
fa885913 PG |
83 | static int s_iTargetFPS = 100; |
84 | static float s_fAccDrawWait = 0; | |
85 | ||
e2b1ddcd PG |
86 | static int s_iUPS, s_iFPS; |
87 | static int s_iUpdateCount, s_iDrawCount; | |
88 | static MICRO s_micLastSecond; | |
8baa4b2f | 89 | |
e2b1ddcd | 90 | static const float s_fGameStep = 10; |
ad9f1fb6 | 91 | |
e2b1ddcd PG |
92 | static bool s_bUpdateInitialized = false; |
93 | static bool s_bDrawInitialized = false; | |
d279b77b | 94 | |
617dcc71 | 95 | /// ***** MAIN Method ***** |
3c3c195d | 96 | int main(int argc, char** args) |
617dcc71 | 97 | { |
2a02c4bb | 98 | mainInit(); |
617dcc71 | 99 | run(); |
2a02c4bb | 100 | mainClean(); |
e2b1ddcd | 101 | |
617dcc71 PG |
102 | return 0; |
103 | } | |
104 | ||
105 | /// ***** Initializers/Cleaners ***** | |
106 | ||
2a02c4bb | 107 | void mainInit() |
ad9f1fb6 | 108 | { |
7cbd4505 PG |
109 | installSignal(); |
110 | ||
2a02c4bb PG |
111 | debug::init(); |
112 | } | |
2a02c4bb PG |
113 | void mainClean() |
114 | { | |
563db69a | 115 | debug::fini(); |
2a02c4bb PG |
116 | } |
117 | ||
118 | void updatesInit() | |
119 | { | |
e2b1ddcd | 120 | while(!s_bDrawInitialized) |
2a02c4bb | 121 | SDL_Delay(100); |
3c3c195d | 122 | |
68b2316d | 123 | game::init(); |
5f3520c8 | 124 | DPF(0, "Game initialized"); |
3c3c195d | 125 | |
68b2316d | 126 | input::init(); |
5f3520c8 | 127 | DPF(0, "Input initialized"); |
3c3c195d | 128 | |
b1d92c2f | 129 | cfg::init(); |
5f3520c8 | 130 | DPF(0, "Configs initialized"); |
b1d92c2f | 131 | |
5f3520c8 | 132 | DPF(0, "Initialization Complete"); |
2a02c4bb | 133 | |
e2b1ddcd | 134 | s_bUpdateInitialized = true; |
ad9f1fb6 | 135 | } |
2a02c4bb | 136 | void updatesClean() |
44b079f8 | 137 | { |
2a02c4bb | 138 | DPF(0, "Update Thread Cleaning"); |
44b079f8 | 139 | |
b1d92c2f PG |
140 | cfg::clean(); |
141 | ||
68b2316d | 142 | input::clean(); |
617dcc71 | 143 | |
68b2316d | 144 | game::clean(); |
2a02c4bb PG |
145 | } |
146 | ||
147 | void drawInit() | |
148 | { | |
149 | graphics::init(); | |
150 | DPF(0, "Graphics initialized"); | |
151 | ||
e2b1ddcd | 152 | s_bDrawInitialized = true; |
2a02c4bb | 153 | |
e2b1ddcd | 154 | while(!s_bUpdateInitialized) |
2a02c4bb PG |
155 | SDL_Delay(100); |
156 | } | |
2a02c4bb PG |
157 | void drawClean() |
158 | { | |
159 | DPF(0, "Draw Thread Cleaning"); | |
44b079f8 | 160 | |
68b2316d | 161 | graphics::clean(); |
44b079f8 PG |
162 | } |
163 | ||
617dcc71 PG |
164 | /// ***** Private Methods ***** |
165 | ||
ad9f1fb6 PG |
166 | void run() |
167 | { | |
e2b1ddcd | 168 | s_bIsRunning = true; |
d279b77b | 169 | |
e2b1ddcd PG |
170 | SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL); |
171 | SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL); | |
d279b77b | 172 | |
e2b1ddcd PG |
173 | SDL_WaitThread(s_pUpdatesThread, NULL); |
174 | SDL_WaitThread(s_pDrawThread, NULL); | |
ad9f1fb6 PG |
175 | } |
176 | ||
177 | void updateFPSCounters() | |
178 | { | |
179 | // Check if a second has passed to recalculate UPS and FPS | |
e2b1ddcd | 180 | if (tickCountMicro() - s_micLastSecond >= 1000000) |
ad9f1fb6 | 181 | { |
e2b1ddcd PG |
182 | s_iUPS = s_iUpdateCount; |
183 | s_iFPS = s_iDrawCount; | |
ce53f20f PG |
184 | |
185 | // NOT thread safe, but they're estimates anyways | |
e2b1ddcd PG |
186 | s_iUpdateCount -= s_iUPS; |
187 | s_iDrawCount -= s_iFPS; | |
ad9f1fb6 | 188 | |
e2b1ddcd | 189 | s_micLastSecond = tickCountMicro(); |
44b079f8 | 190 | |
c46074c1 PG |
191 | if(cfg::showFPS()) |
192 | { | |
e2b1ddcd | 193 | cout << "fps:\t" << s_iFPS << endl; |
c46074c1 PG |
194 | } |
195 | if(cfg::showUPS()) | |
196 | { | |
e2b1ddcd | 197 | cout << "ups:\t" << s_iUPS << endl; |
c46074c1 | 198 | } |
ad9f1fb6 PG |
199 | } |
200 | } | |
201 | ||
68b2316d | 202 | void handleInput() |
ad9f1fb6 | 203 | { |
ce53f20f | 204 | input::update(); |
f7b3b2eb | 205 | |
2b8199e7 | 206 | cfg::handleInput(); |
ce53f20f | 207 | game::handleInput(); |
ad9f1fb6 | 208 | |
ce53f20f | 209 | if(cfg::endGame()) |
e2b1ddcd | 210 | s_bIsRunning = false; |
ad9f1fb6 PG |
211 | } |
212 | ||
e2b1ddcd | 213 | void update(float fTimeStep) |
ad9f1fb6 | 214 | { |
e2b1ddcd PG |
215 | game::update(fTimeStep); |
216 | s_iUpdateCount++; | |
ad9f1fb6 PG |
217 | } |
218 | ||
219 | void draw() | |
220 | { | |
ce53f20f | 221 | game::draw(); |
d279b77b | 222 | |
4a0863f3 PG |
223 | SDL_PumpEvents(); // has to be on the Draw thread for the Windows API |
224 | ||
ce53f20f PG |
225 | SDL_GL_SwapBuffers(); |
226 | ||
227 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
ad9f1fb6 | 228 | |
e2b1ddcd | 229 | s_iDrawCount++; |
ce53f20f | 230 | } |
d279b77b | 231 | |
ce53f20f PG |
232 | int startUpdateThread(void*) |
233 | { | |
2a02c4bb PG |
234 | updatesInit(); |
235 | ||
e2b1ddcd | 236 | s_micLastSecond = tickCountMicro(); |
8baa4b2f | 237 | |
e2b1ddcd | 238 | while(s_bIsRunning) |
ce53f20f | 239 | { |
8baa4b2f PG |
240 | MICRO time = tickCountMicro(); |
241 | handleInput(); | |
e2b1ddcd | 242 | update(s_fGameStep); |
8baa4b2f PG |
243 | |
244 | updateFPSCounters(); | |
245 | time = tickCountMicro() - time; | |
d279b77b | 246 | |
e2b1ddcd PG |
247 | float wait = (1000000.0 / s_iTargetUPS - time); |
248 | s_fAccUpdateWait += 0 < wait ? wait : 0; | |
d279b77b | 249 | |
e2b1ddcd | 250 | if(s_iMinWaitMicro < s_fAccUpdateWait) |
8baa4b2f | 251 | { |
fa885913 | 252 | int iWaits = (int)(s_fAccUpdateWait / s_iMinWaitMicro); |
e2b1ddcd PG |
253 | s_fAccUpdateWait -= iWaits * s_iMinWaitMicro; |
254 | SDL_Delay(iWaits * s_iMinWaitMilli); | |
8baa4b2f | 255 | } |
ce53f20f | 256 | } |
ad9f1fb6 | 257 | |
2a02c4bb PG |
258 | updatesClean(); |
259 | ||
ce53f20f PG |
260 | return 0; |
261 | } | |
ad9f1fb6 | 262 | |
ce53f20f PG |
263 | int startDrawThread(void*) |
264 | { | |
2a02c4bb PG |
265 | drawInit(); |
266 | ||
e2b1ddcd | 267 | while(s_bIsRunning) |
ce53f20f | 268 | { |
fa885913 PG |
269 | MICRO time = tickCountMicro(); |
270 | draw(); | |
271 | time = tickCountMicro() - time; | |
272 | ||
273 | float wait = (1000000.0 / s_iTargetFPS - time); | |
274 | s_fAccDrawWait += 0 < wait ? wait : 0; | |
275 | ||
276 | if(s_iMinWaitMicro < s_fAccDrawWait) | |
277 | { | |
278 | int iWaits = (int)(s_fAccDrawWait / s_iMinWaitMicro); | |
279 | s_fAccDrawWait -= iWaits * s_iMinWaitMicro; | |
280 | SDL_Delay(iWaits * s_iMinWaitMilli); | |
281 | } | |
ce53f20f | 282 | } |
25d84412 | 283 | |
2a02c4bb PG |
284 | drawClean(); |
285 | ||
ce53f20f | 286 | return 0; |
ad9f1fb6 | 287 | } |