added rolling times
[physics.git] / src / main.cpp
1 /*
2  *  Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include <GL/gl.h>
19 #include <GL/glu.h>
20 #include <SDL/SDL.h>
21
22 #include "debug.h"
23 #include "handleSignal.h"
24
25 #include "game.h"
26 #include "ticks.h"
27
28 #include "graphics/graphics.h"
29 #include "input/inputManager.h"
30 #include "config/config.h"
31
32 /// ***** Private Method Headers *****
33
34 void init();
35
36 void sighandler( int sig );
37
38 void run();
39 void clean();
40
41 void blockUpdate();
42 void updateFPSCounters();
43
44 void handleInput();
45 void update(float);
46 void draw();
47
48 /// ***** Private Variables *****
49
50 // variable used to determine if it is time to shutdown
51 bool is_Running;
52
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
57
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
63  */
64 int target_UPS = 100;
65 long int last_Block_Update;
66 float update_Sum = 0;
67
68 int ups, fps;
69 int update_Count, draw_Count;
70 long int last_Second;
71
72
73 float target_time_steps_per_second = 100;
74
75
76 // experiment with rolling averages
77 float rUpdate = 100;
78 float rDraw = 100;
79 float rRun = 100;
80 float num = 10;
81
82
83 /// ***** MAIN Method *****
84 int main()
85 {
86     init();
87     run();
88     clean();
89     return 0;
90 }
91
92 /// ***** Initializers/Cleaners *****
93
94 void init()
95 {
96     installSignal();
97
98     graphics::init();
99
100     game::init();
101
102     input::init();
103
104     cfg::init();
105
106 #ifdef DEBUGGING
107     cout << "Initialization Complete" << endl;
108 #endif
109 }
110
111 void clean()
112 {
113 #ifdef DEBUGGING
114     cout << "Cleaning up" << endl;
115 #endif
116
117     cfg::clean();
118
119     input::clean();
120
121     game::clean();
122
123     graphics::clean();
124 }
125
126 /// ***** Private Methods *****
127
128 void run()
129 {
130     is_Running = true;
131     last_Block_Update = tickCountMicro();
132     last_Second = tickCountMicro();
133
134     while(is_Running)
135     {
136         long int time;
137
138         time = tickCountMicro();
139             blockUpdate();
140             updateFPSCounters();
141             draw();
142         time = tickCountMicro() - time;
143
144         rRun = (rRun * (num-1) + time) / num;
145
146         cout << "rR:\t" << rRun << endl;
147     }
148 }
149
150 void blockUpdate()
151 {
152     long int start = tickCountMicro();
153
154     cout << "Block" << endl;
155
156     // Calculate the updates that should be run for the next draw
157     update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS);
158
159     // insures the float to int cast is done once.
160     int iupdate_sum = (int)update_Sum;
161
162     // TODO the main run loop needs to be tested and pruned
163     if (iupdate_sum > 0)
164     {
165         // Calculate a time step that spreads the updates out as much as possible
166         // used because really quick updates are nearly wasted
167         float time_step = ((float)(start - last_Block_Update)) / iupdate_sum;
168
169         // run the updates
170         for (int i = 1; i <= iupdate_sum; i++)
171         {
172             handleInput();
173             update(time_step / 1000);
174         }
175         // remove the updates that where run from the sum
176         update_Sum -= iupdate_sum;
177         last_Block_Update = tickCountMicro();
178     }
179 }
180
181 void updateFPSCounters()
182 {
183     // Check if a second has passed to recalculate UPS and FPS
184     if (tickCountMicro() - last_Second >= 1000000)
185     {
186         ups = update_Count;
187         fps = draw_Count;
188         update_Count = 0;
189         draw_Count = 0;
190
191         last_Second = tickCountMicro();
192
193 #ifdef FPSUPS
194         cout << "ups:\t" << ups << endl;
195         cout << "fps:\t" << fps << endl;
196 #endif
197     }
198 }
199
200 void handleInput()
201 {
202     input::update();
203
204     game::handleInput();
205
206     if(cfg::endGame())
207         is_Running = false;
208 }
209
210 void update(float time_step)
211 {
212     long int time;
213
214     update_Count++;
215
216     time = tickCountMicro();
217         game::update(time_step);
218     time = tickCountMicro() - time;
219
220     rUpdate = (rUpdate * (num-1) + time) / num;
221
222     cout << "ru:\t" << rUpdate << endl;
223     cout << "ts:\t" << time_step << endl;
224 }
225
226 void draw()
227 {
228     long int time;
229
230     draw_Count++;
231
232     time = tickCountMicro();
233         game::draw();
234
235         SDL_GL_SwapBuffers();
236
237         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
238
239         //SDL_Delay(50);
240     time = tickCountMicro() - time;
241
242     rDraw = (rDraw*(num-1) + time) /num;
243
244     cout << "rd:\t" << rDraw << endl;
245 }