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