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