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