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_speed, // Speed indicator
57 *surf_rock[NROCKS], // THE ROCKS
58 *surf_font_big; // The big font
63 // Structure global variables
64 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
65 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
66 struct spacedot sdot[MAXSPACEDOTS];
68 // Other global variables
72 struct shape shipshape;
73 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
74 float shipdx = 8, shipdy = 0; // Change in X position per tick.
75 float screendx = 7.5, screendy = 0.0;
76 float xscroll, yscroll;
77 float gamerate; // this controls the speed of everything that moves.
79 float bangx, bangy, bangdx, bangdy;
81 float game_dist, avg_speed, cur_speed;
82 uint16_t avg_speed_w, cur_speed_w; // [0, 74]
84 int nships,score,game_ticks,ticks_since_last,last_ticks;
87 int sound_flag, music_flag;
88 int tail_plume; // display big engine at the back?
89 int friction; // should there be friction?
90 float fadetimer = 0, faderate;
92 int pausedown = 0, paused = 0;
94 // bangdot start (bd1) and end (bd2) position:
105 enum states state = TITLE_PAGE;
106 float state_timeout = 600.0;
110 "Press SPACE to start",
111 "http://qualdan.com/vor/"
114 int bangdotlife, nbangdots;
115 Uint16 heatcolor[W*3];
119 extern int optind, opterr, optopt;
121 // ************************************* FUNCS
125 return (float)random()/(float)RAND_MAX;
131 for(i = 0; i<MAXENGINEDOTS; i++) {
139 for(i = 0; i<MAXSPACEDOTS; i++) {
140 sdot[i].x = rnd()*(XSIZE-5);
141 sdot[i].y = rnd()*(YSIZE-5);
142 sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
143 b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
144 sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
149 make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
151 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
152 // TODO - generate and display dots in a circular buffer
159 begin_generate = SDL_GetTicks();
162 rawpixel = (Uint16 *) s->pixels;
164 //for(n = 0; n <= power/2; n++) {
168 for(x = 0; x<s->w; x++) {
169 for(y = 0; y<s->h; y++) {
170 c = rawpixel[s->pitch/2*y + x];
171 if(c && c != s->format->colorkey) {
173 theta = rnd()*M_PI*2;
177 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
178 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
179 bdot[bd2].x = x + xbang;
180 bdot[bd2].y = y + ybang;
182 // Replace the last few bang dots with the pixels from the exploding object
183 bdot[bd2].c = (endcount>0)?c:0;
184 bdot[bd2].life = 100;
185 bdot[bd2].decay = rnd()*3 + 1;
186 bdot[bd2].active = 1;
191 // If the circular buffer is filled, who cares? They've had their chance.
192 //if(bd2 == bd1-1) goto exitloop;
198 if(SDL_GetTicks() - begin_generate > 7) endcount++;
201 SDL_UnlockSurface(s);
206 draw_bang_dots(SDL_Surface *s) {
210 rawpixel = (Uint16 *) s->pixels;
215 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
219 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
220 // If the dot has drifted outside the perimeter, kill it
228 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)];
229 bdot[i].life -= bdot[i].decay;
230 bdot[i].x += bdot[i].dx*gamerate - xscroll;
231 bdot[i].y += bdot[i].dy*gamerate - yscroll;
251 draw_space_dots(SDL_Surface *s) {
254 rawpixel = (Uint16 *) s->pixels;
256 for(i = 0; i<MAXSPACEDOTS; i++) {
260 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
261 sdot[i].x -= xscroll / (1.3 + sdot[i].z);
262 sdot[i].y -= yscroll / (1.3 + sdot[i].z);
263 if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
264 else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
265 if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
266 else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
271 draw_engine_dots(SDL_Surface *s) {
274 rawpixel = (Uint16 *) s->pixels;
276 for(i = 0; i<MAXENGINEDOTS; i++) {
278 edot[i].x += edot[i].dx*gamerate - xscroll;
279 edot[i].y += edot[i].dy*gamerate - yscroll;
280 if((edot[i].life -= gamerate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
282 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
286 heatindex = edot[i].life * 6;
287 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
288 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
295 create_engine_dots(int newdots) {
297 double theta,r,dx,dy;
299 if(!tail_plume) return;
301 if(state == GAMEPLAY) {
302 for(i = 0; i<newdots*gamerate; i++) {
303 if(dotptr->active == 0) {
304 theta = rnd()*M_PI*2;
310 dotptr->x = shipx + surf_ship->w/2-14;
311 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*5-1;
312 dotptr->dx = 10*(dx-1.5) + shipdx;
313 dotptr->dy = 1*dy + shipdy;
314 dotptr->life = 45 + rnd(1)*5;
317 if(dotptr-edot >= MAXENGINEDOTS) {
326 create_engine_dots2(int newdots, int m) {
328 double theta, theta2, dx, dy, adx, ady;
330 // Don't create fresh engine dots when
331 // the game is not being played and a demo is not beng shown
332 if(state != GAMEPLAY) return;
334 for(i = 0; i<newdots; i++) {
335 if(dotptr->active == 0) {
336 theta = rnd()*M_PI*2;
337 theta2 = rnd()*M_PI*2;
339 dx = cos(theta) * fabs(cos(theta2));
340 dy = sin(theta) * fabs(cos(theta2));
346 dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
347 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
352 dotptr->dx = -20*adx + shipdx;
353 dotptr->dy = 2*dy + shipdy;
354 dotptr->life = 60 * adx;
357 dotptr->dx = 2*dx + shipdx;
358 dotptr->dy = -20*ady + shipdy;
359 dotptr->life = 60 * ady;
363 dotptr->dx = 20*adx + shipdx;
364 dotptr->dy = 2*dy + shipdy;
365 dotptr->life = 60 * adx;
368 dotptr->dx = 2*dx + shipdx;
369 dotptr->dy = 20*ady + shipdy;
370 dotptr->life = 60 * ady;
374 if(dotptr-edot >= MAXENGINEDOTS) {
382 drawdots(SDL_Surface *s) {
385 // Create more engine dots comin' out da back
386 if(!gameover) create_engine_dots(200);
388 // Create engine dots out the side we're moving from
389 for(m = 0; m<4; m++) {
390 if(maneuver & 1<<m) { // 'maneuver' is a bit field
391 create_engine_dots2(80,m);
399 SDL_UnlockSurface(s);
403 init(int fullscreen) {
409 // Where are our data files?
410 if(!find_files()) exit(1);
411 read_high_score_table();
414 // Initialize SDL with audio and video
415 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
417 printf ("Can't open sound, starting without it\n");
421 atexit(SDL_CloseAudio);
422 sound_flag = init_sound();
425 // Initialize with video only
426 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
432 // Attempt to get the required video size
433 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
434 if(fullscreen) flag |= SDL_FULLSCREEN;
435 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
437 // Set the title bar text
438 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
440 NULLERROR(surf_screen);
442 // Set the heat color from the range 0 (cold) to 300 (blue-white)
443 for(i = 0; i<W*3; i++) {
444 heatcolor[i] = SDL_MapRGB(
446 (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?
451 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
452 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
454 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
455 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
457 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
458 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
460 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
461 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
463 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
464 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
466 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
467 g_font = SFont_InitFont(surf_font_big);
469 // Load the spaceship graphic.
470 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
471 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
472 get_shape(surf_ship, &shipshape);
474 // Load the life indicator (small ship) graphic.
475 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
476 NULLERROR(surf_life = SDL_DisplayFormat(temp));
478 NULLERROR(temp = IMG_Load(add_path("indicators/speed.png")));
479 NULLERROR(surf_speed = SDL_DisplayFormat(temp));
486 // Remove the mouse cursor
488 SDL_ShowCursor(SDL_DISABLE);
500 float fadegame,fadeover;
504 // Draw a fully black background
505 SDL_FillRect(surf_screen,NULL,0);
507 // Draw the background dots
508 drawdots(surf_screen);
511 if(!gameover && state == GAMEPLAY ) {
514 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
519 // Draw the life indicators.
520 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
521 for(i = 0; i<nships-1; i++) {
522 dest.x = (i + 1)*(surf_life->w + 10);
524 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
527 if(state == GAMEPLAY) {
530 if(shipdx < 0) cur_speed = 0;
531 if(shipdx > 20) cur_speed = 20;
532 game_dist += cur_speed*ticks_since_last;
533 game_ticks += ticks_since_last;
534 if(game_ticks < 2*1000) avg_speed = cur_speed;
535 else avg_speed = game_dist/game_ticks;
536 //printf("avg=%.2f, cur=%.2f. shipdx=%.2f\n", avg_speed, cur_speed, shipdx);
537 avg_speed_w = 2 + 64*avg_speed/20;
538 cur_speed_w = 2 + 64*cur_speed/20;
541 if(state == GAMEPLAY || state == DEAD_PAUSE) {
542 // Draw the speed indicators.
543 src.x = 0; src.y = 0;
544 src.h = surf_speed->h;
546 src.w = avg_speed_w; dest.y = 10;
547 SDL_BlitSurface(surf_speed, &src, surf_screen, &dest);
548 src.w = cur_speed_w; dest.y = 20;
549 SDL_BlitSurface(surf_speed, &src, surf_screen, &dest);
553 snprintscore_line(topline, 50, score);
554 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
556 // If it's game over, show the game over graphic in the dead centre
559 if(fadetimer<3.0/faderate) {
560 fadegame = fadetimer/(3.0/faderate);
565 if(fadetimer<3.0/faderate) {
567 } else if(fadetimer<6.0/faderate) {
568 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
573 dest.x = (XSIZE-surf_b_game->w)/2;
574 dest.y = (YSIZE-surf_b_game->h)/2-40;
575 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += gamerate/1.0))));
576 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
578 dest.x = (XSIZE-surf_b_over->w)/2;
579 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
580 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
581 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
586 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
587 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
588 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += gamerate/2.0)));
589 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
591 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
592 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
593 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
594 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
596 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
597 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
598 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
599 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
601 text = "Version " VERSION;
602 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
603 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
605 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
606 //text = "Press SPACE to start!";
607 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
608 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
611 case HIGH_SCORE_ENTRY:
613 if(!process_score_input()) { // done inputting name
615 // Change state to briefly show high scores page
616 state = HIGH_SCORE_DISPLAY;
619 // Write the high score table to the file
620 write_high_score_table();
622 // Play the title page tune
626 case HIGH_SCORE_DISPLAY:
627 // Display de list o high scores mon.
628 display_scores(surf_screen, 150,50);
632 ; // no action necessary
635 if(!gameover && state == GAMEPLAY) {
636 bang = hit_rocks(shipx, shipy, &shipshape);
639 ticks_since_last = SDL_GetTicks()-last_ticks;
640 last_ticks = SDL_GetTicks();
641 if(ticks_since_last>200 || ticks_since_last<0) {
645 gamerate = GAMESPEED*ticks_since_last/50.0;
646 if(state == GAMEPLAY) {
647 score += ticks_since_last;
651 // Update the surface
652 SDL_Flip(surf_screen);
666 // Count down the game loop timer, and change state when it gets to zero or less;
668 if((state_timeout -= gamerate*3) < 0) {
671 // Create a new ship and start all over again
676 state = HIGH_SCORE_ENTRY;
677 state_timeout = 5.0e6;
678 if(new_high_score(score)) {
680 SDL_EnableUNICODE(1);
681 while(SDL_PollEvent(&e))
684 state = HIGH_SCORE_DISPLAY;
688 case HIGH_SCORE_DISPLAY:
690 state_timeout = 500.0;
692 case HIGH_SCORE_ENTRY:
693 // state = TITLE_PAGE;
695 // state_timeout = 100.0;
698 state = HIGH_SCORE_DISPLAY;
699 state_timeout = 200.0;
702 ; // no action necessary
705 if(state == DEAD_PAUSE) {
709 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
710 blast_radius = BLAST_RADIUS * 1.3;
713 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
716 blast_rocks(bangx, bangy, blast_radius, fixonly);
718 if(bangx < 60) bangx = 60;
726 shipdx *= pow((double)0.9,(double)gamerate);
727 shipdy *= pow((double)0.9,(double)gamerate);
731 shipx += shipdx*gamerate;
732 shipy += shipdy*gamerate;
735 tmp = shipy - (YSIZE / 2);
738 tmp = ((screendy * (gamerate - 12)) + (tmp * gamerate)) / 12;
740 tmp = shipx - (XSIZE / 3);
743 tmp = ((screendx * (gamerate - 12)) + (tmp * gamerate)) / 12;
746 xscroll = screendx * gamerate;
747 yscroll = screendy * gamerate;
752 bangx += bangdx*gamerate - xscroll;
753 bangy += bangdy*gamerate - yscroll;
759 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
760 // BOUNCE from left and right wall
761 shipx -= shipdx*gamerate;
766 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
767 // BOUNCE from top and bottom wall
773 if(draw() && state == GAMEPLAY) {
775 play_sound(0); // Play the explosion sound
776 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
777 make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
778 shipdx *= 0.5; shipdy *= 0.5;
782 shipdx = 8; shipdy = 0;
783 state_timeout = 200.0;
789 state_timeout = DEAD_PAUSE_LENGTH;
794 keystate = SDL_GetKeyState(NULL);
797 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
811 shipx = XSIZE/2.2; shipy = YSIZE/2;
812 shipdx = screendx; shipdy = screendy;
818 keystate = SDL_GetKeyState(NULL);
821 if(state == GAMEPLAY) {
825 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*gamerate; maneuver |= 1<<3;}
826 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*gamerate; maneuver |= 1<<1;}
827 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*gamerate; maneuver |= 1<<2;}
828 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*gamerate; maneuver |= 1;}
829 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
832 if(keystate[SDLK_p] | keystate[SDLK_s]) {
846 } else if(state == GAME_OVER) {
847 if(keystate[SDLK_SPACE]) {
852 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
860 main(int argc, char **argv) {
869 while ((x = getopt(argc,argv,"efhmps")) >= 0) {
874 case 'f': // fullscreen
878 printf("Variations on RockDodger\n"
879 " -e big tail [E]ngine\n"
880 " -f [F]ull screen\n"
881 " -h this [H]elp message\n"
882 " -m enable [M]usic\n"
883 " -p original [P]hysics (friction)\n"
884 " -s [S]ilent (no sound)\n");
899 if(init(fullscreen)) {
900 printf ("ta: '%s'\n",initerror);