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"
31 /// ***** Private Method Headers *****
35 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
54 * last_Update := stores the time of the last update
55 * update_Sum := used to determine the updates needs for this run
57 * ups := updates per second for the last second
58 * fps := frames per second for the last second
59 * update_Count := counts this seconds updates
60 * draw_Count := counts this seconds draws
61 * last_Second := stores the time of the last second, used for ups and fps
64 long int last_Block_Update;
68 int update_Count, draw_Count;
72 /// ***** MAIN Method *****
81 /// ***** Initializers/Cleaners *****
94 cout << "Initialization Complete" << endl;
101 cout << "Cleaning up" << endl;
111 /// ***** Private Methods *****
116 last_Block_Update = tickCountMicro();
117 last_Second = tickCountMicro();
129 long int start = tickCountMicro();
131 // Calculate the updates that should be run for the next draw
132 update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS);
134 // insures the float to int cast is done once.
135 int iupdate_sum = (int)update_Sum;
137 // TODO the main run loop needs to be tested and pruned
140 // Calculate a time step that spreads the updates out as much as possible
141 // used because really quick updates are nearly wasted
142 float time_step = ((float)(start - last_Block_Update)) / iupdate_sum;
145 for (int i = 1; i <= iupdate_sum; i++)
148 update(time_step*i / 1000);
150 // remove the updates that where run from the sum
151 update_Sum -= iupdate_sum;
152 last_Block_Update = tickCountMicro();
156 void updateFPSCounters()
158 // Check if a second has passed to recalculate UPS and FPS
159 if (tickCountMicro() - last_Second >= 1000000)
166 last_Second = tickCountMicro();
169 cout << "ups:\t" << ups << endl;
170 cout << "fps:\t" << fps << endl;
181 if(input::wasReleased(SDLK_ESCAPE))
185 void update(float time_step)
189 game::update(time_step);
198 SDL_GL_SwapBuffers();
200 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);