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 *****
43 void sighandler( int sig );
48 void updateFPSCounters();
54 int startUpdateThread(void*);
55 int startDrawThread(void*);
57 /// ***** Private Variables *****
59 // variable used to determine if it is time to shutdown
62 /* Values for the main game loop
63 * target_UPS := the amount of updates that is wanted in one second
65 * ups := updates per second for the last second
66 * fps := frames per second for the last second
67 * update_Count := counts this seconds updates
68 * draw_Count := counts this seconds draws
69 * last_Second := stores the time of the last second, used for ups and fps
72 float update_Wait = 0;
75 int update_Count, draw_Count;
78 const int min_wait_milli = 20;
79 const int min_wait_micro = min_wait_milli * 1000;
80 const float game_step = 10;
82 bool updateInitialized = false;
83 bool drawInitialized = false;
85 /// ***** MAIN Method *****
86 int main(int argc, char** args)
94 /// ***** Initializers/Cleaners *****
110 while(!drawInitialized)
114 DPF(0, "Game initialized");
117 DPF(0, "Input initialized");
120 DPF(0, "Configs initialized");
122 DPF(0, "Initialization Complete");
124 updateInitialized = true;
129 DPF(0, "Update Thread Cleaning");
141 DPF(0, "Graphics initialized");
143 drawInitialized = true;
145 while(!updateInitialized)
151 DPF(0, "Draw Thread Cleaning");
156 /// ***** Private Methods *****
157 SDL_Thread* updatesThread = NULL;
158 SDL_Thread* drawThread = NULL;
164 updatesThread = SDL_CreateThread(startUpdateThread, NULL);
165 drawThread = SDL_CreateThread(startDrawThread, NULL);
167 SDL_WaitThread(updatesThread, NULL);
168 SDL_WaitThread(drawThread, NULL);
171 void updateFPSCounters()
173 // Check if a second has passed to recalculate UPS and FPS
174 if (tickCountMicro() - last_Second >= 1000000)
179 // NOT thread safe, but they're estimates anyways
183 last_Second = tickCountMicro();
186 cout << "ups:\t" << ups << endl;
187 cout << "fps:\t" << fps << endl;
202 void update(float time_step)
204 game::update(time_step);
212 SDL_PumpEvents(); // has to be on the Draw thread for the Windows API
214 SDL_GL_SwapBuffers();
216 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
221 int startUpdateThread(void*)
225 last_Second = tickCountMicro();
229 MICRO time = tickCountMicro();
234 time = tickCountMicro() - time;
236 float wait = (1000000.0 / target_UPS - time);
237 update_Wait += 0 < wait ? wait : 0;
239 while(min_wait_micro < update_Wait)
241 update_Wait -= min_wait_micro;
242 SDL_Delay(min_wait_milli);
251 int startDrawThread(void*)