JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
deleted old collision-detection code.
[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 "score.h"
29 #include "shape.h"
30 #include "sound.h"
31
32 #include <math.h>
33 #include <SDL/SDL.h>
34 #include <SDL/SDL_image.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "SFont.h"
42
43 #define CONDERROR(a) if((a)) {initerror = strdup(SDL_GetError());return 1;}
44 #define NULLERROR(a) CONDERROR((a) == NULL)
45
46 // ************************************* STRUCTS
47 struct rock_struct {
48         float x,y,dx,dy;
49         int active;
50         int dead;  // has been blown out of the way
51                    // to make room for a new ship appearing.
52         SDL_Surface *image;
53         struct shape *shape;
54         int type_number;
55 }; 
56 struct bangdots {
57         // Bang dots have the same colour as shield dots.
58         // Bang dots get darker as they age.
59         // Some are coloured the same as the ex-ship.
60         float x,y,dx,dy;
61         Uint16 c; // when zero, use heatcolor[bangdotlife]
62         float life;     // When reduced to 0, set active = 0
63         int active;
64         float decay;// Amount by which to reduce life each time dot is drawn
65 };
66 struct enginedots {
67         // Engine dots stream out the back of the ship, getting darker as they go.
68         int active;
69         float x,y,dx,dy;
70         // The life of an engine dot 
71         // is a number starting at between 0 and 50 and counting backward.
72         float life;     // When reduced to 0, set active = 0
73 };
74 struct spacedot {
75         // Space dots are harmless background items
76         // All are active. When one falls off the edge, another is created at the start.
77         float x,y,dx;
78         Uint16 color;
79 };
80
81 // ************************************* VARS
82 // SDL_Surface global variables
83 SDL_Surface 
84         *surf_screen,   // Screen
85         *surf_b_variations, // "variations" banner
86         *surf_b_on, // "on" banner
87         *surf_b_rockdodger, // "rockdodger" banner
88         *surf_b_game,   // Title element "game"
89         *surf_b_over,   // Title element "over"
90         *surf_ship,             // Spaceship element
91         *surf_life,     // Indicator of number of ships remaining
92         *surf_rock[NROCKS],     // THE ROCKS
93         *surf_font_big; // The big font
94
95 struct shape rock_shapes[NROCKS];
96
97 SFont_Font *g_font;
98
99 // Structure global variables
100 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
101 struct rock_struct rock[MAXROCKS], *rockptr = rock;
102 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
103 struct spacedot sdot[MAXSPACEDOTS];
104
105 // Other global variables
106 char topline[1024];
107 char *initerror = "";
108
109 struct shape shipshape;
110 float shipx,shipy = 240.0;      // X position, 0..XSIZE
111 float shipdx,shipdy;    // Change in X position per tick.
112 float rockrate,rockspeed;
113 float movementrate;  // this controls the speed of everything that moves.
114 float yscroll;
115 float scrollvel;
116
117 int nships,score,initticks,ticks_since_last, last_ticks;
118 int gameover;
119 int countdown = 0;
120 int maneuver = 0;
121 int sound_flag = 1, music_flag = 0;
122 int tail_plume = 0; // display big engine at the back?
123 int friction = 0;       // should there be friction?
124 float fadetimer = 0, faderate;
125
126 int pausedown = 0, paused = 0;
127
128 // bangdot start (bd1) and end (bd2) position:
129 int bd1 = 0, bd2 = 0;
130
131 enum states {
132         TITLE_PAGE,
133         GAMEPLAY,
134         DEAD_PAUSE,
135         GAME_OVER,
136         HIGH_SCORE_ENTRY,
137         HIGH_SCORE_DISPLAY
138 };
139 enum states state = TITLE_PAGE;
140 float state_timeout = 600.0;
141
142 #define NSEQUENCE 2
143 char *sequence[] = {
144         "Press SPACE to start",
145         "http://qualdan.com/vor/"
146 };
147
148 int bangdotlife, nbangdots;
149 Uint16 heatcolor[W*3];
150
151 char *data_dir;
152 extern char *optarg;
153 extern int optind, opterr, optopt;
154
155 float dist_sq(float x1, float y1, float x2, float y2)
156 {
157         return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
158 }
159
160 // ************************************* FUNCS
161
162 float
163 rnd() {
164         return (float)random()/(float)RAND_MAX;
165 }
166
167 void
168 init_engine_dots() {
169         int i;
170         for(i = 0; i<MAXENGINEDOTS; i++) {
171                 edot[i].active = 0;
172         }
173 }
174
175 void
176 init_space_dots() {
177         int i,intensity;
178         for(i = 0; i<MAXSPACEDOTS; i++) {
179                 float r;
180
181                 sdot[i].x = rnd()*(XSIZE-5);
182                 sdot[i].y = rnd()*(YSIZE-5);
183
184                 r = rnd()*rnd();
185
186                 sdot[i].dx = -r*4;
187                 // -1/((1-r) + .3);
188                 intensity = (int)(r*180 + 70);
189                 sdot[i].color = SDL_MapRGB(surf_screen->format,intensity,intensity,intensity);
190
191         }
192 }
193
194 void
195 makebangdots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
196
197         // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
198         // TODO - generate and display dots in a circular buffer
199
200         int x,y,endcount;
201         Uint16 *rawpixel,c;
202         double theta,r;
203         int begin_generate;
204
205         begin_generate = SDL_GetTicks();
206
207         SDL_LockSurface(s);
208         rawpixel = (Uint16 *) s->pixels;
209
210         //for(n = 0; n <= power/2; n++) {
211
212         endcount = 0;
213         while (endcount<3) {
214                 for(x = 0; x<s->w; x++) {
215                         for(y = 0; y<s->h; y++) {
216                                 c = rawpixel[s->pitch/2*y + x];
217                                 if(c && c != s->format->colorkey) {
218
219                                         theta = rnd()*M_PI*2;
220
221                                         r = 1-(rnd()*rnd());
222
223                                         bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
224                                         bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
225                                         bdot[bd2].x = x + xbang;
226                                         bdot[bd2].y = y + ybang;
227
228                                         // Replace the last few bang dots with the pixels from the exploding object
229                                         bdot[bd2].c = (endcount>0)?c:0;
230                                         bdot[bd2].life = 100;
231                                         bdot[bd2].decay = rnd()*3 + 1;
232                                         bdot[bd2].active = 1;
233
234                                         bd2++;
235                                         bd2 %= MAXBANGDOTS;
236
237                                         // If the circular buffer is filled, who cares? They've had their chance.
238                                         //if(bd2 == bd1-1) goto exitloop;
239
240                                 }
241                         }
242                 }
243
244                 if(SDL_GetTicks() - begin_generate > 7) endcount++;
245         }
246
247         SDL_UnlockSurface(s);
248
249 }
250
251 void
252 draw_bang_dots(SDL_Surface *s) {
253         int i;
254         int first_i, last_i;
255         Uint16 *rawpixel;
256         rawpixel = (Uint16 *) s->pixels;
257
258         first_i = -1;
259
260         for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
261
262                 i %= MAXBANGDOTS;
263
264                 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
265                         // If the dot has drifted outside the perimeter, kill it
266                         bdot[i].active = 0;
267                 }
268
269                 if(bdot[i].active) {
270                         if(first_i < 0)
271                         first_i = i;
272                         //last_i = i + 1;
273                         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)];
274                         bdot[i].life -= bdot[i].decay;
275                         bdot[i].x += bdot[i].dx*movementrate;
276                         bdot[i].y += bdot[i].dy*movementrate + yscroll;
277
278                         if(bdot[i].life<0)
279                         bdot[i].active = 0;
280                 }
281         }
282
283         if(first_i >= 0) {
284                 bd1 = first_i;
285                 bd2 = last_i;
286         }
287         else {
288                 bd1 = 0;
289                 bd2 = 0;
290         }
291
292 }
293
294
295 void
296 draw_space_dots(SDL_Surface *s) {
297         int i;
298         Uint16 *rawpixel;
299         rawpixel = (Uint16 *) s->pixels;
300
301         for(i = 0; i<MAXSPACEDOTS; i++) {
302                 if(sdot[i].y<0) {
303                         sdot[i].y = 0;
304                 }
305                 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
306                 sdot[i].x += sdot[i].dx*movementrate;
307                 sdot[i].y += yscroll;
308                 if(sdot[i].y > YSIZE) {
309                         sdot[i].y -= YSIZE;
310                 } else if(sdot[i].y < 0) {
311                         sdot[i].y += YSIZE;
312                 }
313                 if(sdot[i].x<0) {
314                         sdot[i].x = XSIZE;
315                 }
316         }
317 }
318
319 void
320 draw_engine_dots(SDL_Surface *s) {
321         int i;
322         Uint16 *rawpixel;
323         rawpixel = (Uint16 *) s->pixels;
324
325         for(i = 0; i<MAXENGINEDOTS; i++) {
326                 if(edot[i].active) {
327                         edot[i].x += edot[i].dx*movementrate;
328                         edot[i].y += edot[i].dy*movementrate + yscroll;
329                         if((edot[i].life -= movementrate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
330                                 edot[i].active = 0;
331                         } else if(edot[i].x<0 || edot[i].x>XSIZE) {
332                                 edot[i].active = 0;
333                         } else {
334                                 int heatindex;
335                                 heatindex = edot[i].life * 6;
336                                 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
337                                 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
338                         }
339                 }
340         }
341 }
342
343 void
344 create_engine_dots(int newdots) {
345         int i;
346         double theta,r,dx,dy;
347
348         if(!tail_plume) return;
349
350         if(state == GAMEPLAY) {
351                 for(i = 0; i<newdots*movementrate; i++) {
352                         if(dotptr->active == 0) {
353                                 theta = rnd()*M_PI*2;
354                                 r = rnd();
355                                 dx = cos(theta)*r;
356                                 dy = sin(theta)*r;
357
358                                 dotptr->active = 1;
359                                 dotptr->x = shipx + surf_ship->w/2-14;
360                                 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*5-1;
361                                 dotptr->dx = 10*(dx-1.5) + shipdx;
362                                 dotptr->dy = 1*dy + shipdy;
363                                 dotptr->life = 45 + rnd(1)*5;
364
365                                 dotptr++;
366                                 if(dotptr-edot >= MAXENGINEDOTS) {
367                                         dotptr = edot;
368                                 }
369                         }
370                 }
371         }
372 }
373
374 void
375 create_engine_dots2(int newdots, int m) {
376         int i;
377         double theta, theta2, dx, dy, adx, ady;
378
379         // Don't create fresh engine dots when
380         // the game is not being played and a demo is not beng shown
381         if(state != GAMEPLAY) return;
382
383         for(i = 0; i<newdots; i++) {
384                 if(dotptr->active == 0) {
385                         theta = rnd()*M_PI*2;
386                         theta2 = rnd()*M_PI*2;
387
388                         dx = cos(theta) * fabs(cos(theta2));
389                         dy = sin(theta) * fabs(cos(theta2));
390                         adx = fabs(dx);
391                         ady = fabs(dy);
392
393
394                         dotptr->active = 1;
395                         dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
396                         dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
397
398                         switch(m) {
399                                 case 0:
400                                         dotptr->x -= 14;
401                                         dotptr->dx = -20*adx + shipdx;
402                                         dotptr->dy = 2*dy + shipdy;
403                                         dotptr->life = 60 * adx;
404                                 break;
405                                 case 1:
406                                         dotptr->dx = 2*dx + shipdx;
407                                         dotptr->dy = -20*ady + shipdy;
408                                         dotptr->life = 60 * ady;
409                                 break;
410                                 case 2:
411                                         dotptr->x += 14;
412                                         dotptr->dx = 20*adx + shipdx;
413                                         dotptr->dy = 2*dy + shipdy;
414                                         dotptr->life = 60 * adx;
415                                 break;
416                                 case 3:
417                                         dotptr->dx = 2*dx + shipdx;
418                                         dotptr->dy = 20*ady + shipdy;
419                                         dotptr->life = 60 * ady;
420                                 break;
421                         }
422                         dotptr++;
423                         if(dotptr-edot >= MAXENGINEDOTS) {
424                                 dotptr = edot;
425                         }
426                 }
427         }
428 }
429
430 void
431 drawdots(SDL_Surface *s) {
432         int m;
433
434         SDL_LockSurface(s);
435         // Draw the background stars aka space dots
436         draw_space_dots(s);
437
438         // Draw the score when playing the game or whn the game is freshly over
439         if(1 || state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER ) {
440                 SDL_UnlockSurface(s);
441
442                 snprintscore_line(topline, 50, score);
443                 SFont_Write(s,g_font,XSIZE-250,0,topline);
444
445                 SDL_LockSurface(s);
446         }
447
448         // Draw all the engine dots
449         draw_engine_dots(s);
450
451         // Create more engine dots comin out da back
452         if(!gameover) create_engine_dots(200);
453
454         // Create engine dots out the side we're moving from
455         for(m = 0; m<4; m++) {
456                 if(maneuver & 1<<m) { // 'maneuver' is a bit field
457                         create_engine_dots2(80,m);
458                 }
459         }
460
461         // Draw all outstanding bang dots
462         //if(bangdotlife-- > 0) 
463         draw_bang_dots(s);
464
465         SDL_UnlockSurface(s);
466 }
467
468                 char a[MAX_PATH_LEN];
469 int
470 init(int fullscreen) {
471
472         int i;
473         SDL_Surface *temp;
474         Uint32 flag;
475
476         // Where are our data files?
477         if(!find_files()) exit(1);
478         read_high_score_table();
479
480         if(sound_flag) {
481                 // Initialize SDL with audio and video
482                 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
483                         sound_flag = 0;
484                         printf ("Can't open sound, starting without it\n");
485                         atexit(SDL_Quit);
486                 } else {
487                         atexit(SDL_Quit);
488                         atexit(SDL_CloseAudio);
489                         sound_flag = init_sound();
490                 }
491         } else {
492                 // Initialize with video only
493                 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
494                 atexit(SDL_Quit);
495         }
496
497         play_tune(0);
498
499         // Attempt to get the required video size
500         flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
501         if(fullscreen) flag |= SDL_FULLSCREEN;
502         surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
503
504         // Set the title bar text
505         SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
506
507         NULLERROR(surf_screen);
508
509         // Set the heat color from the range 0 (cold) to 300 (blue-white)
510         for(i = 0; i<W*3; i++) {
511                 heatcolor[i] = SDL_MapRGB(
512                         surf_screen->format,
513                         (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?
514                 );
515         }
516
517         // Load the banners
518         NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
519         NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
520
521         NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
522         NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
523
524         NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
525         NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
526
527         NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
528         NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
529
530         NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
531         NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
532
533         surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
534         g_font = SFont_InitFont(surf_font_big);
535
536         // Load the spaceship graphic.
537         NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
538         NULLERROR(surf_ship = SDL_DisplayFormat(temp));
539         get_shape(surf_ship, &shipshape);
540
541         // Load the life indicator (small ship) graphic.
542         NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
543         NULLERROR(surf_life = SDL_DisplayFormat(temp));
544
545         init_engine_dots();
546         init_space_dots();
547
548         // Load all our lovely rocks
549         for(i = 0; i<NROCKS; i++) {
550                 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
551                 NULLERROR(temp = IMG_Load(a));
552                 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
553                 get_shape(surf_rock[i], &rock_shapes[i]);
554         }
555
556         // Remove the mouse cursor
557 #ifdef SDL_DISABLE
558         SDL_ShowCursor(SDL_DISABLE);
559 #endif
560
561         return 0;
562 }
563
564 int
565 draw() {
566         int i;
567         SDL_Rect src,dest;
568         int bang, x;
569         char *text;
570         float fadegame,fadeover;
571
572         bang = 0;
573
574         src.x = 0;
575         src.y = 0;
576         dest.x = 0;
577         dest.y = 0;
578
579         // Draw a fully black background
580         SDL_FillRect(surf_screen,NULL,0);
581
582         // Draw the background dots
583         drawdots(surf_screen);
584
585         // Draw ship
586         if(!gameover && state == GAMEPLAY ) {
587                 src.w = surf_ship->w;
588                 src.h = surf_ship->h;
589                 dest.w = src.w;
590                 dest.h = src.h;
591                 dest.x = (int)shipx;
592                 dest.y = (int)shipy;
593                 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
594         }
595
596         // Draw all the rocks, in all states
597         for(i = 0; i<MAXROCKS; i++) {
598                 if(rock[i].active) {
599
600                         src.w = rock[i].image->w;
601                         src.h = rock[i].image->h;
602                         dest.w = src.w;
603                         dest.h = src.h;
604                         dest.x = (int) rock[i].x;
605                         dest.y = (int) rock[i].y;
606
607                         // Draw the rock
608                         SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
609
610                 }
611         }
612
613         // If it's game over, show the game over graphic in the dead centre
614         switch (state) {
615                 case GAME_OVER:
616                         if(fadetimer<3.0/faderate) {
617                                 fadegame = fadetimer/(3.0/faderate);
618                         } else {
619                                 fadegame = 1.0;
620                         }
621
622                         if(fadetimer<3.0/faderate) {
623                                 fadeover = 0.0;
624                         } else if(fadetimer<6.0/faderate) {
625                                 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
626                         } else {
627                                 fadeover = 1.0;
628                         }
629
630                         src.w = surf_b_game->w;
631                         src.h = surf_b_game->h;
632                         dest.w = src.w;
633                         dest.h = src.h;
634                         dest.x = (XSIZE-src.w)/2;
635                         dest.y = (YSIZE-src.h)/2-40;
636                         SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += movementrate/1.0))));
637                         SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
638
639                         src.w = surf_b_over->w;
640                         src.h = surf_b_over->h;
641                         dest.w = src.w;
642                         dest.h = src.h;
643                         dest.x = (XSIZE-src.w)/2;
644                         dest.y = (YSIZE-src.h)/2 + 40;
645                         SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
646                         SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
647                 break;
648
649                 case TITLE_PAGE:
650
651                         src.w = surf_b_variations->w;
652                         src.h = surf_b_variations->h;
653                         dest.w = src.w;
654                         dest.h = src.h;
655                         dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
656                         dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
657                         SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += movementrate/2.0)));
658                         SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
659
660                         src.w = surf_b_on->w;
661                         src.h = surf_b_on->h;
662                         dest.w = src.w;
663                         dest.h = src.h;
664                         dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
665                         dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
666                         SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
667                         SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
668
669                         src.w = surf_b_rockdodger->w;
670                         src.h = surf_b_rockdodger->h;
671                         dest.w = src.w;
672                         dest.h = src.h;
673                         dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
674                         dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
675                         SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
676                         SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
677
678                         text = "Version " VERSION;
679                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
680                         SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
681
682                         text = sequence[(int)(fadetimer/40)%NSEQUENCE];
683                         //text = "Press SPACE to start!";
684                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
685                         SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
686                 break;
687
688                 case HIGH_SCORE_ENTRY:
689                         play_tune(2);
690                         if(!process_score_input()) {  // done inputting name
691
692                                 // Change state to briefly show high scores page
693                                 state = HIGH_SCORE_DISPLAY;
694                                 state_timeout = 200;
695
696                                 // Write the high score table to the file
697                                 write_high_score_table();
698                 
699                                 // Play the title page tune
700                                 play_tune(0);
701                         }
702                 // FALL THROUGH TO
703                 case HIGH_SCORE_DISPLAY:
704                         // Display de list o high scores mon.
705                         display_scores(surf_screen, 150,50);
706                         break;
707                 case GAMEPLAY:
708                 case DEAD_PAUSE:
709                         ; // no action necessary
710         }
711
712         if(!gameover && state == GAMEPLAY) {
713                 for(i=0; i<MAXROCKS; i++) {
714                         if(rock[i].active) {
715                                 if(collide(shipx-rock[i].x, shipy-rock[i].y, rock[i].shape, &shipshape)) 
716                                         bang = 1;
717                         }
718                 }
719         }
720
721         // Draw all the little ships
722         if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
723         for(i = 0; i<nships-1; i++) {
724                 src.w = surf_life->w;
725                 src.h = surf_life->h;
726                 dest.w = src.w;
727                 dest.h = src.h;
728                 dest.x = (i + 1)*(src.w + 10);
729                 dest.y = 20;
730                 SDL_BlitSurface(surf_life,&src,surf_screen,&dest);
731         }
732
733
734         // Update the score
735         /*
736         n = SDL_GetTicks()-initticks;
737         if(score)
738         ticks_since_last = n-score;
739         score = n;
740         */
741
742         ticks_since_last = SDL_GetTicks()-last_ticks;
743         last_ticks = SDL_GetTicks();
744         if(ticks_since_last>200 || ticks_since_last<0) {
745                 movementrate = 0;
746         }
747         else {
748                 movementrate = ticks_since_last/50.0;
749                 if(state == GAMEPLAY) {
750                         score += ticks_since_last;
751                 }
752         }
753
754         // Update the surface
755         SDL_Flip(surf_screen);
756
757
758         return bang;
759 }
760
761 int
762 gameloop() {
763         int i = 0;
764         Uint8 *keystate;
765
766
767         for(;;) {
768                 if(!paused) {
769                         // Count down the game loop timer, and change state when it gets to zero or less;
770
771                         if((state_timeout -= movementrate*3) < 0) {
772                                 switch(state) {
773                                         case DEAD_PAUSE:
774                                                 // Create a new ship and start all over again
775                                                 state = GAMEPLAY;
776                                                 play_tune(1);
777                                                 shipx -= 50;
778                                                 break;
779                                         case GAME_OVER:
780                                                 state = HIGH_SCORE_ENTRY;
781                                                 state_timeout = 5.0e6;
782                                                 if(new_high_score(score)) {
783                                                         SDL_Event e;
784                                                         SDL_EnableUNICODE(1);
785                                                         while(SDL_PollEvent(&e))
786                                                                 ;
787                                                 } else {
788                                                         state = HIGH_SCORE_DISPLAY;
789                                                         state_timeout = 400;
790                                                 }
791                                                 break;
792                                         case HIGH_SCORE_DISPLAY:
793                                                 state = TITLE_PAGE;
794                                                 state_timeout = 500.0;
795                                                 break;
796                                         case HIGH_SCORE_ENTRY:
797                                                 // state = TITLE_PAGE;
798                                                 // play_tune(1);
799                                                 // state_timeout = 100.0;
800                                                 break;
801                                         case TITLE_PAGE:
802                                                 state = HIGH_SCORE_DISPLAY;
803                                                 state_timeout = 200.0;
804                                                 break;
805                                         case GAMEPLAY:
806                                                 ; // no action necessary
807                                 }
808                         } else {
809                                 if(state == DEAD_PAUSE) {
810                                         float blast_radius;
811                                         int fixonly;
812
813                                         if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
814                                                 blast_radius = BLAST_RADIUS * 1.3;
815                                                 fixonly = 1;
816                                         } else {
817                                                 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
818                                                 fixonly = 0;
819                                         }
820
821                                         if(shipx < 60) shipx = 60;
822                                         for(i = 0; i<MAXROCKS; i++ ) {
823                                                 float dx, dy, n;
824                                                 if(rock[i].x <= 0) continue;
825
826                                                 // This makes it so your explosion from dying magically doesn't leave
827                                                 // any rocks that aren't moving much on the x axis. After the first
828                                                 // 20 tics, only rocks that are barely moving will be pushed.
829                                                 if(fixonly && (!rock[i].dead || rock[i].dx < -4 || rock[i].dx > 3)) {
830                                                         continue;
831                                                 }
832
833                                                 dx = rock[i].x - shipx;
834                                                 dy = rock[i].y - shipy;
835
836                                                 n = sqrt(dx*dx + dy*dy);
837                                                 if(n < blast_radius) {
838                                                         n *= 20;
839                                                         rock[i].dx += rockrate*(dx+30)/n;
840                                                         rock[i].dy += rockrate*dy/n;
841                                                         rock[i].dead = 1;
842                                                 }
843                                         }
844                                 }
845                         }
846
847                         if(--countdown <= 0 && (rnd()*100.0<(rockrate += 0.025))) {
848                                 // Create a rock
849                                 rockptr++;
850                                 if(rockptr-rock >= MAXROCKS) {
851                                         rockptr = rock;
852                                 }
853                                 if(!rockptr->active) {
854                                         rockptr->x = (float)XSIZE;
855                                         rockptr->dx = -(rockspeed)*(1 + rnd());
856                                         rockptr->dy = rnd()-0.5;
857                                         rockptr->type_number = random() % NROCKS;
858                                         rockptr->image = surf_rock[rockptr->type_number];// [random()%NROCKS];
859                                         rockptr->shape = &rock_shapes[rockptr->type_number];
860                                         rockptr->active = 1;
861                                         rockptr->y = rnd()*(YSIZE + rockptr->image->h);
862                                 }
863                                 if(movementrate>0.1) {
864                                         countdown = (int)(ROCKRATE/movementrate);
865                                 } else {
866                                         countdown = 0;
867                                 }
868                         }
869
870                         // FRICTION?
871                         if(friction) {
872                                 shipdx *= pow((double)0.9,(double)movementrate);
873                                 shipdy *= pow((double)0.9,(double)movementrate);
874                                 // if(abs(shipdx)<0.00001) shipdx = 0;
875                                 // if(abs(shipdy)<0.00001) shipdy = 0;
876                         }
877
878                         // INERTIA
879                         shipx += shipdx*movementrate;
880                         shipy += shipdy*movementrate;
881
882                         // SCROLLING
883                         yscroll = shipy - (YSIZE / 2);
884                         yscroll += shipdy * 25;
885                         yscroll /= -25;
886                         yscroll = ((scrollvel * (12 - movementrate)) + (yscroll * movementrate)) / 12;
887                         scrollvel = yscroll;
888                         yscroll = yscroll*movementrate;
889                         shipy += yscroll;
890                         
891                         // Move all the rocks
892                         for(i = 0; i < MAXROCKS; i++) {
893                                 if(rock[i].active) {
894                                         rock[i].x += rock[i].dx*movementrate;
895                                         rock[i].y += rock[i].dy*movementrate + yscroll;
896                                         if(rock[i].y > YSIZE || rock[i].y < -rock[i].image->h) {
897                                                 if(rock[i].dead) {
898                                                         rock[i].dead = 0;
899                                                         rock[i].active = 0;
900                                                 } else {
901                                                         // wrap
902                                                         rock[i].y = (YSIZE - rock[i].image->h) - rock[i].y;
903                                                         rock[i].y += (rock[i].dy*movementrate + yscroll) * 1.01;
904                                                 }
905                                         }
906                                         if(rock[i].x < -rock[i].image->w || rock[i].x > XSIZE) {
907                                                 rock[i].active = 0;
908                                                 rock[i].dead = 0;
909                                         }
910                                 }
911                         }
912
913
914                         // BOUNCE X
915                         if(shipx<0 || shipx>XSIZE-surf_ship->w) {
916                                 // BOUNCE from left and right wall
917                                 shipx -= shipdx*movementrate;
918                                 shipdx *= -0.99;
919                         }
920
921                         // BOUNCE Y
922                         if(shipy<0 || shipy>YSIZE-surf_ship->h) {
923                                 // BOUNCE from top and bottom wall
924                                 shipy -= shipdy;
925                                 shipdy *= -0.99;
926                         }
927
928
929                         if(draw() && state == GAMEPLAY) {
930                                 // Play the explosion sound
931                                 play_sound(0);
932                                 makebangdots(shipx,shipy,shipdx,shipdy,surf_ship,30);
933                                 if(--nships <= 0) {
934                                         gameover = 1;
935                                         state = GAME_OVER;
936                                         state_timeout = 200.0;
937                                         fadetimer = 0.0;
938                                         faderate = movementrate;
939                                 }
940                                 else {
941                                         state = DEAD_PAUSE;
942                                         state_timeout = DEAD_PAUSE_LENGTH;
943                                         shipdx = 0;
944                                         shipdy = 0;
945                                 }
946                         }
947
948                         SDL_PumpEvents();
949                         keystate = SDL_GetKeyState(NULL);
950
951                         if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
952                                 return 0;
953                         }
954
955                         if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
956
957                                 for(i = 0; i<MAXROCKS; i++ ) {
958                                         rock[i].active = 0;
959                                         rock[i].dead = 0;
960                                 }
961
962                                 rockrate = 54.0;
963                                 rockspeed = 5.0;
964
965                                 nships = 4;
966                                 score = 0;
967
968                                 state = GAMEPLAY;
969                                 play_tune(1);
970
971                                 gameover = 0;
972                                 shipx = 0;
973                                 shipy = YSIZE/2;
974                                 shipdx = -1;
975                                 shipdy = 0;
976                         }
977
978                         maneuver = 0;
979                 } else {
980                         SDL_PumpEvents();
981                         keystate = SDL_GetKeyState(NULL);
982                 }
983
984                 if(state == GAMEPLAY) {
985                         if(!gameover) {
986
987                                 if(!paused) {
988                                         if(keystate[SDLK_UP] | keystate[SDLK_c])                { shipdy -= 1.5*movementrate; maneuver |= 1<<3;}
989                                         if(keystate[SDLK_DOWN] | keystate[SDLK_t])              { shipdy += 1.5*movementrate; maneuver |= 1<<1;}
990                                         if(keystate[SDLK_LEFT] | keystate[SDLK_h])              { shipdx -= 1.5*movementrate; maneuver |= 1<<2;}
991                                         if(keystate[SDLK_RIGHT] | keystate[SDLK_n])             { shipdx += 1.5*movementrate; maneuver |= 1;}
992                                         if(keystate[SDLK_3])            { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
993                                 }
994
995                                 if(keystate[SDLK_p] | keystate[SDLK_s]) {
996                                         if(!pausedown) {
997                                                 paused = !paused;
998                                                 pausedown = 1;
999                                         }
1000                                 } else {
1001                                         pausedown = 0;
1002                                 }
1003
1004                         }
1005                         else {
1006                                 paused = 0;
1007                                 pausedown = 0;
1008                         }
1009                 } else if(state == GAME_OVER) {
1010                         if(keystate[SDLK_SPACE]) {
1011                                 state_timeout = -1;
1012                         }
1013                 }
1014         }
1015 }
1016
1017 int
1018 main(int argc, char **argv) {
1019         int i, x, fullscreen;
1020
1021         fullscreen = 0;
1022         tail_plume = 0;
1023         friction = 0;
1024         sound_flag = 1;
1025         music_flag = 0;
1026
1027         while ((x = getopt(argc,argv,"efhmps")) >= 0) {
1028                 switch(x) {
1029                         case 'e': // engine
1030                                 tail_plume = 1;
1031                         break;
1032                         case 'f': // fullscreen
1033                                 fullscreen = 1;
1034                         break;
1035                         case 'h': // help
1036                                 printf("Variations on RockDodger\n"
1037                                        " -e big tail [E]ngine\n"
1038                                        " -f [F]ull screen\n"
1039                                        " -h this [H]elp message\n"
1040                                        " -m enable [M]usic\n"
1041                                        " -p original [P]hysics (friction)\n"
1042                                        " -s [S]ilent (no sound)\n");
1043                                 exit(0);
1044                         break;
1045                         case 'm': // music
1046                                 music_flag = 1;
1047                         case 'p': // physics
1048                                 friction = 1;
1049                         break;
1050                         case 's': // silent
1051                                 sound_flag = 0;
1052                                 music_flag = 0;
1053                         break;
1054                 }
1055         }
1056
1057         if(init(fullscreen)) {
1058                 printf ("ta: '%s'\n",initerror);
1059                 return 1;
1060         }
1061
1062         for(i = 0; i<MAXROCKS; i++) {
1063                 rock[i].active = 0;
1064                 rock[i].dead = 0;
1065         }
1066         rockrate = 54.0;
1067         rockspeed = 5.0;
1068         initticks = SDL_GetTicks();
1069         gameloop();
1070
1071         return 0;
1072 }