input system setup, debug header started
[physics.git] / src / main.cpp
1 #include <GL/gl.h>
2 #include <GL/glu.h>
3 #include <SDL/SDL.h>
4
5 #include <vector>
6 using std::vector;
7
8 #include "debug.h"
9
10 #include "game.h"
11 #include "ticks.h"
12 #include "graphics.h"
13 #include "input/inputManager.h"
14
15
16 /// ***** Private Method Headers *****
17 void init();
18
19 void run();
20 void cleanUp();
21
22 void blockUpdate();
23 void updateFPSCounters();
24
25 void input();
26 void update(float);
27 void draw();
28
29
30 /// ***** MAIN Method *****
31 int 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
43 bool 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  */
56 int target_UPS = 250;
57 long int last_Block_Update;
58 float update_Sum = 0;
59
60 int ups, fps;
61 int update_Count, draw_Count;
62 long int last_Second;
63
64
65 /// ***** Private Methods *****
66 void init()
67 {
68     graphicsInit();
69
70     gameInit();
71
72     // TODO
73     // add a game state
74
75 #ifdef DEBUGGING
76     cout << "Initialization Complete" << endl;
77 #endif
78
79     // create starting entities
80
81 #ifdef DEBUGGING
82     cout << "World Created" << endl;
83 #endif
84 }
85
86 void 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
100 void cleanUp()
101 {
102     gameClean();
103 }
104
105 void 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
134 void 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
148 void input()
149 {
150     inputUpdate();
151
152     gameInput();
153
154     if(isPressed(SDLK_ESCAPE))
155         is_Running = false;
156 }
157
158 void update(float time_step)
159 {
160     update_Count++;
161
162     gameUpdate(time_step);
163 }
164
165 void 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 }