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
36 #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 struct shape shipshape;
72 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
73 float shipdx = 7.5, shipdy = 0.0; // Change in X position per tick.
74 float screendx = 7.5, screendy = 0.0;
75 float xscroll, yscroll;
76 float gamerate; // this controls the speed of everything that moves.
78 float bangx, bangy, bangdx, bangdy;
80 int nships,score,game_ticks,ticks_since_last,last_ticks;
83 int sound_flag, music_flag;
84 int tail_plume; // display big engine at the back?
85 int friction; // should there be friction?
86 float fadetimer = 0, faderate;
88 int pausedown = 0, paused = 0;
90 // bangdot start (bd1) and end (bd2) position:
101 enum states state = TITLE_PAGE;
102 float state_timeout = 600.0;
106 "Press SPACE to start",
107 "http://qualdan.com/vor/"
110 int bangdotlife, nbangdots;
111 Uint16 heatcolor[W*3];
115 extern int optind, opterr, optopt;
117 // ************************************* FUNCS
121 return (float)random()/(float)RAND_MAX;
127 for(i = 0; i<MAXENGINEDOTS; i++) {
135 for(i = 0; i<MAXSPACEDOTS; i++) {
136 sdot[i].x = rnd()*(XSIZE-5);
137 sdot[i].y = rnd()*(YSIZE-5);
138 sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
139 b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
140 sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
145 make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
147 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
148 // TODO - generate and display dots in a circular buffer
155 begin_generate = SDL_GetTicks();
158 rawpixel = (Uint16 *) s->pixels;
160 //for(n = 0; n <= power/2; n++) {
164 for(x = 0; x<s->w; x++) {
165 for(y = 0; y<s->h; y++) {
166 c = rawpixel[s->pitch/2*y + x];
167 if(c && c != s->format->colorkey) {
169 theta = rnd()*M_PI*2;
173 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
174 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
175 bdot[bd2].x = x + xbang;
176 bdot[bd2].y = y + ybang;
178 // Replace the last few bang dots with the pixels from the exploding object
179 bdot[bd2].c = (endcount>0)?c:0;
180 bdot[bd2].life = 100;
181 bdot[bd2].decay = rnd()*3 + 1;
182 bdot[bd2].active = 1;
187 // If the circular buffer is filled, who cares? They've had their chance.
188 //if(bd2 == bd1-1) goto exitloop;
194 if(SDL_GetTicks() - begin_generate > 7) endcount++;
197 SDL_UnlockSurface(s);
202 draw_bang_dots(SDL_Surface *s) {
206 rawpixel = (Uint16 *) s->pixels;
211 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
215 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
216 // If the dot has drifted outside the perimeter, kill it
224 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)];
225 bdot[i].life -= bdot[i].decay;
226 bdot[i].x += bdot[i].dx*gamerate - xscroll;
227 bdot[i].y += bdot[i].dy*gamerate - yscroll;
247 draw_space_dots(SDL_Surface *s) {
250 rawpixel = (Uint16 *) s->pixels;
252 for(i = 0; i<MAXSPACEDOTS; i++) {
256 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
257 sdot[i].x -= xscroll / (1.3 + sdot[i].z);
258 sdot[i].y -= yscroll / (1.3 + sdot[i].z);
259 if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
260 else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
261 if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
262 else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
267 draw_engine_dots(SDL_Surface *s) {
270 rawpixel = (Uint16 *) s->pixels;
272 for(i = 0; i<MAXENGINEDOTS; i++) {
274 edot[i].x += edot[i].dx*gamerate - xscroll;
275 edot[i].y += edot[i].dy*gamerate - yscroll;
276 if((edot[i].life -= gamerate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
278 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
282 heatindex = edot[i].life * 6;
283 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
284 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
291 create_engine_dots(int newdots) {
293 double theta,r,dx,dy;
295 if(!tail_plume) return;
297 if(state == GAMEPLAY) {
298 for(i = 0; i<newdots*gamerate; i++) {
299 if(dotptr->active == 0) {
300 theta = rnd()*M_PI*2;
306 dotptr->x = shipx + surf_ship->w/2-14;
307 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*5-1;
308 dotptr->dx = 10*(dx-1.5) + shipdx;
309 dotptr->dy = 1*dy + shipdy;
310 dotptr->life = 45 + rnd(1)*5;
313 if(dotptr-edot >= MAXENGINEDOTS) {
322 create_engine_dots2(int newdots, int m) {
324 double theta, theta2, dx, dy, adx, ady;
326 // Don't create fresh engine dots when
327 // the game is not being played and a demo is not beng shown
328 if(state != GAMEPLAY) return;
330 for(i = 0; i<newdots; i++) {
331 if(dotptr->active == 0) {
332 theta = rnd()*M_PI*2;
333 theta2 = rnd()*M_PI*2;
335 dx = cos(theta) * fabs(cos(theta2));
336 dy = sin(theta) * fabs(cos(theta2));
342 dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
343 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
348 dotptr->dx = -20*adx + shipdx;
349 dotptr->dy = 2*dy + shipdy;
350 dotptr->life = 60 * adx;
353 dotptr->dx = 2*dx + shipdx;
354 dotptr->dy = -20*ady + shipdy;
355 dotptr->life = 60 * ady;
359 dotptr->dx = 20*adx + shipdx;
360 dotptr->dy = 2*dy + shipdy;
361 dotptr->life = 60 * adx;
364 dotptr->dx = 2*dx + shipdx;
365 dotptr->dy = 20*ady + shipdy;
366 dotptr->life = 60 * ady;
370 if(dotptr-edot >= MAXENGINEDOTS) {
378 drawdots(SDL_Surface *s) {
381 // Create more engine dots comin' out da back
382 if(!gameover) create_engine_dots(200);
384 // Create engine dots out the side we're moving from
385 for(m = 0; m<4; m++) {
386 if(maneuver & 1<<m) { // 'maneuver' is a bit field
387 create_engine_dots2(80,m);
395 SDL_UnlockSurface(s);
399 init(int fullscreen) {
405 // Where are our data files?
406 if(!find_files()) exit(1);
407 read_high_score_table();
410 // Initialize SDL with audio and video
411 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
413 printf ("Can't open sound, starting without it\n");
417 atexit(SDL_CloseAudio);
418 sound_flag = init_sound();
421 // Initialize with video only
422 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
428 // Attempt to get the required video size
429 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
430 if(fullscreen) flag |= SDL_FULLSCREEN;
431 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
433 // Set the title bar text
434 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
436 NULLERROR(surf_screen);
438 // Set the heat color from the range 0 (cold) to 300 (blue-white)
439 for(i = 0; i<W*3; i++) {
440 heatcolor[i] = SDL_MapRGB(
442 (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?
447 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
448 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
450 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
451 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
453 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
454 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
456 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
457 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
459 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
460 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
462 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
463 g_font = SFont_InitFont(surf_font_big);
465 // Load the spaceship graphic.
466 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
467 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
468 get_shape(surf_ship, &shipshape);
470 // Load the life indicator (small ship) graphic.
471 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
472 NULLERROR(surf_life = SDL_DisplayFormat(temp));
479 // Remove the mouse cursor
481 SDL_ShowCursor(SDL_DISABLE);
493 float fadegame,fadeover;
497 // Draw a fully black background
498 SDL_FillRect(surf_screen,NULL,0);
500 // Draw the background dots
501 drawdots(surf_screen);
504 if(!gameover && state == GAMEPLAY ) {
507 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
512 // Draw the life indicators.
513 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
514 for(i = 0; i<nships-1; i++) {
515 dest.x = (i + 1)*(surf_life->w + 10);
517 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
521 snprintscore_line(topline, 50, score);
522 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
524 // If it's game over, show the game over graphic in the dead centre
527 if(fadetimer<3.0/faderate) {
528 fadegame = fadetimer/(3.0/faderate);
533 if(fadetimer<3.0/faderate) {
535 } else if(fadetimer<6.0/faderate) {
536 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
541 dest.x = (XSIZE-surf_b_game->w)/2;
542 dest.y = (YSIZE-surf_b_game->h)/2-40;
543 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += gamerate/1.0))));
544 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
546 dest.x = (XSIZE-surf_b_over->w)/2;
547 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
548 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
549 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
554 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
555 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
556 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += gamerate/2.0)));
557 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
559 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
560 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
561 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
562 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
564 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
565 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
566 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
567 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
569 text = "Version " VERSION;
570 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
571 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
573 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
574 //text = "Press SPACE to start!";
575 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
576 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
579 case HIGH_SCORE_ENTRY:
581 if(!process_score_input()) { // done inputting name
583 // Change state to briefly show high scores page
584 state = HIGH_SCORE_DISPLAY;
587 // Write the high score table to the file
588 write_high_score_table();
590 // Play the title page tune
594 case HIGH_SCORE_DISPLAY:
595 // Display de list o high scores mon.
596 display_scores(surf_screen, 150,50);
600 ; // no action necessary
603 if(!gameover && state == GAMEPLAY) {
604 bang = hit_rocks(shipx, shipy, &shipshape);
607 ticks_since_last = SDL_GetTicks()-last_ticks;
608 last_ticks = SDL_GetTicks();
609 if(ticks_since_last>200 || ticks_since_last<0) {
613 gamerate = GAMESPEED*ticks_since_last/50.0;
614 if(state == GAMEPLAY) {
615 score += ticks_since_last;
619 // Update the surface
620 SDL_Flip(surf_screen);
634 // Count down the game loop timer, and change state when it gets to zero or less;
636 if((state_timeout -= gamerate*3) < 0) {
639 // Create a new ship and start all over again
644 state = HIGH_SCORE_ENTRY;
645 state_timeout = 5.0e6;
646 if(new_high_score(score)) {
648 SDL_EnableUNICODE(1);
649 while(SDL_PollEvent(&e))
652 state = HIGH_SCORE_DISPLAY;
656 case HIGH_SCORE_DISPLAY:
658 state_timeout = 500.0;
660 case HIGH_SCORE_ENTRY:
661 // state = TITLE_PAGE;
663 // state_timeout = 100.0;
666 state = HIGH_SCORE_DISPLAY;
667 state_timeout = 200.0;
670 ; // no action necessary
673 if(state == DEAD_PAUSE) {
677 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
678 blast_radius = BLAST_RADIUS * 1.3;
681 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
684 blast_rocks(bangx, bangy, blast_radius, fixonly);
686 if(bangx < 60) bangx = 60;
694 shipdx *= pow((double)0.9,(double)gamerate);
695 shipdy *= pow((double)0.9,(double)gamerate);
699 shipx += shipdx*gamerate;
700 shipy += shipdy*gamerate;
703 tmp = shipy - (YSIZE / 2);
706 tmp = ((screendy * (gamerate - 12)) + (tmp * gamerate)) / 12;
708 tmp = shipx - (XSIZE / 3);
711 tmp = ((screendx * (gamerate - 12)) + (tmp * gamerate)) / 12;
713 if(screendx < 7.5) screendx=7.5;
715 xscroll = screendx * gamerate;
716 yscroll = screendy * gamerate;
721 bangx += bangdx*gamerate - xscroll;
722 bangy += bangdy*gamerate - yscroll;
728 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
729 // BOUNCE from left and right wall
730 shipx -= (shipdx-screendx)*gamerate;
731 shipdx = 2*screendx-shipdx;
735 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
736 // BOUNCE from top and bottom wall
737 shipy -= (shipdy-screendy)*gamerate;
738 shipdy = 2*screendy-shipdy;
742 if(draw() && state == GAMEPLAY) {
744 play_sound(0); // Play the explosion sound
745 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
746 make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
747 shipdx *= 0.5; shipdy *= 0.5;
751 shipdx = 8; shipdy = 0;
752 state_timeout = 200.0;
758 state_timeout = DEAD_PAUSE_LENGTH;
763 keystate = SDL_GetKeyState(NULL);
766 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
779 shipx = XSIZE/2.2; shipy = YSIZE/2;
780 shipdx = screendx; shipdy = screendy;
786 keystate = SDL_GetKeyState(NULL);
789 if(state == GAMEPLAY) {
793 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*gamerate; maneuver |= 1<<3;}
794 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*gamerate; maneuver |= 1<<1;}
795 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*gamerate; maneuver |= 1<<2;}
796 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*gamerate; maneuver |= 1;}
797 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
800 if(keystate[SDLK_p] | keystate[SDLK_s]) {
814 } else if(state == GAME_OVER) {
815 if(keystate[SDLK_SPACE]) {
820 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
828 main(int argc, char **argv) {
837 while ((x = getopt(argc,argv,"efhmps")) >= 0) {
842 case 'f': // fullscreen
846 printf("Variations on RockDodger\n"
847 " -e big tail [E]ngine\n"
848 " -f [F]ull screen\n"
849 " -h this [H]elp message\n"
850 " -m enable [M]usic\n"
851 " -p original [P]hysics (friction)\n"
852 " -s [S]ilent (no sound)\n");
867 if(init(fullscreen)) {
868 printf ("ta: '%s'\n",initerror);