-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
328 lines (255 loc) · 7.36 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//main.c
/******************************************************************************
* MAY FIRST - A game inspired by http://en.wikipedia.org/wiki/Juno_First
*******************************************************************************
* The main control loop handles control of frame rate and calls various
* functions that make up the game, like input handling, screen updates, etc.
* Some basic mathematical functions and types are kept here as well.
******************************************************************************/
#include <stdarg.h>
#include <sys/time.h> // gettimeofday()
#include "main.h" // Flow control, program state, run modes
#include "draw_frame.h" // Renders the current game state to screen
#include "game.h" // Game related settings, game state control
#include "world.h" // Simulation, advances game state step wise
#include "ui.h" // Window settings, process_event_queue()
// GLOBAL VARIABLES ///////////////////////////////////////////////////////////
program_state_t program_state = {
.run_mode = RM_INIT,
.window_width = INITIAL_WINDOW_WIDTH,
.window_height = INITIAL_WINDOW_HEIGHT,
.next_fps_sample = 0,
.frame_times = {0}, // See:
// http://stackoverflow.com/questions/5636070/zero-an-array-in-c-code
.initial_TAS = -1,
.debug = DEBUG,
#if START_IN_FULLSCREEN
.screen_flags = SDL_OPENGL | SDL_DOUBLEBUF | SDL_FULLSCREEN,
#else
.screen_flags = SDL_OPENGL | SDL_DOUBLEBUF | SDL_RESIZABLE,
#endif
};
game_state_t game_state;
// COMMON HELPERS /////////////////////////////////////////////////////////////
void debugf( bool_t do_print, char* format_string, ... )
{
#if DEBUG
if (do_print) {
va_list argp;
va_start( argp, format_string );
vprintf( format_string, argp );
va_end(argp);
}
#endif
}
/* error_quit()
* When an error occurs, this function is used to terminate the program with
* a useful error message.
*/
void error_quit( char* error_message )
{
fprintf( stderr, "Error: %s\n", error_message );
exit( -1 );
}
/* get_time()
* Returns time in microseconds.
*/
microtime_t get_time( void )
{
struct timeval t;
gettimeofday( &t, NULL );
return ((microtime_t)t.tv_sec * 1000000) + t.tv_usec;
}
// MATH HELPERS ///////////////////////////////////////////////////////////////
vector_t vector( real_t x, real_t y, real_t z )
{
vector_t ret;
ret.x = x;
ret.y = y;
ret.z = z;
return ret;
}
void round_vector( vector_t* v )
{
v->x = round( v->x );
v->y = round( v->y );
v->z = round( v->z );
}
vector_t add_vector( vector_t base, vector_t offset )
{
vector_t result;
result.x = base.x + offset.x;
result.y = base.y + offset.y;
result.z = base.z + offset.z;
return result;
}
vector_t subtract_vector( vector_t base, vector_t offset )
{
vector_t result;
result.x = base.x - offset.x;
result.y = base.y - offset.y;
result.z = base.z - offset.z;
return result;
}
real_t vector_length( vector_t v )
{
return sqrt( v.x*v.x + v.y*v.y + v.z*v.z );
}
vector_t multiply_vector_scalar( vector_t v, real_t scalar )
{
v.x *= scalar;
v.y *= scalar;
v.z *= scalar;
return v;
}
vector_t unity_vector( vector_t v )
{
real_t length = vector_length(v);
if (length == 0) {
fprintf( stderr, "unity_vector(): length == 0\n" );
return multiply_vector_scalar( v, 0 );
}
else {
return multiply_vector_scalar( v, 1/length );
}
}
int sgn( int r )
{
return (r > 0) - (r < 0);
}
real_t fsgn( real_t r )
{
return (r > 0) - (r < 0);
}
int min( int a, int b )
{
return (a < b) ? a : b ;
}
int max( int a, int b )
{
return (a > b) ? a : b ;
}
// MAIN PROGRAM ///////////////////////////////////////////////////////////////
/* save_frame_time()
* Stores a record of measured frame calculation time into an array used for
* finding the average frame rate over the last NR_FPS_SAMPLES samples.
*/
void save_frame_time( program_state_t* PS )
{
++PS->next_fps_sample;
PS->next_fps_sample %= NR_FPS_SAMPLES;
PS->frame_times[ PS->next_fps_sample ] = PS->frame_time_us;
}
/* calculate_frames_per_second()
* Returns the average for all NR_FPS_SAMPLES samples
*/
real_t average_frame_time( program_state_t* PS )
{
int i;
int active_samples = 0;
microtime_t Tsum = 0;
for( i = 0 ; i < NR_FPS_SAMPLES ; i++ ) {
if (PS->frame_times[i] > 0) {
++active_samples;
Tsum += PS->frame_times[i];
}
}
return Tsum / (real_t)active_samples;
}
/* main()
* Program entry point. Initializes everything and runs the main loop.
* Times are calculated and the frame rate is controlled here.
*/
int main( int argc, char* argv[] )
{
program_state_t* PS = &program_state;
game_state_t* GS = &game_state;
sounds_t* snd = &(GS->sounds);
PS->program_start_us = get_time();
init_sdl( PS, GS ); // Create window, load sounds
init_sound( PS, GS ); // Load music, sounds, set volume
init_font( PS ); // Load font
init_opengl( PS ); // Load textures
play_music( snd->music );
load_highscore( GS );
PS->current_time_us =
PS->frame_start_us = get_time();
// Make the following look nice in debug info
PS->game_start_us =
PS->pause_since_us = PS->program_start_us - 1;
PS->tick_fraction_s = -1;
PS->highest_frame_time = 0;
PS->lowest_frame_time = 99999999;
PS->run_mode = RM_INTRO;
while (PS->run_mode != RM_EXIT) {
PS->current_time_us = get_time();
// Calculate how much time we need to waste for target FPS
PS->delay_until_us
= PS->current_time_us
+ (1000000 / TARGET_FPS)
;
// Keep track of game time (handling pause)
if ( !(PS->run_mode & (RM_PAUSE | RM_MAIN_MENU)) ) {
PS->game_time_us
= PS->current_time_us
- PS->game_start_us
;
}
// Show intro after main menu idles for a while
if (PS->run_mode == RM_MAIN_MENU) {
if (PS->main_menu_since_us
+ MAIN_MENU_INTRO_SWITCH_TIME < get_time()) {
PS->run_mode = RM_INTRO;
}
}
// Handle user input
process_event_queue( PS, GS );
// Let the universe live for a short moment
if (PS->run_mode & (RM_RUNNING | RM_AFTER_LIFE)) {
advance_simulation( PS, GS );
}
// Display the current world status as game scene
draw_frame( PS, GS );
#if LIMIT_FPS
// Waste time until the next frame needs to be drawn
while (get_time() < PS->delay_until_us) {
#if LIMIT_FPS_USING_SDL_DELAY
SDL_Delay( 1 );
#endif
}
#endif
// Keep track of how many FPS we actually achieved
PS->frame_time_us = get_time() - PS->frame_start_us;
PS->highest_frame_time = max(
PS->highest_frame_time,
PS->frame_time_us
);
PS->lowest_frame_time = min(
PS->lowest_frame_time,
PS->frame_time_us
);
PS->frame_start_us = get_time();
PS->tick_fraction_s = (real_t)PS->frame_time_us / 1000000.0;
save_frame_time( PS ); // For average FPS
PS->average_frame_time = average_frame_time( PS );
}
save_highscore( PS, GS );
glDeleteTextures( 1, &(PS->textures.background) );
glDeleteTextures( 1, &(PS->textures.digits) );
Mix_FreeChunk( snd->laser );
Mix_FreeChunk( snd->hit );
Mix_FreeChunk( snd->punch );
Mix_FreeChunk( snd->blast );
Mix_FreeChunk( snd->denied );
Mix_FreeChunk( snd->alarm );
Mix_FreeChunk( snd->blub );
Mix_FreeChunk( snd->computer_autofire );
Mix_FreeChunk( snd->computer_doubleshot );
Mix_FreeChunk( snd->computer_roundshot );
Mix_FreeChunk( snd->computer_danger );
Mix_FreeChunk( snd->computer_weaponlost );
Mix_FreeMusic( snd->music );
SDL_Quit(); // Will (allegedly) also free screen .
return 0;
}
//EOF