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 );
42 void updateFPSCounters();
48 /// ***** Private Variables *****
50 // variable used to determine if it is time to shutdown
53 /* Values for the main game loop
54 * target_UPS := the amount of updates that is wanted in one second
55 * last_Update := stores the time of the last update
56 * update_Sum := used to determine the updates needs for this run
58 * ups := updates per second for the last second
59 * fps := frames per second for the last second
60 * update_Count := counts this seconds updates
61 * draw_Count := counts this seconds draws
62 * last_Second := stores the time of the last second, used for ups and fps
65 long int last_Block_Update;
69 int update_Count, draw_Count;
73 float target_time_steps_per_second = 1000;
75 float time_steps_Count;
78 // experiment with rolling averages
88 /// ***** MAIN Method *****
89 int main(int argc, char** args)
97 /// ***** Initializers/Cleaners *****
104 DPF(0, "Graphics initialized");
107 DPF(0, "Game initialized");
110 DPF(0, "Input initialized");
113 DPF(0, "Configs initialized");
115 DPF(0, "Initialization Complete");
120 DPF(0, "Cleaning up");
131 /// ***** Private Methods *****
136 last_Block_Update = tickCountMicro();
137 last_Second = tickCountMicro();
143 time = tickCountMicro();
147 time = tickCountMicro() - time;
149 rRun = (rRun * (num-1) + time) / num;
151 //cout << "total:\t" << total << endl;
152 //cout << "rR:\t" << rRun << endl;
159 long int start = tickCountMicro();
160 long int diff = start - last_Block_Update;
162 //cout << "Block" << endl;
164 // Calculate the updates that should be run for the next draw
165 update_Sum += (float)(diff * target_UPS) / 1000000;
167 // insures the float to int cast is done once.
168 int iupdate_sum = (int)update_Sum;
170 // TODO the main run loop needs to be tested and pruned
173 // Calculate a time step that spreads the updates out as much as
174 // possible used because really quick updates are nearly wasted
176 //float time_step = ((float)diff) / iupdate_sum / 1000;
177 //float time_step = 1000 / (100000 / rUpdate) / iupdate_sum;
178 //float time_step = 1000 / ups / iupdate_sum;
179 float time_step = 10;
182 for (int i = 0; i < iupdate_sum; i++)
188 // remove the updates that were run from the sum
189 update_Sum -= iupdate_sum;
190 last_Block_Update = tickCountMicro();
194 void updateFPSCounters()
196 // Check if a second has passed to recalculate UPS and FPS
197 if (tickCountMicro() - last_Second >= 1000000)
201 times = time_steps_Count;
204 time_steps_Count = 0;
206 last_Second = tickCountMicro();
209 cout << "ups:\t" << ups << endl;
210 cout << "fps:\t" << fps << endl;
211 cout << "times:\t" << times << endl;
220 time = tickCountMicro();
227 time = tickCountMicro() - time;
229 rInput = (rInput*(num-1) + time) /num;
234 void update(float time_step)
239 time_steps_Count += time_step;
241 time = tickCountMicro();
242 game::update(time_step);
243 time = tickCountMicro() - time;
245 rUpdate = (rUpdate * (num-1) + time) / num;
247 //cout << "ts:\t" << time_step << endl;
248 //cout << "ru:\t" << rUpdate << endl;
259 time = tickCountMicro();
262 SDL_GL_SwapBuffers();
264 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
267 time = tickCountMicro() - time;
269 rDraw = (rDraw*(num-1) + time) /num;
271 //cout << "rd:\t" << rDraw << endl;