1 /* Variations on RockDodger
2 * Space Rocks copyright (C) 2001 Paul Holt <pad@pcholt.com>
4 * Project fork 2004, Jason Woofenden and Joshua Grams.
5 * (a whole bunch of modifications and project rename)
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.
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.
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
37 #include <SDL/SDL_image.h>
45 // ************************************* VARS
46 // SDL_Surface global variables
48 *surf_screen, // Screen
49 *surf_b_variations, // "variations" banner
50 *surf_b_on, // "on" banner
51 *surf_b_rockdodger, // "rockdodger" banner
52 *surf_b_game, // Title element "game"
53 *surf_b_over, // Title element "over"
54 *surf_ship, // Spaceship element
55 *surf_life, // Indicator of number of ships remaining
56 *surf_rock[NROCKS], // THE ROCKS
57 *surf_font_big; // The big font
62 // Structure global variables
63 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
64 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
65 struct spacedot sdot[MAXSPACEDOTS];
67 // Other global variables
71 // Command-line argument parsing
79 const char *argp_program_version = "Variations on Rockdodger " VERSION;
80 const char *argp_program_bug_address = "<josh@qualdan.com>";
81 static char doc[] = "VoR: Dodge the rocks until you die.";
82 static struct argp_option opts[] = {
83 {0, 0, 0, 0, "Basic Options:"},
84 {"full-screen", 'f', 0, 0, ""},
85 {"music", 'm', 0, 0, "Enable music"},
86 {"silent", 's', 0, 0, "Turn off explosion sounds"},
87 {0, 0, 0, 0, "Gameplay Options:"},
88 {"game-speed", 'g', "N%", 0, "Game speed [50-100%]"},
89 {"engine", 'e', 0, 0, "Display large tail plume"},
90 {"old-physics", 'p', 0, 0, "Original physics (i.e. friction)."},
93 error_t parse_opt(int, char*, struct argp_state *);
94 static struct argp argp = { opts, &parse_opt, 0, doc };
97 struct shape shipshape;
98 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
99 float shipdx = SCREENDXMIN, shipdy = 0.0; // Change in X position per tick.
100 float screendx = SCREENDXMIN, screendy = 0.0;
101 float xscroll, yscroll;
102 float gamerate; // this controls the speed of everything that moves.
104 float bangx, bangy, bangdx, bangdy;
106 int nships,score,ticks_since_last,last_ticks;
110 float fadetimer = 0, faderate;
112 int pausedown = 0, paused = 0;
114 // bangdot start (bd1) and end (bd2) position:
115 int bd1 = 0, bd2 = 0;
125 enum states state = TITLE_PAGE;
126 float state_timeout = 600.0;
130 "Press SPACE to start",
131 "http://herkamire.com/jason/vor"
134 int bangdotlife, nbangdots;
135 Uint16 heatcolor[W*3];
139 extern int optind, opterr, optopt;
141 // ************************************* FUNCS
149 opt_gamespeed = 1.00; // Run game at full speed.
150 // These switch back to the old gameplay and are off by default.
156 parse_opt(int key, char *arg, struct argp_state *state)
161 case 'f': opt_fullscreen = 1; break;
162 case 'm': opt_music = 1; break;
163 case 's': opt_sound = 0; opt_music = 0; break;
164 case 'g': sscanf(arg, "%d%%", &i);
165 if(i < 50) i = 50; else if(i > 100) i = 100;
166 opt_gamespeed = (float)i / 100;
168 case 'e': opt_tail_engine = 1; break;
169 case 'p': opt_friction = 1; break;
177 return (float)random()/(float)RAND_MAX;
183 for(i = 0; i<MAXENGINEDOTS; i++) {
191 for(i = 0; i<MAXSPACEDOTS; i++) {
192 sdot[i].x = rnd()*(XSIZE-5);
193 sdot[i].y = rnd()*(YSIZE-5);
194 sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
195 b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
196 sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
201 make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
203 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
204 // TODO - generate and display dots in a circular buffer
211 begin_generate = SDL_GetTicks();
214 rawpixel = (Uint16 *) s->pixels;
216 //for(n = 0; n <= power/2; n++) {
220 for(x = 0; x<s->w; x++) {
221 for(y = 0; y<s->h; y++) {
222 c = rawpixel[s->pitch/2*y + x];
223 if(c && c != s->format->colorkey) {
225 theta = rnd()*M_PI*2;
229 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
230 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
231 bdot[bd2].x = x + xbang;
232 bdot[bd2].y = y + ybang;
234 // Replace the last few bang dots with the pixels from the exploding object
235 bdot[bd2].c = (endcount>0)?c:0;
236 bdot[bd2].life = 100;
237 bdot[bd2].decay = rnd()*3 + 1;
238 bdot[bd2].active = 1;
243 // If the circular buffer is filled, who cares? They've had their chance.
244 //if(bd2 == bd1-1) goto exitloop;
250 if(SDL_GetTicks() - begin_generate > 7) endcount++;
253 SDL_UnlockSurface(s);
258 draw_bang_dots(SDL_Surface *s) {
262 rawpixel = (Uint16 *) s->pixels;
267 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
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
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*gamerate - xscroll;
283 bdot[i].y += bdot[i].dy*gamerate - yscroll;
303 draw_space_dots(SDL_Surface *s) {
306 rawpixel = (Uint16 *) s->pixels;
308 for(i = 0; i<MAXSPACEDOTS; i++) {
312 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
313 sdot[i].x -= xscroll / (1.3 + sdot[i].z);
314 sdot[i].y -= yscroll / (1.3 + sdot[i].z);
315 if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
316 else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
317 if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
318 else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
323 draw_engine_dots(SDL_Surface *s) {
326 rawpixel = (Uint16 *) s->pixels;
328 for(i = 0; i<MAXENGINEDOTS; i++) {
330 edot[i].x += edot[i].dx*gamerate - xscroll;
331 edot[i].y += edot[i].dy*gamerate - yscroll;
332 if((edot[i].life -= gamerate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
334 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
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];
347 create_engine_dots(int newdots) {
349 double theta,r,dx,dy;
351 if(!opt_tail_engine) return;
353 if(state == GAMEPLAY) {
354 for(i = 0; i<newdots*gamerate; i++) {
355 if(dotptr->active == 0) {
356 theta = rnd()*M_PI*2;
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;
369 if(dotptr-edot >= MAXENGINEDOTS) {
378 create_engine_dots2(int newdots, int m) {
380 double theta, theta2, dx, dy, adx, ady;
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;
386 for(i = 0; i<newdots; i++) {
387 if(dotptr->active == 0) {
388 theta = rnd()*M_PI*2;
389 theta2 = rnd()*M_PI*2;
391 dx = cos(theta) * fabs(cos(theta2));
392 dy = sin(theta) * fabs(cos(theta2));
398 dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
399 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
404 dotptr->dx = -20*adx + shipdx;
405 dotptr->dy = 2*dy + shipdy;
406 dotptr->life = 60 * adx;
409 dotptr->dx = 2*dx + shipdx;
410 dotptr->dy = -20*ady + shipdy;
411 dotptr->life = 60 * ady;
415 dotptr->dx = 20*adx + shipdx;
416 dotptr->dy = 2*dy + shipdy;
417 dotptr->life = 60 * adx;
420 dotptr->dx = 2*dx + shipdx;
421 dotptr->dy = 20*ady + shipdy;
422 dotptr->life = 60 * ady;
426 if(dotptr-edot >= MAXENGINEDOTS) {
434 drawdots(SDL_Surface *s) {
437 // Create more engine dots comin' out da back
438 if(!gameover) create_engine_dots(200);
440 // Create engine dots out the side we're moving from
441 for(m = 0; m<4; m++) {
442 if(maneuver & 1<<m) { // 'maneuver' is a bit field
443 create_engine_dots2(80,m);
451 SDL_UnlockSurface(s);
461 // Where are our data files?
462 if(!find_files()) exit(1);
463 read_high_score_table();
466 // Initialize SDL with audio and video
467 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
469 printf ("Can't open sound, starting without it\n");
473 atexit(SDL_CloseAudio);
474 opt_sound = init_sound();
477 // Initialize with video only
478 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
484 // Attempt to get the required video size
485 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
486 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
487 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
489 // Set the title bar text
490 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
492 NULLERROR(surf_screen);
494 // Set the heat color from the range 0 (cold) to 300 (blue-white)
495 for(i = 0; i<W*3; i++) {
496 heatcolor[i] = SDL_MapRGB(
498 (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?
503 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
504 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
506 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
507 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
509 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
510 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
512 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
513 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
515 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
516 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
518 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
519 g_font = SFont_InitFont(surf_font_big);
521 // Load the spaceship graphic.
522 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
523 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
524 get_shape(surf_ship, &shipshape);
526 // Load the life indicator (small ship) graphic.
527 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
528 NULLERROR(surf_life = SDL_DisplayFormat(temp));
535 // Remove the mouse cursor
537 SDL_ShowCursor(SDL_DISABLE);
549 float fadegame,fadeover;
553 // Draw a fully black background
554 SDL_FillRect(surf_screen,NULL,0);
556 // Draw the background dots
557 drawdots(surf_screen);
560 if(!gameover && state == GAMEPLAY ) {
563 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
568 // Draw the life indicators.
569 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
570 for(i = 0; i<nships-1; i++) {
571 dest.x = (i + 1)*(surf_life->w + 10);
573 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
577 snprintscore_line(topline, 50, score);
578 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
580 // If it's game over, show the game over graphic in the dead centre
583 if(fadetimer<3.0/faderate) {
584 fadegame = fadetimer/(3.0/faderate);
589 if(fadetimer<3.0/faderate) {
591 } else if(fadetimer<6.0/faderate) {
592 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
597 dest.x = (XSIZE-surf_b_game->w)/2;
598 dest.y = (YSIZE-surf_b_game->h)/2-40;
599 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += gamerate/1.0))));
600 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
602 dest.x = (XSIZE-surf_b_over->w)/2;
603 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
604 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
605 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
610 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
611 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
612 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += gamerate/2.0)));
613 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
615 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
616 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
617 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
618 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
620 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
621 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
622 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
623 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
625 text = "Version " VERSION;
626 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
627 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
629 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
630 //text = "Press SPACE to start!";
631 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
632 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
635 case HIGH_SCORE_ENTRY:
637 if(!process_score_input()) { // done inputting name
639 // Change state to briefly show high scores page
640 state = HIGH_SCORE_DISPLAY;
643 // Write the high score table to the file
644 write_high_score_table();
646 // Play the title page tune
650 case HIGH_SCORE_DISPLAY:
651 // Display de list o high scores mon.
652 display_scores(surf_screen, 150,50);
656 ; // no action necessary
659 if(!gameover && state == GAMEPLAY) {
660 bang = hit_rocks(shipx, shipy, &shipshape);
663 ticks_since_last = SDL_GetTicks()-last_ticks;
664 last_ticks = SDL_GetTicks();
665 if(ticks_since_last>200 || ticks_since_last<0) {
669 gamerate = opt_gamespeed*ticks_since_last/50.0;
670 if(state == GAMEPLAY) {
671 score += ticks_since_last;
675 // Update the surface
676 SDL_Flip(surf_screen);
690 // Count down the game loop timer, and change state when it gets to zero or less;
692 if((state_timeout -= gamerate*3) < 0) {
695 // Create a new ship and start all over again
700 state = HIGH_SCORE_ENTRY;
701 state_timeout = 5.0e6;
702 if(new_high_score(score)) {
704 SDL_EnableUNICODE(1);
705 while(SDL_PollEvent(&e))
708 state = HIGH_SCORE_DISPLAY;
712 case HIGH_SCORE_DISPLAY:
714 state_timeout = 500.0;
716 case HIGH_SCORE_ENTRY:
717 // state = TITLE_PAGE;
719 // state_timeout = 100.0;
722 state = HIGH_SCORE_DISPLAY;
723 state_timeout = 200.0;
726 ; // no action necessary
729 if(state == DEAD_PAUSE) {
733 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
734 blast_radius = BLAST_RADIUS * 1.3;
737 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
740 blast_rocks(bangx, bangy, blast_radius, fixonly);
742 if(bangx < 60) bangx = 60;
750 shipdx *= pow((double)0.9,(double)gamerate);
751 shipdy *= pow((double)0.9,(double)gamerate);
755 shipx += shipdx*gamerate;
756 shipy += shipdy*gamerate;
759 tmp = shipy - (YSIZE / 2);
762 tmp = ((screendy * (gamerate - 12)) + (tmp * gamerate)) / 12;
764 tmp = shipx - (XSIZE / 3);
767 tmp = ((screendx * (gamerate - 12)) + (tmp * gamerate)) / 12;
769 if(screendx < SCREENDXMIN) screendx=SCREENDXMIN;
771 xscroll = screendx * gamerate;
772 yscroll = screendy * gamerate;
777 bangx += bangdx*gamerate - xscroll;
778 bangy += bangdy*gamerate - yscroll;
784 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
785 // BOUNCE from left and right wall
786 shipx -= (shipdx-screendx)*gamerate;
787 shipdx = 2*screendx-shipdx;
791 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
792 // BOUNCE from top and bottom wall
793 shipy -= (shipdy-screendy)*gamerate;
794 shipdy = 2*screendy-shipdy;
798 if(draw() && state == GAMEPLAY) {
800 play_sound(0); // Play the explosion sound
801 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
802 make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
803 shipdx *= 0.5; shipdy *= 0.5;
804 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
808 shipdx = 8; shipdy = 0;
809 state_timeout = 200.0;
815 state_timeout = DEAD_PAUSE_LENGTH;
820 keystate = SDL_GetKeyState(NULL);
823 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
834 shipx = XSIZE/2.2; shipy = YSIZE/2;
835 shipdx = screendx; shipdy = screendy;
841 keystate = SDL_GetKeyState(NULL);
844 if(state == GAMEPLAY) {
848 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*gamerate; maneuver |= 1<<3;}
849 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*gamerate; maneuver |= 1<<1;}
850 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*gamerate; maneuver |= 1<<2;}
851 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*gamerate; maneuver |= 1;}
852 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
855 if(keystate[SDLK_p] | keystate[SDLK_s]) {
869 } else if(state == GAME_OVER) {
870 if(keystate[SDLK_SPACE]) {
875 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
883 main(int argc, char **argv) {
886 argp_parse(&argp, argc, argv, 0, 0, 0);
889 printf ("ta: '%s'\n",initerror);