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/>.
23 #include "handleSignal.h"
28 #include "graphics/graphics.h"
29 #include "input/inputManager.h"
30 #include "config/config.h"
32 /// ***** Private Method Headers *****
34 static void mainInit();
35 static void mainClean();
37 static void updatesInit();
38 static void updatesClean();
40 static void drawInit();
41 static void drawClean();
45 static void updateFPSCounters();
47 static void handleInput();
48 static void update(float);
51 static int startUpdateThread(void*);
52 static int startDrawThread(void*);
54 /// ***** Private Variables *****
56 // variable used to determine if it is time to shutdown
57 static bool s_bIsRunning;
59 // Minimum possible wait times by used library
60 static const int s_iMinWaitMilli = 20;
61 static const int s_iMinWaitMicro = s_iMinWaitMilli * 1000;
63 /* Values for the main game loop
65 * s_iTargetUPS := the amount of updates that is wanted in one second
66 * s_fAccUpdateWait := the accumulated wait time for the update sleeps
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
74 static int s_iTargetUPS = 100;
75 static float s_fAccUpdateWait = 0;
77 static int s_iTargetFPS = 100;
78 static float s_fAccDrawWait = 0;
80 static int s_iUPS, s_iFPS;
81 static int s_iUpdateCount, s_iDrawCount;
82 static MICRO s_micLastSecond;
84 static const float s_fGameStep = 10;
86 static bool s_bUpdateInitialized = false;
87 static bool s_bDrawInitialized = false;
89 /// ***** MAIN Method *****
90 int main(int argc, char** args)
99 /// ***** Initializers/Cleaners *****
114 while(!s_bDrawInitialized)
118 DPF(0, "Game initialized");
121 DPF(0, "Input initialized");
124 DPF(0, "Configs initialized");
126 DPF(0, "Initialization Complete");
128 s_bUpdateInitialized = true;
132 DPF(0, "Update Thread Cleaning");
144 DPF(0, "Graphics initialized");
146 s_bDrawInitialized = true;
148 while(!s_bUpdateInitialized)
153 DPF(0, "Draw Thread Cleaning");
158 /// ***** Private Methods *****
164 SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL);
165 SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL);
167 SDL_WaitThread(s_pUpdatesThread, NULL);
168 SDL_WaitThread(s_pDrawThread, NULL);
171 void updateFPSCounters()
173 // Check if a second has passed to recalculate UPS and FPS
174 if (tickCountMicro() - s_micLastSecond >= 1000000)
176 s_iUPS = s_iUpdateCount;
177 s_iFPS = s_iDrawCount;
179 // NOT thread safe, but they're estimates anyways
180 s_iUpdateCount -= s_iUPS;
181 s_iDrawCount -= s_iFPS;
183 s_micLastSecond = tickCountMicro();
187 cout << "fps:\t" << s_iFPS << endl;
191 cout << "ups:\t" << s_iUPS << endl;
204 s_bIsRunning = false;
207 void update(float fTimeStep)
209 game::update(fTimeStep);
217 SDL_PumpEvents(); // has to be on the Draw thread for the Windows API
219 SDL_GL_SwapBuffers();
221 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
226 int startUpdateThread(void*)
230 s_micLastSecond = tickCountMicro();
234 MICRO time = tickCountMicro();
239 time = tickCountMicro() - time;
241 float wait = (1000000.0 / s_iTargetUPS - time);
242 s_fAccUpdateWait += 0 < wait ? wait : 0;
244 if(s_iMinWaitMicro < s_fAccUpdateWait)
246 int iWaits = (int)(s_fAccUpdateWait / s_iMinWaitMicro);
247 s_fAccUpdateWait -= iWaits * s_iMinWaitMicro;
248 SDL_Delay(iWaits * s_iMinWaitMilli);
257 int startDrawThread(void*)
263 MICRO time = tickCountMicro();
265 time = tickCountMicro() - time;
267 float wait = (1000000.0 / s_iTargetFPS - time);
268 s_fAccDrawWait += 0 < wait ? wait : 0;
270 if(s_iMinWaitMicro < s_fAccDrawWait)
272 int iWaits = (int)(s_fAccDrawWait / s_iMinWaitMicro);
273 s_fAccDrawWait -= iWaits * s_iMinWaitMicro;
274 SDL_Delay(iWaits * s_iMinWaitMilli);