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_iUPS, s_iFPS;
78 static int s_iUpdateCount, s_iDrawCount;
79 static MICRO s_micLastSecond;
81 static const float s_fGameStep = 10;
83 static bool s_bUpdateInitialized = false;
84 static bool s_bDrawInitialized = false;
86 /// ***** MAIN Method *****
87 int main(int argc, char** args)
96 /// ***** Initializers/Cleaners *****
111 while(!s_bDrawInitialized)
115 DPF(0, "Game initialized");
118 DPF(0, "Input initialized");
121 DPF(0, "Configs initialized");
123 DPF(0, "Initialization Complete");
125 s_bUpdateInitialized = true;
129 DPF(0, "Update Thread Cleaning");
141 DPF(0, "Graphics initialized");
143 s_bDrawInitialized = true;
145 while(!s_bUpdateInitialized)
150 DPF(0, "Draw Thread Cleaning");
155 /// ***** Private Methods *****
161 SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL);
162 SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL);
164 SDL_WaitThread(s_pUpdatesThread, NULL);
165 SDL_WaitThread(s_pDrawThread, NULL);
168 void updateFPSCounters()
170 // Check if a second has passed to recalculate UPS and FPS
171 if (tickCountMicro() - s_micLastSecond >= 1000000)
173 s_iUPS = s_iUpdateCount;
174 s_iFPS = s_iDrawCount;
176 // NOT thread safe, but they're estimates anyways
177 s_iUpdateCount -= s_iUPS;
178 s_iDrawCount -= s_iFPS;
180 s_micLastSecond = tickCountMicro();
184 cout << "fps:\t" << s_iFPS << endl;
188 cout << "ups:\t" << s_iUPS << endl;
201 s_bIsRunning = false;
204 void update(float fTimeStep)
206 game::update(fTimeStep);
214 SDL_PumpEvents(); // has to be on the Draw thread for the Windows API
216 SDL_GL_SwapBuffers();
218 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
223 int startUpdateThread(void*)
227 s_micLastSecond = tickCountMicro();
231 MICRO time = tickCountMicro();
236 time = tickCountMicro() - time;
238 float wait = (1000000.0 / s_iTargetUPS - time);
239 s_fAccUpdateWait += 0 < wait ? wait : 0;
241 if(s_iMinWaitMicro < s_fAccUpdateWait)
243 int iWaits = s_fAccUpdateWait / s_iMinWaitMicro;
244 s_fAccUpdateWait -= iWaits * s_iMinWaitMicro;
245 SDL_Delay(iWaits * s_iMinWaitMilli);
254 int startDrawThread(void*)