added an interupt handler has SIGINT was being ignored... ?
[physics.git] / src / main.cpp
1 #include <GL/gl.h>
2 #include <GL/glu.h>
3 #include <SDL/SDL.h>
4
5 #include "debug.h"
6 #include "handleSignal.h"
7
8 #include "game.h"
9 #include "ticks.h"
10
11 #include "graphics/graphics.h"
12 #include "input/inputManager.h"
13
14
15
16 /// ***** Private Method Headers *****
17 void init();
18
19 void sighandler( int sig );
20
21 void run();
22 void cleanUp();
23
24 void blockUpdate();
25 void updateFPSCounters();
26
27 void input();
28 void update(float);
29 void draw();
30
31
32 /// ***** MAIN Method *****
33 int main()
34 {
35     init();
36     run();
37     cleanUp();
38     return 0;
39 }
40
41
42 /// ***** Private Variables *****
43
44 // variable used to determine if it is time to shutdown
45 bool is_Running;
46
47 /* Values for the main game loop
48  * target_UPS := the amount of updates that is wanted in one second
49  * last_Update := stores the time of the last update
50  * update_Sum := used to determine the updates needs for this run
51
52  * ups := updates per second for the last second
53  * fps := frames per second for the last second
54  * update_Count := counts this seconds updates
55  * draw_Count  := counts this seconds draws
56  * last_Second := stores the time of the last second, used for ups and fps
57  */
58 int target_UPS = 250;
59 long int last_Block_Update;
60 float update_Sum = 0;
61
62 int ups, fps;
63 int update_Count, draw_Count;
64 long int last_Second;
65
66
67 /// ***** Private Methods *****
68 void init()
69 {
70     installSignal();
71
72     graphicsInit();
73
74     gameInit();
75
76 #ifdef DEBUGGING
77     cout << "Initialization Complete" << endl;
78 #endif
79 }
80
81 void cleanUp()
82 {
83 #ifdef DEBUGGING
84     cout << "Cleaning up" << endl;
85 #endif
86
87     gameClean();
88
89     graphicsCleanUp();
90 }
91
92 void 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
106 void 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
135 void 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();
146
147         //cout << "ups:\t" << ups << endl;
148         //cout << "fps:\t" << fps << endl;
149     }
150 }
151
152 void input()
153 {
154     inputUpdate();
155
156     gameInput();
157
158     if(wasReleased(SDLK_ESCAPE))
159         is_Running = false;
160 }
161
162 void update(float time_step)
163 {
164     update_Count++;
165
166     gameUpdate(time_step);
167 }
168
169 void 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 }