2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
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.
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.
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/>.
24 #include "handleSignal.h"
29 #include "graphics/graphics.h"
30 #include "input/inputManager.h"
31 #include "config/config.h"
33 /// ***** Private Method Headers *****
35 static void mainInit();
36 static void mainClean();
38 static void updatesInit();
39 static void updatesClean();
41 static void drawInit();
42 static void drawClean();
46 static void updateFPSCounters();
48 static void handleInput();
49 static void update(float);
52 static int startUpdateThread(void*);
53 static int startDrawThread(void*);
55 /// ***** Private Variables *****
57 // variable used to determine if it is time to shutdown
58 static bool s_bIsRunning;
60 // Minimum possible wait times by used library
61 static const int s_iMinWaitMilli = 20;
62 static const int s_iMinWaitMicro = s_iMinWaitMilli * 1000;
64 /* Values for the main game loop
66 * s_iTargetUPS := the amount of updates that is wanted in one second
67 * s_fAccUpdateWait := the accumulated wait time for the update sleeps
69 * s_iUPS := updates per second for the last second
70 * s_iFPS := frames per second for the last second
71 * s_iUpdateCount := counts this seconds updates
72 * s_iDrawCount := counts this seconds draws
73 * s_micLastSecond := stores the time of the last second, used for ups and fps
75 static int s_iTargetUPS = 100;
76 static float s_fAccUpdateWait = 0;
78 static int s_iTargetFPS = 100;
79 static float s_fAccDrawWait = 0;
81 static int s_iUPS, s_iFPS;
82 static int s_iUpdateCount, s_iDrawCount;
83 static MICRO s_micLastSecond;
85 static const float s_fGameStep = 10;
87 static bool s_bUpdateInitialized = false;
88 static bool s_bDrawInitialized = false;
90 /// ***** MAIN Method *****
91 int main(int argc, char** args)
100 /// ***** Initializers/Cleaners *****
115 while(!s_bDrawInitialized)
119 DPF(0, "Game initialized");
122 DPF(0, "Input initialized");
125 DPF(0, "Configs initialized");
127 DPF(0, "Initialization Complete");
129 s_bUpdateInitialized = true;
133 DPF(0, "Update Thread Cleaning");
145 DPF(0, "Graphics initialized");
147 s_bDrawInitialized = true;
149 while(!s_bUpdateInitialized)
154 DPF(0, "Draw Thread Cleaning");
159 /// ***** Private Methods *****
165 SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL);
166 SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL);
168 SDL_WaitThread(s_pUpdatesThread, NULL);
169 SDL_WaitThread(s_pDrawThread, NULL);
172 void updateFPSCounters()
174 // Check if a second has passed to recalculate UPS and FPS
175 if (tickCountMicro() - s_micLastSecond >= 1000000)
177 s_iUPS = s_iUpdateCount;
178 s_iFPS = s_iDrawCount;
180 // NOT thread safe, but they're estimates anyways
181 s_iUpdateCount -= s_iUPS;
182 s_iDrawCount -= s_iFPS;
184 s_micLastSecond = tickCountMicro();
188 cout << "fps:\t" << s_iFPS << endl;
192 cout << "ups:\t" << s_iUPS << endl;
205 s_bIsRunning = false;
208 void update(float fTimeStep)
210 game::update(fTimeStep);
218 SDL_PumpEvents(); // has to be on the Draw thread for the Windows API
220 SDL_GL_SwapBuffers();
222 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
227 int startUpdateThread(void*)
231 s_micLastSecond = tickCountMicro();
235 MICRO time = tickCountMicro();
240 time = tickCountMicro() - time;
242 float wait = (1000000.0 / s_iTargetUPS - time);
243 s_fAccUpdateWait += 0 < wait ? wait : 0;
245 if(s_iMinWaitMicro < s_fAccUpdateWait)
247 int iWaits = (int)(s_fAccUpdateWait / s_iMinWaitMicro);
248 s_fAccUpdateWait -= iWaits * s_iMinWaitMicro;
249 SDL_Delay(iWaits * s_iMinWaitMilli);
258 int startDrawThread(void*)
264 MICRO time = tickCountMicro();
266 time = tickCountMicro() - time;
268 float wait = (1000000.0 / s_iTargetFPS - time);
269 s_fAccDrawWait += 0 < wait ? wait : 0;
271 if(s_iMinWaitMicro < s_fAccDrawWait)
273 int iWaits = (int)(s_fAccDrawWait / s_iMinWaitMicro);
274 s_fAccDrawWait -= iWaits * s_iMinWaitMicro;
275 SDL_Delay(iWaits * s_iMinWaitMilli);