JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made closest stars a bit further away
[vor.git] / main.c
1 /* Variations on RockDodger
2  * Space Rocks copyright (C) 2001 Paul Holt <pad@pcholt.com>
3  *
4  * Project fork 2004, Jason Woofenden and Joshua Grams.
5  * (a whole bunch of modifications and project rename)
6
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #ifdef DEBUG
23 #include "debug.h"
24 #endif
25
26 #include "config.h"
27 #include "file.h"
28 #include "globals.h"
29 #include "rocks.h"
30 #include "score.h"
31 #include "shape.h"
32 #include "sound.h"
33
34 #include <math.h>
35 #include <SDL/SDL.h>
36 #include <SDL/SDL_image.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "SFont.h"
44
45 // ************************************* VARS
46 // SDL_Surface global variables
47 SDL_Surface 
48         *surf_screen,   // Screen
49         *surf_b_variations, // "variations" banner
50         *surf_b_on, // "on" banner
51         *surf_b_rockdodger, // "rockdodger" banner
52         *surf_b_game,   // Title element "game"
53         *surf_b_over,   // Title element "over"
54         *surf_ship,             // Spaceship element
55         *surf_life,     // Indicator of number of ships remaining
56         *surf_rock[NROCKS],     // THE ROCKS
57         *surf_font_big; // The big font
58
59 SFont_Font *g_font;
60
61 // Structure global variables
62 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
63 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
64 struct spacedot sdot[MAXSPACEDOTS];
65
66 // Other global variables
67 char topline[1024];
68 char *initerror = "";
69
70 struct shape shipshape;
71 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
72 float shipdx = 8, shipdy = 0;   // Change in X position per tick.
73 float screendx = 7.5, screendy = 0.0;
74 float xscroll, yscroll;
75 float gamerate;  // this controls the speed of everything that moves.
76
77 float bangx, bangy, bangdx, bangdy;
78
79 int nships,score,initticks,ticks_since_last, last_ticks;
80 int gameover;
81 int maneuver = 0;
82 int sound_flag, music_flag;
83 int tail_plume; // display big engine at the back?
84 int friction;   // should there be friction?
85 float fadetimer = 0, faderate;
86
87 int pausedown = 0, paused = 0;
88
89 // bangdot start (bd1) and end (bd2) position:
90 int bd1 = 0, bd2 = 0;
91
92 enum states {
93         TITLE_PAGE,
94         GAMEPLAY,
95         DEAD_PAUSE,
96         GAME_OVER,
97         HIGH_SCORE_ENTRY,
98         HIGH_SCORE_DISPLAY
99 };
100 enum states state = TITLE_PAGE;
101 float state_timeout = 600.0;
102
103 #define NSEQUENCE 2
104 char *sequence[] = {
105         "Press SPACE to start",
106         "http://qualdan.com/vor/"
107 };
108
109 int bangdotlife, nbangdots;
110 Uint16 heatcolor[W*3];
111
112 char *data_dir;
113 extern char *optarg;
114 extern int optind, opterr, optopt;
115
116 // ************************************* FUNCS
117
118 float
119 rnd() {
120         return (float)random()/(float)RAND_MAX;
121 }
122
123 void
124 init_engine_dots() {
125         int i;
126         for(i = 0; i<MAXENGINEDOTS; i++) {
127                 edot[i].active = 0;
128         }
129 }
130
131 void
132 init_space_dots() {
133         int i,b;
134         for(i = 0; i<MAXSPACEDOTS; i++) {
135                 sdot[i].x = rnd()*(XSIZE-5);
136                 sdot[i].y = rnd()*(YSIZE-5);
137                 sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
138                 b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
139                 sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
140         }
141 }
142
143 void
144 make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
145
146         // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
147         // TODO - generate and display dots in a circular buffer
148
149         int x,y,endcount;
150         Uint16 *rawpixel,c;
151         double theta,r;
152         int begin_generate;
153
154         begin_generate = SDL_GetTicks();
155
156         SDL_LockSurface(s);
157         rawpixel = (Uint16 *) s->pixels;
158
159         //for(n = 0; n <= power/2; n++) {
160
161         endcount = 0;
162         while (endcount<3) {
163                 for(x = 0; x<s->w; x++) {
164                         for(y = 0; y<s->h; y++) {
165                                 c = rawpixel[s->pitch/2*y + x];
166                                 if(c && c != s->format->colorkey) {
167
168                                         theta = rnd()*M_PI*2;
169
170                                         r = 1-(rnd()*rnd());
171
172                                         bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
173                                         bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
174                                         bdot[bd2].x = x + xbang;
175                                         bdot[bd2].y = y + ybang;
176
177                                         // Replace the last few bang dots with the pixels from the exploding object
178                                         bdot[bd2].c = (endcount>0)?c:0;
179                                         bdot[bd2].life = 100;
180                                         bdot[bd2].decay = rnd()*3 + 1;
181                                         bdot[bd2].active = 1;
182
183                                         bd2++;
184                                         bd2 %= MAXBANGDOTS;
185
186                                         // If the circular buffer is filled, who cares? They've had their chance.
187                                         //if(bd2 == bd1-1) goto exitloop;
188
189                                 }
190                         }
191                 }
192
193                 if(SDL_GetTicks() - begin_generate > 7) endcount++;
194         }
195
196         SDL_UnlockSurface(s);
197
198 }
199
200 void
201 draw_bang_dots(SDL_Surface *s) {
202         int i;
203         int first_i, last_i;
204         Uint16 *rawpixel;
205         rawpixel = (Uint16 *) s->pixels;
206
207         first_i = -1;
208
209         for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
210
211                 i %= MAXBANGDOTS;
212
213                 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
214                         // If the dot has drifted outside the perimeter, kill it
215                         bdot[i].active = 0;
216                 }
217
218                 if(bdot[i].active) {
219                         if(first_i < 0)
220                         first_i = i;
221                         //last_i = i + 1;
222                         rawpixel[(int)(s->pitch/2*(int)(bdot[i].y)) + (int)(bdot[i].x)] = bdot[i].c ? bdot[i].c : heatcolor[(int)(bdot[i].life*3)];
223                         bdot[i].life -= bdot[i].decay;
224                         bdot[i].x += bdot[i].dx*gamerate - xscroll;
225                         bdot[i].y += bdot[i].dy*gamerate - yscroll;
226
227                         if(bdot[i].life<0)
228                         bdot[i].active = 0;
229                 }
230         }
231
232         if(first_i >= 0) {
233                 bd1 = first_i;
234                 bd2 = last_i;
235         }
236         else {
237                 bd1 = 0;
238                 bd2 = 0;
239         }
240
241 }
242
243
244 void
245 draw_space_dots(SDL_Surface *s) {
246         int i;
247         Uint16 *rawpixel;
248         rawpixel = (Uint16 *) s->pixels;
249
250         for(i = 0; i<MAXSPACEDOTS; i++) {
251                 if(sdot[i].y<0) {
252                         sdot[i].y = 0;
253                 }
254                 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
255                 sdot[i].x -= xscroll / (1.3 + sdot[i].z);
256                 sdot[i].y -= yscroll / (1.3 + sdot[i].z);
257                 if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
258                 else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
259                 if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
260                 else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
261         }
262 }
263
264 void
265 draw_engine_dots(SDL_Surface *s) {
266         int i;
267         Uint16 *rawpixel;
268         rawpixel = (Uint16 *) s->pixels;
269
270         for(i = 0; i<MAXENGINEDOTS; i++) {
271                 if(edot[i].active) {
272                         edot[i].x += edot[i].dx*gamerate - xscroll;
273                         edot[i].y += edot[i].dy*gamerate - yscroll;
274                         if((edot[i].life -= gamerate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
275                                 edot[i].active = 0;
276                         } else if(edot[i].x<0 || edot[i].x>XSIZE) {
277                                 edot[i].active = 0;
278                         } else {
279                                 int heatindex;
280                                 heatindex = edot[i].life * 6;
281                                 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
282                                 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
283                         }
284                 }
285         }
286 }
287
288 void
289 create_engine_dots(int newdots) {
290         int i;
291         double theta,r,dx,dy;
292
293         if(!tail_plume) return;
294
295         if(state == GAMEPLAY) {
296                 for(i = 0; i<newdots*gamerate; i++) {
297                         if(dotptr->active == 0) {
298                                 theta = rnd()*M_PI*2;
299                                 r = rnd();
300                                 dx = cos(theta)*r;
301                                 dy = sin(theta)*r;
302
303                                 dotptr->active = 1;
304                                 dotptr->x = shipx + surf_ship->w/2-14;
305                                 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*5-1;
306                                 dotptr->dx = 10*(dx-1.5) + shipdx;
307                                 dotptr->dy = 1*dy + shipdy;
308                                 dotptr->life = 45 + rnd(1)*5;
309
310                                 dotptr++;
311                                 if(dotptr-edot >= MAXENGINEDOTS) {
312                                         dotptr = edot;
313                                 }
314                         }
315                 }
316         }
317 }
318
319 void
320 create_engine_dots2(int newdots, int m) {
321         int i;
322         double theta, theta2, dx, dy, adx, ady;
323
324         // Don't create fresh engine dots when
325         // the game is not being played and a demo is not beng shown
326         if(state != GAMEPLAY) return;
327
328         for(i = 0; i<newdots; i++) {
329                 if(dotptr->active == 0) {
330                         theta = rnd()*M_PI*2;
331                         theta2 = rnd()*M_PI*2;
332
333                         dx = cos(theta) * fabs(cos(theta2));
334                         dy = sin(theta) * fabs(cos(theta2));
335                         adx = fabs(dx);
336                         ady = fabs(dy);
337
338
339                         dotptr->active = 1;
340                         dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
341                         dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
342
343                         switch(m) {
344                                 case 0:
345                                         dotptr->x -= 14;
346                                         dotptr->dx = -20*adx + shipdx;
347                                         dotptr->dy = 2*dy + shipdy;
348                                         dotptr->life = 60 * adx;
349                                 break;
350                                 case 1:
351                                         dotptr->dx = 2*dx + shipdx;
352                                         dotptr->dy = -20*ady + shipdy;
353                                         dotptr->life = 60 * ady;
354                                 break;
355                                 case 2:
356                                         dotptr->x += 14;
357                                         dotptr->dx = 20*adx + shipdx;
358                                         dotptr->dy = 2*dy + shipdy;
359                                         dotptr->life = 60 * adx;
360                                 break;
361                                 case 3:
362                                         dotptr->dx = 2*dx + shipdx;
363                                         dotptr->dy = 20*ady + shipdy;
364                                         dotptr->life = 60 * ady;
365                                 break;
366                         }
367                         dotptr++;
368                         if(dotptr-edot >= MAXENGINEDOTS) {
369                                 dotptr = edot;
370                         }
371                 }
372         }
373 }
374
375 void
376 drawdots(SDL_Surface *s) {
377         int m;
378
379         // Create more engine dots comin' out da back
380         if(!gameover) create_engine_dots(200);
381
382         // Create engine dots out the side we're moving from
383         for(m = 0; m<4; m++) {
384                 if(maneuver & 1<<m) { // 'maneuver' is a bit field
385                         create_engine_dots2(80,m);
386                 }
387         }
388
389         SDL_LockSurface(s);
390         draw_space_dots(s);
391         draw_engine_dots(s);
392         draw_bang_dots(s);
393         SDL_UnlockSurface(s);
394 }
395
396 int
397 init(int fullscreen) {
398
399         int i;
400         SDL_Surface *temp;
401         Uint32 flag;
402
403         // Where are our data files?
404         if(!find_files()) exit(1);
405         read_high_score_table();
406
407         if(sound_flag) {
408                 // Initialize SDL with audio and video
409                 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
410                         sound_flag = 0;
411                         printf ("Can't open sound, starting without it\n");
412                         atexit(SDL_Quit);
413                 } else {
414                         atexit(SDL_Quit);
415                         atexit(SDL_CloseAudio);
416                         sound_flag = init_sound();
417                 }
418         } else {
419                 // Initialize with video only
420                 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
421                 atexit(SDL_Quit);
422         }
423
424         play_tune(0);
425
426         // Attempt to get the required video size
427         flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
428         if(fullscreen) flag |= SDL_FULLSCREEN;
429         surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
430
431         // Set the title bar text
432         SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
433
434         NULLERROR(surf_screen);
435
436         // Set the heat color from the range 0 (cold) to 300 (blue-white)
437         for(i = 0; i<W*3; i++) {
438                 heatcolor[i] = SDL_MapRGB(
439                         surf_screen->format,
440                         (i<W)?(i*M/W):(M),(i<W)?0:(i<2*W)?((i-W)*M/W):M,(i<2*W)?0:((i-W)*M/W) // Got that?
441                 );
442         }
443
444         // Load the banners
445         NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
446         NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
447
448         NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
449         NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
450
451         NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
452         NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
453
454         NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
455         NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
456
457         NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
458         NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
459
460         surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
461         g_font = SFont_InitFont(surf_font_big);
462
463         // Load the spaceship graphic.
464         NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
465         NULLERROR(surf_ship = SDL_DisplayFormat(temp));
466         get_shape(surf_ship, &shipshape);
467
468         // Load the life indicator (small ship) graphic.
469         NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
470         NULLERROR(surf_life = SDL_DisplayFormat(temp));
471
472         init_engine_dots();
473         init_space_dots();
474
475         init_rocks();
476
477         // Remove the mouse cursor
478 #ifdef SDL_DISABLE
479         SDL_ShowCursor(SDL_DISABLE);
480 #endif
481
482         return 0;
483 }
484
485 int
486 draw() {
487         int i;
488         SDL_Rect src,dest;
489         int bang, x;
490         char *text;
491         float fadegame,fadeover;
492
493         bang = 0;
494
495         src.x = 0;
496         src.y = 0;
497         dest.x = 0;
498         dest.y = 0;
499
500         // Draw a fully black background
501         SDL_FillRect(surf_screen,NULL,0);
502
503         // Draw the background dots
504         drawdots(surf_screen);
505
506         // Draw ship
507         if(!gameover && state == GAMEPLAY ) {
508                 src.w = surf_ship->w;
509                 src.h = surf_ship->h;
510                 dest.w = src.w;
511                 dest.h = src.h;
512                 dest.x = (int)shipx;
513                 dest.y = (int)shipy;
514                 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
515         }
516
517         draw_rocks();
518
519         // Draw the life indicators.
520         if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
521         for(i = 0; i<nships-1; i++) {
522                 src.w = surf_life->w;
523                 src.h = surf_life->h;
524                 dest.w = src.w;
525                 dest.h = src.h;
526                 dest.x = (i + 1)*(src.w + 10);
527                 dest.y = 20;
528                 SDL_BlitSurface(surf_life, &src, surf_screen, &dest);
529         }
530
531         // Draw the score
532         snprintscore_line(topline, 50, score);
533         SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
534
535         // If it's game over, show the game over graphic in the dead centre
536         switch (state) {
537                 case GAME_OVER:
538                         if(fadetimer<3.0/faderate) {
539                                 fadegame = fadetimer/(3.0/faderate);
540                         } else {
541                                 fadegame = 1.0;
542                         }
543
544                         if(fadetimer<3.0/faderate) {
545                                 fadeover = 0.0;
546                         } else if(fadetimer<6.0/faderate) {
547                                 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
548                         } else {
549                                 fadeover = 1.0;
550                         }
551
552                         src.w = surf_b_game->w;
553                         src.h = surf_b_game->h;
554                         dest.w = src.w;
555                         dest.h = src.h;
556                         dest.x = (XSIZE-src.w)/2;
557                         dest.y = (YSIZE-src.h)/2-40;
558                         SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += gamerate/1.0))));
559                         SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
560
561                         src.w = surf_b_over->w;
562                         src.h = surf_b_over->h;
563                         dest.w = src.w;
564                         dest.h = src.h;
565                         dest.x = (XSIZE-src.w)/2;
566                         dest.y = (YSIZE-src.h)/2 + 40;
567                         SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
568                         SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
569                 break;
570
571                 case TITLE_PAGE:
572
573                         src.w = surf_b_variations->w;
574                         src.h = surf_b_variations->h;
575                         dest.w = src.w;
576                         dest.h = src.h;
577                         dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
578                         dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
579                         SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += gamerate/2.0)));
580                         SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
581
582                         src.w = surf_b_on->w;
583                         src.h = surf_b_on->h;
584                         dest.w = src.w;
585                         dest.h = src.h;
586                         dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
587                         dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
588                         SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
589                         SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
590
591                         src.w = surf_b_rockdodger->w;
592                         src.h = surf_b_rockdodger->h;
593                         dest.w = src.w;
594                         dest.h = src.h;
595                         dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
596                         dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
597                         SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
598                         SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
599
600                         text = "Version " VERSION;
601                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
602                         SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
603
604                         text = sequence[(int)(fadetimer/40)%NSEQUENCE];
605                         //text = "Press SPACE to start!";
606                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
607                         SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
608                 break;
609
610                 case HIGH_SCORE_ENTRY:
611                         play_tune(2);
612                         if(!process_score_input()) {  // done inputting name
613
614                                 // Change state to briefly show high scores page
615                                 state = HIGH_SCORE_DISPLAY;
616                                 state_timeout = 200;
617
618                                 // Write the high score table to the file
619                                 write_high_score_table();
620                 
621                                 // Play the title page tune
622                                 play_tune(0);
623                         }
624                 // FALL THROUGH TO
625                 case HIGH_SCORE_DISPLAY:
626                         // Display de list o high scores mon.
627                         display_scores(surf_screen, 150,50);
628                         break;
629                 case GAMEPLAY:
630                 case DEAD_PAUSE:
631                         ; // no action necessary
632         }
633
634         if(!gameover && state == GAMEPLAY) {
635                 bang = hit_rocks(shipx, shipy, &shipshape);
636         }
637
638         ticks_since_last = SDL_GetTicks()-last_ticks;
639         last_ticks = SDL_GetTicks();
640         if(ticks_since_last>200 || ticks_since_last<0) {
641                 gamerate = 0;
642         }
643         else {
644                 gamerate = GAMESPEED*ticks_since_last/50.0;
645                 if(state == GAMEPLAY) {
646                         score += ticks_since_last;
647                 }
648         }
649
650         // Update the surface
651         SDL_Flip(surf_screen);
652
653
654         return bang;
655 }
656
657 int
658 gameloop() {
659         Uint8 *keystate;
660         float tmp;
661
662
663         for(;;) {
664                 if(!paused) {
665                         // Count down the game loop timer, and change state when it gets to zero or less;
666
667                         if((state_timeout -= gamerate*3) < 0) {
668                                 switch(state) {
669                                         case DEAD_PAUSE:
670                                                 // Create a new ship and start all over again
671                                                 state = GAMEPLAY;
672                                                 play_tune(1);
673                                                 break;
674                                         case GAME_OVER:
675                                                 state = HIGH_SCORE_ENTRY;
676                                                 state_timeout = 5.0e6;
677                                                 if(new_high_score(score)) {
678                                                         SDL_Event e;
679                                                         SDL_EnableUNICODE(1);
680                                                         while(SDL_PollEvent(&e))
681                                                                 ;
682                                                 } else {
683                                                         state = HIGH_SCORE_DISPLAY;
684                                                         state_timeout = 400;
685                                                 }
686                                                 break;
687                                         case HIGH_SCORE_DISPLAY:
688                                                 state = TITLE_PAGE;
689                                                 state_timeout = 500.0;
690                                                 break;
691                                         case HIGH_SCORE_ENTRY:
692                                                 // state = TITLE_PAGE;
693                                                 // play_tune(1);
694                                                 // state_timeout = 100.0;
695                                                 break;
696                                         case TITLE_PAGE:
697                                                 state = HIGH_SCORE_DISPLAY;
698                                                 state_timeout = 200.0;
699                                                 break;
700                                         case GAMEPLAY:
701                                                 ; // no action necessary
702                                 }
703                         } else {
704                                 if(state == DEAD_PAUSE) {
705                                         float blast_radius;
706                                         int fixonly;
707
708                                         if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
709                                                 blast_radius = BLAST_RADIUS * 1.3;
710                                                 fixonly = 1;
711                                         } else {
712                                                 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
713                                                 fixonly = 0;
714                                         }
715                                         blast_rocks(bangx, bangy, blast_radius, fixonly);
716
717                                         if(bangx < 60) bangx = 60;
718                                 }
719                         }
720
721                         new_rocks();
722
723                         // FRICTION?
724                         if(friction) {
725                                 shipdx *= pow((double)0.9,(double)gamerate);
726                                 shipdy *= pow((double)0.9,(double)gamerate);
727                         }
728
729                         // INERTIA
730                         shipx += shipdx*gamerate;
731                         shipy += shipdy*gamerate;
732
733                         // SCROLLING
734                         tmp = shipy - (YSIZE / 2);
735                         tmp += shipdy * 25;
736                         tmp /= -25;
737                         tmp = ((screendy * (gamerate - 12)) + (tmp * gamerate)) / 12;
738                         screendy = -tmp;
739                         tmp = shipx - (XSIZE / 3);
740                         tmp += shipdx * 25;
741                         tmp /= -25;
742                         tmp = ((screendx * (gamerate - 12)) + (tmp * gamerate)) / 12;
743                         screendx = -tmp;
744
745                         xscroll = screendx * gamerate;
746                         yscroll = screendy * gamerate;
747                         shipx -= xscroll;
748                         shipy -= yscroll;
749
750                         // move bang center
751                         bangx += bangdx*gamerate - xscroll;
752                         bangy += bangdy*gamerate - yscroll;
753
754                         move_rocks();
755
756
757                         // BOUNCE X
758                         if(shipx<0 || shipx>XSIZE-surf_ship->w) {
759                                 // BOUNCE from left and right wall
760                                 shipx -= shipdx*gamerate;
761                                 shipdx *= -99;
762                         }
763
764                         // BOUNCE Y
765                         if(shipy<0 || shipy>YSIZE-surf_ship->h) {
766                                 // BOUNCE from top and bottom wall
767                                 shipy -= shipdy;
768                                 shipdy *= -0.99;
769                         }
770
771
772                         if(draw() && state == GAMEPLAY) {
773                                 // Died
774                                 play_sound(0); // Play the explosion sound
775                                 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
776                                 make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
777                                 shipdx *= 0.5; shipdy *= 0.5;
778                                 if(--nships <= 0) {
779                                         state = GAME_OVER;
780                                         gameover = 1;
781                                         shipdx = 8; shipdy = 0;
782                                         state_timeout = 200.0;
783                                         fadetimer = 0.0;
784                                         faderate = gamerate;
785                                 }
786                                 else {
787                                         state = DEAD_PAUSE;
788                                         state_timeout = DEAD_PAUSE_LENGTH;
789                                 }
790                         }
791
792                         SDL_PumpEvents();
793                         keystate = SDL_GetKeyState(NULL);
794
795                         if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
796
797                                 reset_rocks();
798
799                                 nships = 4;
800                                 score = 0;
801
802                                 state = GAMEPLAY;
803                                 play_tune(1);
804
805                                 gameover = 0;
806                                 shipx = XSIZE/2.2; shipy = YSIZE/2;
807                                 shipdx = screendx; shipdy = screendy;
808                         }
809
810                         maneuver = 0;
811                 } else {
812                         SDL_PumpEvents();
813                         keystate = SDL_GetKeyState(NULL);
814                 }
815
816                 if(state == GAMEPLAY) {
817                         if(!gameover) {
818
819                                 if(!paused) {
820                                         if(keystate[SDLK_UP] | keystate[SDLK_c])                { shipdy -= 1.5*gamerate; maneuver |= 1<<3;}
821                                         if(keystate[SDLK_DOWN] | keystate[SDLK_t])              { shipdy += 1.5*gamerate; maneuver |= 1<<1;}
822                                         if(keystate[SDLK_LEFT] | keystate[SDLK_h])              { shipdx -= 1.5*gamerate; maneuver |= 1<<2;}
823                                         if(keystate[SDLK_RIGHT] | keystate[SDLK_n])             { shipdx += 1.5*gamerate; maneuver |= 1;}
824                                         if(keystate[SDLK_3])            { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
825                                 }
826
827                                 if(keystate[SDLK_p] | keystate[SDLK_s]) {
828                                         if(!pausedown) {
829                                                 paused = !paused;
830                                                 pausedown = 1;
831                                         }
832                                 } else {
833                                         pausedown = 0;
834                                 }
835
836                         }
837                         else {
838                                 paused = 0;
839                                 pausedown = 0;
840                         }
841                 } else if(state == GAME_OVER) {
842                         if(keystate[SDLK_SPACE]) {
843                                 state_timeout = -1;
844                         }
845                 }
846
847                 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
848                         return 0;
849                 }
850
851         }
852 }
853
854 int
855 main(int argc, char **argv) {
856         int x, fullscreen;
857
858         fullscreen = 0;
859         tail_plume = 0;
860         friction = 0;
861         sound_flag = 1;
862         music_flag = 0;
863
864         while ((x = getopt(argc,argv,"efhmps")) >= 0) {
865                 switch(x) {
866                         case 'e': // engine
867                                 tail_plume = 1;
868                         break;
869                         case 'f': // fullscreen
870                                 fullscreen = 1;
871                         break;
872                         case 'h': // help
873                                 printf("Variations on RockDodger\n"
874                                        " -e big tail [E]ngine\n"
875                                        " -f [F]ull screen\n"
876                                        " -h this [H]elp message\n"
877                                        " -m enable [M]usic\n"
878                                        " -p original [P]hysics (friction)\n"
879                                        " -s [S]ilent (no sound)\n");
880                                 exit(0);
881                         break;
882                         case 'm': // music
883                                 music_flag = 1;
884                         case 'p': // physics
885                                 friction = 1;
886                         break;
887                         case 's': // silent
888                                 sound_flag = 0;
889                                 music_flag = 0;
890                         break;
891                 }
892         }
893
894         if(init(fullscreen)) {
895                 printf ("ta: '%s'\n",initerror);
896                 return 1;
897         }
898
899         reset_rocks();
900         initticks = SDL_GetTicks();
901         gameloop();
902
903         return 0;
904 }