input system setup, debug header started
[physics.git] / src / main.cpp
CommitLineData
ad9f1fb6
PG
1#include <GL/gl.h>
2#include <GL/glu.h>
3#include <SDL/SDL.h>
4
ad9f1fb6
PG
5#include <vector>
6using std::vector;
7
f7b3b2eb
PG
8#include "debug.h"
9
ad9f1fb6
PG
10#include "game.h"
11#include "ticks.h"
12#include "graphics.h"
f7b3b2eb 13#include "input/inputManager.h"
ad9f1fb6
PG
14
15
16/// ***** Private Method Headers *****
17void init();
18
19void run();
20void cleanUp();
21
22void blockUpdate();
23void updateFPSCounters();
24
25void input();
26void update(float);
27void draw();
28
29
30/// ***** MAIN Method *****
31int main()
32{
33 init();
34 run();
35 cleanUp();
36 return 0;
37}
38
39
40/// ***** Private Variables *****
41
42// variable used to determine if it is time to shutdown
43bool is_Running;
44
45/* Values for the main game loop
46 * target_UPS := the amount of updates that is wanted in one second
47 * last_Update := stores the time of the last update
48 * update_Sum := used to determine the updates needs for this run
49
50 * ups := updates per second for the last second
51 * fps := frames per second for the last second
52 * update_Count := counts this seconds updates
53 * draw_Count := counts this seconds draws
54 * last_Second := stores the time of the last second, used for ups and fps
55 */
56int target_UPS = 250;
57long int last_Block_Update;
58float update_Sum = 0;
59
60int ups, fps;
61int update_Count, draw_Count;
62long int last_Second;
63
64
65/// ***** Private Methods *****
66void init()
67{
68 graphicsInit();
69
70 gameInit();
71
72 // TODO
73 // add a game state
74
f7b3b2eb 75#ifdef DEBUGGING
ad9f1fb6 76 cout << "Initialization Complete" << endl;
f7b3b2eb 77#endif
ad9f1fb6
PG
78
79 // create starting entities
80
f7b3b2eb 81#ifdef DEBUGGING
ad9f1fb6 82 cout << "World Created" << endl;
f7b3b2eb 83#endif
ad9f1fb6
PG
84}
85
86void run()
87{
88 is_Running = true;
89 last_Block_Update = tickCountMicro();
90 last_Second = tickCountMicro();
91
92 while(is_Running)
93 {
94 blockUpdate();
95 updateFPSCounters();
96 draw();
97 }
98}
99
100void cleanUp()
101{
102 gameClean();
103}
104
105void blockUpdate()
106{
107 long int start = tickCountMicro();
108
109 // Calculate the updates that should be run for the next draw
110 update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS);
111
112 // insures the float to int cast is done once.
113 int iupdate_sum = (int)update_Sum;
114
115 // TODO the main run loop needs to be tested and pruned
116 if (iupdate_sum > 0)
117 {
118 // Calculate a time step that spreads the updates out as much as possible
119 // used because really quick updates are nearly wasted
120 float time_step = ((float)(start - last_Block_Update)) / iupdate_sum;
121
122 // run the updates
123 for (int i = 1; i <= iupdate_sum; i++)
124 {
125 input();
126 update(time_step*i / 1000);
127 }
128 // remove the updates that where run from the sum
129 update_Sum -= iupdate_sum;
130 last_Block_Update = tickCountMicro();
131 }
132}
133
134void updateFPSCounters()
135{
136 // Check if a second has passed to recalculate UPS and FPS
137 if (tickCountMicro() - last_Second >= 1000000)
138 {
139 ups = update_Count;
140 fps = draw_Count;
141 update_Count = 0;
142 draw_Count = 0;
143
144 last_Second = tickCountMicro();
145 }
146}
147
148void input()
149{
f7b3b2eb
PG
150 inputUpdate();
151
ad9f1fb6
PG
152 gameInput();
153
f7b3b2eb 154 if(isPressed(SDLK_ESCAPE))
ad9f1fb6 155 is_Running = false;
ad9f1fb6
PG
156}
157
158void update(float time_step)
159{
160 update_Count++;
161
162 gameUpdate(time_step);
163}
164
165void draw()
166{
167 draw_Count++;
168
169 gameDraw();
170
171 SDL_GL_SwapBuffers();
172
173 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
174}