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