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