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