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/>.
29 #include "handleSignal.h"
34 #include "graphics/graphics.h"
35 #include "input/inputManager.h"
36 #include "config/config.h"
38 /// ***** Private Method Headers *****
40 static void mainInit();
41 static void mainClean();
43 static void updatesInit();
44 static void updatesClean();
46 static void drawInit();
47 static void drawClean();
51 static void updateFPSCounters();
53 static void handleInput();
54 static void update(float);
57 static int startUpdateThread(void*);
58 static int startDrawThread(void*);
60 /// ***** Private Variables *****
62 // variable used to determine if it is time to shutdown
63 static bool s_bIsRunning;
65 // Minimum possible wait times by used library
66 static const int s_iMinWaitMilli = 20;
67 static const int s_iMinWaitMicro = s_iMinWaitMilli * 1000;
69 /* Values for the main game loop
71 * s_iTargetUPS := the amount of updates that is wanted in one second
72 * s_fAccUpdateWait := the accumulated wait time for the update sleeps
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
80 static int s_iTargetUPS = 100;
81 static float s_fAccUpdateWait = 0;
83 static int s_iTargetFPS = 100;
84 static float s_fAccDrawWait = 0;
86 static int s_iUPS, s_iFPS;
87 static int s_iUpdateCount, s_iDrawCount;
88 static MICRO s_micLastSecond;
90 static const float s_fGameStep = 10;
92 static bool s_bUpdateInitialized = false;
93 static bool s_bDrawInitialized = false;
95 /// ***** MAIN Method *****
96 int main(int argc, char** args)
105 /// ***** Initializers/Cleaners *****
120 while(!s_bDrawInitialized)
124 DPF(0, "Game initialized");
127 DPF(0, "Input initialized");
130 DPF(0, "Configs initialized");
132 DPF(0, "Initialization Complete");
134 s_bUpdateInitialized = true;
138 DPF(0, "Update Thread Cleaning");
150 DPF(0, "Graphics initialized");
152 s_bDrawInitialized = true;
154 while(!s_bUpdateInitialized)
159 DPF(0, "Draw Thread Cleaning");
164 /// ***** Private Methods *****
170 SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL);
171 SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL);
173 SDL_WaitThread(s_pUpdatesThread, NULL);
174 SDL_WaitThread(s_pDrawThread, NULL);
177 void updateFPSCounters()
179 // Check if a second has passed to recalculate UPS and FPS
180 if (tickCountMicro() - s_micLastSecond >= 1000000)
182 s_iUPS = s_iUpdateCount;
183 s_iFPS = s_iDrawCount;
185 // NOT thread safe, but they're estimates anyways
186 s_iUpdateCount -= s_iUPS;
187 s_iDrawCount -= s_iFPS;
189 s_micLastSecond = tickCountMicro();
193 cout << "fps:\t" << s_iFPS << endl;
197 cout << "ups:\t" << s_iUPS << endl;
210 s_bIsRunning = false;
213 void update(float fTimeStep)
215 game::update(fTimeStep);
223 SDL_PumpEvents(); // has to be on the Draw thread for the Windows API
225 SDL_GL_SwapBuffers();
227 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
232 int startUpdateThread(void*)
236 s_micLastSecond = tickCountMicro();
240 MICRO time = tickCountMicro();
245 time = tickCountMicro() - time;
247 float wait = (1000000.0 / s_iTargetUPS - time);
248 s_fAccUpdateWait += 0 < wait ? wait : 0;
250 if(s_iMinWaitMicro < s_fAccUpdateWait)
252 int iWaits = (int)(s_fAccUpdateWait / s_iMinWaitMicro);
253 s_fAccUpdateWait -= iWaits * s_iMinWaitMicro;
254 SDL_Delay(iWaits * s_iMinWaitMilli);
263 int startDrawThread(void*)
269 MICRO time = tickCountMicro();
271 time = tickCountMicro() - time;
273 float wait = (1000000.0 / s_iTargetFPS - time);
274 s_fAccDrawWait += 0 < wait ? wait : 0;
276 if(s_iMinWaitMicro < s_fAccDrawWait)
278 int iWaits = (int)(s_fAccDrawWait / s_iMinWaitMicro);
279 s_fAccDrawWait -= iWaits * s_iMinWaitMicro;
280 SDL_Delay(iWaits * s_iMinWaitMilli);