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