[7f9531]: main.c Maximize Restore History

Download this file

main.c    331 lines (290 with data), 9.9 kB

  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
329
330
/* Asteroids3D - a first person game of blowing up asteroids
* Copyright (C) 2000 Stuart Mark Pomerantz <smp [at] psc edu>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Stuart Pomerantz
* 3935 Stonecliffe drive
* Monroeville, PA 15146
*/
#include <GL/glut.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "asteroids3D.h"
#define COPYRIGHT "Asteroids3D v0.2.2 Copyright (C) 2000 Stuart Mark Pomerantz\n"
/* The code which generates data for rendering will cycle some number of ticks
per second.
On a "slow" computer, this code will go slower than the number of ticks per
second. The limiting factor should always be the time to render, however.
On a "fast" computer, this code will cycle no faster than ticks per second, so
the game should never be un-playable on a "fast" computer. */
#define TPS 60.0
static void display_copyright_on_terminal(void);
static void glut_initialization(int *, char **);
static void gl_initialization(void);
static void idle(void);
static void my_initialization(void);
static void parse_command_line(int, char **);
static void reshape(int, int);
/**
** These variables are ALL the global variables in the game
**/
/* Flags for internal program control */
int mouse_roty_flag = 0 ; /* set to 1 if the mouse has been moved out of the dead zone to rotate the ship about the y axis */
int mouse_rotx_flag = 0 ; /* set to 1 if the mouse has been moved out of the dead zone to rotate the ship about the x axis */
int game_end_flag = 0 ; /* is the game over */
/* track the mouse cursor */
int mouse_x, mouse_y ;
/* the proportion the mouse has moved out of the dead zone */
double mouse_xprop = 0;
double mouse_yprop = 0;
/* the number of game time ticks that have past */
unsigned long ticks = 0 ;
/* dt = delta time, the amount of time (in seconds)
* which passes with each game tick
*/
double dt = 1.0 / TPS;
/* this is the number of ticks per second, which I may want to change dynamically */
double tps = TPS;
/* frame calculation thread id, and the run_yield
* semaphore for interleaving calculation and display updates
*/
sem_t run_yield ;
static pthread_t fc_tid;
// Dead zone boundaries */
static int mouse_left_bound, mouse_right_bound, mouse_top_bound,
mouse_bottom_bound;
int main(int argc, char **argv) {
#ifdef DATADIR
if(chdir(DATADIR) != 0)
fprintf(stderr, "Could not chdir to %s: %s\n", DATADIR, strerror(errno));
return EXIT_FAILURE;
}
#endif
srand(time(NULL));
parse_command_line( argc, argv ) ;
display_copyright_on_terminal() ;
read_rcfile() ;
glut_initialization(&argc, argv) ;
gl_initialization() ;
my_initialization() ;
glutMainLoop();
return(0) ;
}
static void glut_initialization(int *argc, char **argv) {
/* pass command line args to glut */
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(optmap.windowx, optmap.windowy);
glutInitWindowPosition(optmap.window_xpos, optmap.window_ypos);
glutCreateWindow(COPYRIGHT);
glutDisplayFunc( display );
glutMouseFunc( mouse_handler );
glutKeyboardFunc( keyboard_handler );
glutIdleFunc( idle ) ;
glutReshapeFunc( reshape ) ;
glutPassiveMotionFunc( passive_mouse_handler ) ;
/* center the mouse cursor -- see glutPassiveMouseFunc */
glutWarpPointer(optmap.windowx/2, optmap.windowy/2) ;
/* looks better than the arrow, I think */
/* glutSetCursor(GLUT_CURSOR_CROSSHAIR) ;*/
glutSetCursor(GLUT_CURSOR_NONE) ;
return ;
}
static void gl_initialization(void) {
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(FRUSTUM_LEFT, 1 /* right */, -1 /* bottom */, FRUSTUM_TOP,
1 /* near */, 100 /* far */);
glEnable(GL_CULL_FACE) ;
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D) ;
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
return ;
}
static void my_initialization(void) {
printf("Initializing...\n");
reset_view() ;
gl_init_light() ;
gl_init_headlight() ; /* reset_view() must be called before gl_init_headlight */
init_thrust() ;
init_torps() ;
init_textures();
init_asteroids() ;
init_shield() ;
init_hud() ;
init_sun() ; /* must be called after gl_init_light */
init_explosions();
init_dust() ;
init_target_dls() ;
init_nebula_billboards() ; /* must be called after gl_init_light, and init_textures */
init_wind_parts() ;
init_sound() ;
init_lens_flares() ;
/* finally, and always last,
* set up the run_yield semaphore and
* crank up the frame calculation thread
*/
sem_init( &run_yield, 0 , 1) ;
pthread_create(&fc_tid, NULL, calculate_frame, NULL);
printf("Done.\n");
return ;
}
static void idle(void) {
/* calculate_frame() is now a thread by itself, hope this works */
/* calculate_frame() ;*/
glutPostRedisplay() ;
return ;
}
static void reshape(int w, int h) {
unsigned int m;
glViewport(0, 0, w, h);
optmap.windowx = w ;
optmap.windowy = h ;
m = optmap.windowx / 2;
mouse_left_bound = m - 50;
mouse_right_bound = m + 50;
m = optmap.windowy / 2;
mouse_top_bound = m - 50;
mouse_bottom_bound = m + 50;
return ;
}
void mouse_handler( int button, int state, int x, int y) {
/* mouse x,y are given in window coordinates
*/
switch( button ) {
case GLUT_LEFT_BUTTON:
if( state == GLUT_DOWN ) {
fire_torpedo() ;
}
break ;
case GLUT_RIGHT_BUTTON:
if( state == GLUT_DOWN ) {
target_asteroid();
}
break ;
}
return ;
}
void passive_mouse_handler(int x, int y) {
/* set the mouse position globals */
mouse_x = x ;
mouse_y = y ;
/* if mouse is inside dead zone */
if(x > mouse_left_bound && x < mouse_right_bound &&
y > mouse_top_bound && y < mouse_bottom_bound) {
mouse_roty_flag = 0 ;
mouse_rotx_flag = 0 ;
return ;
}
/* if mouse is not in the dead zone set the flag */
if(x > mouse_right_bound) {
mouse_roty_flag = 1 ;
mouse_yprop = (double)(x - mouse_right_bound) / (optmap.windowx - mouse_right_bound);
}
if(x < mouse_left_bound) {
mouse_roty_flag = 1 ;
mouse_yprop = -1 + (double)x / mouse_left_bound;
}
if(y < mouse_top_bound) {
mouse_rotx_flag = 1 ;
mouse_xprop = 1 - (double)y / mouse_top_bound;
}
if(y > mouse_bottom_bound) {
mouse_rotx_flag = 1 ;
mouse_xprop = (double)-(y - mouse_bottom_bound) / (optmap.windowy - mouse_bottom_bound);
}
/* for reversing mouse direction */
mouse_yprop = mouse_yprop*optmap.mouse_ydir ;
mouse_xprop = mouse_xprop*optmap.mouse_xdir ;
return ;
}
void end_game(void) {
game_end_flag = 1 ; /* this causes the calculation thread to exit */
init_score_display() ;
return ;
}
static void parse_command_line(int argc, char **argv) {
int i ;
int temp1, temp2, temp3, temp4 ;
for( i = 0 ; i < argc ; i++ ) {
if(strcmp("-help", argv[i]) == 0 || strcmp("-h", argv[i]) == 0 ||
strcmp("--help", argv[i]) == 0 || strcmp("-?", argv[i]) == 0) {
printf("valid command line options:\n") ;
printf(" -num <number>\n"
" to set the number of asteroids\n\n") ;
printf(" -geometry <width>x<height>+<xoffset>+<yoffset>\n"
" to set the geometry of the window (in pixels); e.g. 640x480+150+50\n\n");
printf(" -rcfile <filename>\n"
" to use <filename> as the .a3drc file instead of $HOME/.a3drc\n\n") ;
printf(" -freshrc <filename>\n"
" to print out a fresh .a3drc file\n\n") ;
printf(" -freshrc1 <filename>\n"
" same as -freshrc, but use old key mappings\n\n") ;
exit(0) ;
} else if(strcmp("-num", argv[i]) == 0) {
if( i + 1 < argc ) {
if( sscanf(argv[i+1],"%d\n",&temp1) == 1 ) {
if( temp1 > 1 )
optmap.num_asteroids = temp1 ;
i++ ;
}
}
} else if(strcmp("-geometry", argv[i]) == 0) {
if( i + 1 < argc ) {
if( sscanf(argv[i+1],"%dx%d+%d+%d\n", &temp1, &temp2, &temp3, &temp4) >= 2 ) {
if(temp1 > 0 && temp2 >0) {
optmap.windowx = temp1 ;
optmap.windowy = temp2 ;
}
if( temp3 >= 0 )
optmap.window_xpos = temp3 ;
if( temp4 >= 0 )
optmap.window_ypos = temp4 ;
}
}
} else if(strcmp("-rcfile", argv[i]) == 0) {
if( i + 1 < argc ) {
temp1 = strlen(argv[i+1]) + 1 ;
rcfile = calloc(1, temp1);
memcpy(rcfile, argv[i+1], temp1 ) ;
}
} else if(strcmp("-freshrc", argv[i]) == 0) {
write_rcfile_to_terminal(0);
exit(0) ;
} else if(strcmp("-freshrc1", argv[i]) == 0) {
write_rcfile_to_terminal(1);
exit(0) ;
}
} /* end for each argument */
return ;
}
static void display_copyright_on_terminal(void) {
printf(COPYRIGHT) ;
printf("as3D-jen patch(es) by Jan Engelhardt"
" <jengelh [at] linux01 gwdg de>, 2002 - 2005\n");
printf("Asteroids3D comes with ABSOLUTELY NO WARRANTY; for details\n"
"see the file COPYRIGHT which should have come with this program.\n"
"This is free software, and you are welcome to redistribute it\n"
"under certain conditions; see the file COPYRIGHT\n"
"which should have come with this program for details.\n") ;
}