setup config to be one file
[physics.git] / src / main.cpp
CommitLineData
e68f847b
PG
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
ad9f1fb6
PG
18#include <GL/gl.h>
19#include <GL/glu.h>
20#include <SDL/SDL.h>
21
f7b3b2eb 22#include "debug.h"
7cbd4505 23#include "handleSignal.h"
f7b3b2eb 24
ad9f1fb6
PG
25#include "game.h"
26#include "ticks.h"
44b079f8
PG
27
28#include "graphics/graphics.h"
f7b3b2eb 29#include "input/inputManager.h"
b1d92c2f 30#include "config/config.h"
ad9f1fb6 31
ad9f1fb6 32/// ***** Private Method Headers *****
617dcc71 33
ad9f1fb6
PG
34void init();
35
7cbd4505
PG
36void sighandler( int sig );
37
ad9f1fb6 38void run();
617dcc71 39void clean();
ad9f1fb6
PG
40
41void blockUpdate();
42void updateFPSCounters();
43
68b2316d 44void handleInput();
ad9f1fb6
PG
45void update(float);
46void draw();
47
ad9f1fb6
PG
48/// ***** Private Variables *****
49
50// variable used to determine if it is time to shutdown
51bool 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 */
64int target_UPS = 250;
65long int last_Block_Update;
66float update_Sum = 0;
67
68int ups, fps;
69int update_Count, draw_Count;
70long int last_Second;
71
72
617dcc71
PG
73/// ***** MAIN Method *****
74int main()
75{
76 init();
77 run();
78 clean();
79 return 0;
80}
81
82/// ***** Initializers/Cleaners *****
83
ad9f1fb6
PG
84void init()
85{
7cbd4505
PG
86 installSignal();
87
68b2316d 88 graphics::init();
ad9f1fb6 89
68b2316d 90 game::init();
ad9f1fb6 91
68b2316d 92 input::init();
617dcc71 93
b1d92c2f
PG
94 cfg::init();
95
f7b3b2eb 96#ifdef DEBUGGING
ad9f1fb6 97 cout << "Initialization Complete" << endl;
f7b3b2eb 98#endif
ad9f1fb6
PG
99}
100
617dcc71 101void clean()
44b079f8
PG
102{
103#ifdef DEBUGGING
104 cout << "Cleaning up" << endl;
105#endif
106
b1d92c2f
PG
107 cfg::clean();
108
68b2316d 109 input::clean();
617dcc71 110
68b2316d 111 game::clean();
44b079f8 112
68b2316d 113 graphics::clean();
44b079f8
PG
114}
115
617dcc71
PG
116/// ***** Private Methods *****
117
ad9f1fb6
PG
118void run()
119{
120 is_Running = true;
121 last_Block_Update = tickCountMicro();
122 last_Second = tickCountMicro();
123
124 while(is_Running)
125 {
126 blockUpdate();
127 updateFPSCounters();
128 draw();
129 }
130}
131
ad9f1fb6
PG
132void blockUpdate()
133{
134 long int start = tickCountMicro();
135
136 // Calculate the updates that should be run for the next draw
137 update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS);
138
139 // insures the float to int cast is done once.
140 int iupdate_sum = (int)update_Sum;
141
142 // TODO the main run loop needs to be tested and pruned
143 if (iupdate_sum > 0)
144 {
145 // Calculate a time step that spreads the updates out as much as possible
146 // used because really quick updates are nearly wasted
147 float time_step = ((float)(start - last_Block_Update)) / iupdate_sum;
148
149 // run the updates
150 for (int i = 1; i <= iupdate_sum; i++)
151 {
68b2316d 152 handleInput();
ad9f1fb6
PG
153 update(time_step*i / 1000);
154 }
155 // remove the updates that where run from the sum
156 update_Sum -= iupdate_sum;
157 last_Block_Update = tickCountMicro();
158 }
159}
160
161void updateFPSCounters()
162{
163 // Check if a second has passed to recalculate UPS and FPS
164 if (tickCountMicro() - last_Second >= 1000000)
165 {
166 ups = update_Count;
167 fps = draw_Count;
168 update_Count = 0;
169 draw_Count = 0;
170
171 last_Second = tickCountMicro();
44b079f8 172
7adc59fe
PG
173#ifdef FPSUPS
174 cout << "ups:\t" << ups << endl;
175 cout << "fps:\t" << fps << endl;
176#endif
ad9f1fb6
PG
177 }
178}
179
68b2316d 180void handleInput()
ad9f1fb6 181{
68b2316d 182 input::update();
f7b3b2eb 183
68b2316d 184 game::input();
ad9f1fb6 185
b1d92c2f 186 if(cfg::endGame())
ad9f1fb6 187 is_Running = false;
ad9f1fb6
PG
188}
189
190void update(float time_step)
191{
192 update_Count++;
193
68b2316d 194 game::update(time_step);
ad9f1fb6
PG
195}
196
197void draw()
198{
199 draw_Count++;
200
68b2316d 201 game::draw();
ad9f1fb6
PG
202
203 SDL_GL_SwapBuffers();
204
205 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
206}