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 *****
36 void sighandler( int sig );
41 void updateFPSCounters();
47 /// ***** Private Variables *****
49 // variable used to determine if it is time to shutdown
52 /* Values for the main game loop
53 * target_UPS := the amount of updates that is wanted in one second
55 * ups := updates per second for the last second
56 * fps := frames per second for the last second
57 * update_Count := counts this seconds updates
58 * draw_Count := counts this seconds draws
59 * last_Second := stores the time of the last second, used for ups and fps
62 float update_Wait = 0;
65 int update_Count, draw_Count;
68 const int min_wait_milli = 20;
69 const int min_wait_micro = min_wait_milli * 1000;
70 const float game_step = 10;
72 int startUpdateThread(void*);
73 int startDrawThread(void*);
75 /// ***** MAIN Method *****
76 int main(int argc, char** args)
84 /// ***** Initializers/Cleaners *****
91 DPF(0, "Graphics initialized");
94 DPF(0, "Game initialized");
97 DPF(0, "Input initialized");
100 DPF(0, "Configs initialized");
102 DPF(0, "Initialization Complete");
107 DPF(0, "Cleaning up");
118 /// ***** Private Methods *****
124 SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL);
126 // for some reason this needs to be on the main thread ...?
127 startDrawThread(NULL);
129 SDL_WaitThread(updates, NULL);
132 void updateFPSCounters()
134 // Check if a second has passed to recalculate UPS and FPS
135 if (tickCountMicro() - last_Second >= 1000000)
140 // NOT thread safe, but they're estimates anyways
144 last_Second = tickCountMicro();
147 cout << "ups:\t" << ups << endl;
148 cout << "fps:\t" << fps << endl;
163 void update(float time_step)
165 game::update(time_step);
173 SDL_GL_SwapBuffers();
175 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
180 int startUpdateThread(void*)
182 last_Second = tickCountMicro();
186 MICRO time = tickCountMicro();
191 time = tickCountMicro() - time;
193 float wait = (1000000.0 / target_UPS - time);
194 update_Wait += 0 < wait ? wait : 0;
196 while(min_wait_micro < update_Wait)
198 update_Wait -= min_wait_micro;
199 SDL_Delay(min_wait_milli);
206 int startDrawThread(void*)