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
61 // Structure global variables
62 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
63 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
64 struct spacedot sdot[MAXSPACEDOTS];
66 // Other global variables
70 struct shape shipshape;
71 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
72 float shipdx = 8, shipdy = 0; // Change in X position per tick.
73 float screendx = 7.5, screendy = 0.0;
74 float xscroll, yscroll;
75 float gamerate; // this controls the speed of everything that moves.
77 float bangx, bangy, bangdx, bangdy;
79 int nships,score,ticks_since_last, last_ticks;
82 int sound_flag, music_flag;
83 int tail_plume; // display big engine at the back?
84 int friction; // should there be friction?
85 float fadetimer = 0, faderate;
87 int pausedown = 0, paused = 0;
89 // bangdot start (bd1) and end (bd2) position:
100 enum states state = TITLE_PAGE;
101 float state_timeout = 600.0;
105 "Press SPACE to start",
106 "http://qualdan.com/vor/"
109 int bangdotlife, nbangdots;
110 Uint16 heatcolor[W*3];
114 extern int optind, opterr, optopt;
116 // ************************************* FUNCS
120 return (float)random()/(float)RAND_MAX;
126 for(i = 0; i<MAXENGINEDOTS; i++) {
134 for(i = 0; i<MAXSPACEDOTS; i++) {
135 sdot[i].x = rnd()*(XSIZE-5);
136 sdot[i].y = rnd()*(YSIZE-5);
137 sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
138 b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
139 sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
144 make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
146 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
147 // TODO - generate and display dots in a circular buffer
154 begin_generate = SDL_GetTicks();
157 rawpixel = (Uint16 *) s->pixels;
159 //for(n = 0; n <= power/2; n++) {
163 for(x = 0; x<s->w; x++) {
164 for(y = 0; y<s->h; y++) {
165 c = rawpixel[s->pitch/2*y + x];
166 if(c && c != s->format->colorkey) {
168 theta = rnd()*M_PI*2;
172 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
173 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
174 bdot[bd2].x = x + xbang;
175 bdot[bd2].y = y + ybang;
177 // Replace the last few bang dots with the pixels from the exploding object
178 bdot[bd2].c = (endcount>0)?c:0;
179 bdot[bd2].life = 100;
180 bdot[bd2].decay = rnd()*3 + 1;
181 bdot[bd2].active = 1;
186 // If the circular buffer is filled, who cares? They've had their chance.
187 //if(bd2 == bd1-1) goto exitloop;
193 if(SDL_GetTicks() - begin_generate > 7) endcount++;
196 SDL_UnlockSurface(s);
201 draw_bang_dots(SDL_Surface *s) {
205 rawpixel = (Uint16 *) s->pixels;
209 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
213 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
214 // If the dot has drifted outside the perimeter, kill it
222 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)];
223 bdot[i].life -= bdot[i].decay;
224 bdot[i].x += bdot[i].dx*gamerate - xscroll;
225 bdot[i].y += bdot[i].dy*gamerate - yscroll;
245 draw_space_dots(SDL_Surface *s) {
248 rawpixel = (Uint16 *) s->pixels;
250 for(i = 0; i<MAXSPACEDOTS; i++) {
254 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
255 sdot[i].x -= xscroll / (1.3 + sdot[i].z);
256 sdot[i].y -= yscroll / (1.3 + sdot[i].z);
257 if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
258 else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
259 if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
260 else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
265 draw_engine_dots(SDL_Surface *s) {
268 rawpixel = (Uint16 *) s->pixels;
270 for(i = 0; i<MAXENGINEDOTS; i++) {
272 edot[i].x += edot[i].dx*gamerate - xscroll;
273 edot[i].y += edot[i].dy*gamerate - yscroll;
274 if((edot[i].life -= gamerate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
276 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
280 heatindex = edot[i].life * 6;
281 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
282 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
289 create_engine_dots(int newdots) {
291 double theta,r,dx,dy;
293 if(!tail_plume) return;
295 if(state == GAMEPLAY) {
296 for(i = 0; i<newdots*gamerate; i++) {
297 if(dotptr->active == 0) {
298 theta = rnd()*M_PI*2;
304 dotptr->x = shipx + surf_ship->w/2-14;
305 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*5-1;
306 dotptr->dx = 10*(dx-1.5) + shipdx;
307 dotptr->dy = 1*dy + shipdy;
308 dotptr->life = 45 + rnd(1)*5;
311 if(dotptr-edot >= MAXENGINEDOTS) {
320 create_engine_dots2(int newdots, int m) {
322 double theta, theta2, dx, dy, adx, ady;
324 // Don't create fresh engine dots when
325 // the game is not being played and a demo is not beng shown
326 if(state != GAMEPLAY) return;
328 for(i = 0; i<newdots; i++) {
329 if(dotptr->active == 0) {
330 theta = rnd()*M_PI*2;
331 theta2 = rnd()*M_PI*2;
333 dx = cos(theta) * fabs(cos(theta2));
334 dy = sin(theta) * fabs(cos(theta2));
340 dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
341 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
346 dotptr->dx = -20*adx + shipdx;
347 dotptr->dy = 2*dy + shipdy;
348 dotptr->life = 60 * adx;
351 dotptr->dx = 2*dx + shipdx;
352 dotptr->dy = -20*ady + shipdy;
353 dotptr->life = 60 * ady;
357 dotptr->dx = 20*adx + shipdx;
358 dotptr->dy = 2*dy + shipdy;
359 dotptr->life = 60 * adx;
362 dotptr->dx = 2*dx + shipdx;
363 dotptr->dy = 20*ady + shipdy;
364 dotptr->life = 60 * ady;
368 if(dotptr-edot >= MAXENGINEDOTS) {
376 drawdots(SDL_Surface *s) {
379 // Create more engine dots comin' out da back
380 if(!gameover) create_engine_dots(200);
382 // Create engine dots out the side we're moving from
383 for(m = 0; m<4; m++) {
384 if(maneuver & 1<<m) { // 'maneuver' is a bit field
385 create_engine_dots2(80,m);
393 SDL_UnlockSurface(s);
397 init(int fullscreen) {
403 // Where are our data files?
404 if(!find_files()) exit(1);
405 read_high_score_table();
408 // Initialize SDL with audio and video
409 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
411 printf ("Can't open sound, starting without it\n");
415 atexit(SDL_CloseAudio);
416 sound_flag = init_sound();
419 // Initialize with video only
420 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
426 // Attempt to get the required video size
427 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
428 if(fullscreen) flag |= SDL_FULLSCREEN;
429 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
431 // Set the title bar text
432 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
434 NULLERROR(surf_screen);
436 // Set the heat color from the range 0 (cold) to 300 (blue-white)
437 for(i = 0; i<W*3; i++) {
438 heatcolor[i] = SDL_MapRGB(
440 (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?
445 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
446 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
448 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
449 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
451 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
452 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
454 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
455 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
457 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
458 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
460 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
461 g_font = SFont_InitFont(surf_font_big);
463 // Load the spaceship graphic.
464 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
465 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
466 get_shape(surf_ship, &shipshape);
468 // Load the life indicator (small ship) graphic.
469 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
470 NULLERROR(surf_life = SDL_DisplayFormat(temp));
477 // Remove the mouse cursor
479 SDL_ShowCursor(SDL_DISABLE);
491 float fadegame,fadeover;
500 // Draw a fully black background
501 SDL_FillRect(surf_screen,NULL,0);
503 // Draw the background dots
504 drawdots(surf_screen);
507 if(!gameover && state == GAMEPLAY ) {
508 src.w = surf_ship->w;
509 src.h = surf_ship->h;
514 SDL_BlitSurface(surf_ship,&src,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 src.w = surf_life->w;
523 src.h = surf_life->h;
526 dest.x = (i + 1)*(src.w + 10);
528 SDL_BlitSurface(surf_life, &src, surf_screen, &dest);
532 snprintscore_line(topline, 50, score);
533 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
535 // If it's game over, show the game over graphic in the dead centre
538 if(fadetimer<3.0/faderate) {
539 fadegame = fadetimer/(3.0/faderate);
544 if(fadetimer<3.0/faderate) {
546 } else if(fadetimer<6.0/faderate) {
547 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
552 src.w = surf_b_game->w;
553 src.h = surf_b_game->h;
556 dest.x = (XSIZE-src.w)/2;
557 dest.y = (YSIZE-src.h)/2-40;
558 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += gamerate/1.0))));
559 SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
561 src.w = surf_b_over->w;
562 src.h = surf_b_over->h;
565 dest.x = (XSIZE-src.w)/2;
566 dest.y = (YSIZE-src.h)/2 + 40;
567 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
568 SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
573 src.w = surf_b_variations->w;
574 src.h = surf_b_variations->h;
577 dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
578 dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
579 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += gamerate/2.0)));
580 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
582 src.w = surf_b_on->w;
583 src.h = surf_b_on->h;
586 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
587 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
588 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
589 SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
591 src.w = surf_b_rockdodger->w;
592 src.h = surf_b_rockdodger->h;
595 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
596 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
597 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
598 SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
600 text = "Version " VERSION;
601 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
602 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
604 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
605 //text = "Press SPACE to start!";
606 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
607 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
610 case HIGH_SCORE_ENTRY:
612 if(!process_score_input()) { // done inputting name
614 // Change state to briefly show high scores page
615 state = HIGH_SCORE_DISPLAY;
618 // Write the high score table to the file
619 write_high_score_table();
621 // Play the title page tune
625 case HIGH_SCORE_DISPLAY:
626 // Display de list o high scores mon.
627 display_scores(surf_screen, 150,50);
631 ; // no action necessary
634 if(!gameover && state == GAMEPLAY) {
635 bang = hit_rocks(shipx, shipy, &shipshape);
638 ticks_since_last = SDL_GetTicks()-last_ticks;
639 last_ticks = SDL_GetTicks();
640 if(ticks_since_last>200 || ticks_since_last<0) {
644 gamerate = GAMESPEED*ticks_since_last/50.0;
645 if(state == GAMEPLAY) {
646 score += ticks_since_last;
650 // Update the surface
651 SDL_Flip(surf_screen);
665 // Count down the game loop timer, and change state when it gets to zero or less;
667 if((state_timeout -= gamerate*3) < 0) {
670 // Create a new ship and start all over again
675 state = HIGH_SCORE_ENTRY;
676 state_timeout = 5.0e6;
677 if(new_high_score(score)) {
679 SDL_EnableUNICODE(1);
680 while(SDL_PollEvent(&e))
683 state = HIGH_SCORE_DISPLAY;
687 case HIGH_SCORE_DISPLAY:
689 state_timeout = 500.0;
691 case HIGH_SCORE_ENTRY:
692 // state = TITLE_PAGE;
694 // state_timeout = 100.0;
697 state = HIGH_SCORE_DISPLAY;
698 state_timeout = 200.0;
701 ; // no action necessary
704 if(state == DEAD_PAUSE) {
708 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
709 blast_radius = BLAST_RADIUS * 1.3;
712 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
715 blast_rocks(bangx, bangy, blast_radius, fixonly);
717 if(bangx < 60) bangx = 60;
725 shipdx *= pow((double)0.9,(double)gamerate);
726 shipdy *= pow((double)0.9,(double)gamerate);
730 shipx += shipdx*gamerate;
731 shipy += shipdy*gamerate;
734 tmp = shipy - (YSIZE / 2);
737 tmp = ((screendy * (gamerate - 12)) + (tmp * gamerate)) / 12;
739 tmp = shipx - (XSIZE / 3);
742 tmp = ((screendx * (gamerate - 12)) + (tmp * gamerate)) / 12;
745 xscroll = screendx * gamerate;
746 yscroll = screendy * gamerate;
751 bangx += bangdx*gamerate - xscroll;
752 bangy += bangdy*gamerate - yscroll;
758 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
759 // BOUNCE from left and right wall
760 shipx -= shipdx*gamerate;
765 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
766 // BOUNCE from top and bottom wall
772 if(draw() && state == GAMEPLAY) {
774 play_sound(0); // Play the explosion sound
775 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
776 make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
777 shipdx *= 0.5; shipdy *= 0.5;
781 shipdx = 8; shipdy = 0;
782 state_timeout = 200.0;
788 state_timeout = DEAD_PAUSE_LENGTH;
793 keystate = SDL_GetKeyState(NULL);
795 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
806 shipx = XSIZE/2.2; shipy = YSIZE/2;
807 shipdx = screendx; shipdy = screendy;
813 keystate = SDL_GetKeyState(NULL);
816 if(state == GAMEPLAY) {
820 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*gamerate; maneuver |= 1<<3;}
821 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*gamerate; maneuver |= 1<<1;}
822 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*gamerate; maneuver |= 1<<2;}
823 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*gamerate; maneuver |= 1;}
824 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
827 if(keystate[SDLK_p] | keystate[SDLK_s]) {
841 } else if(state == GAME_OVER) {
842 if(keystate[SDLK_SPACE]) {
847 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
855 main(int argc, char **argv) {
864 while ((x = getopt(argc,argv,"efhmps")) >= 0) {
869 case 'f': // fullscreen
873 printf("Variations on RockDodger\n"
874 " -e big tail [E]ngine\n"
875 " -f [F]ull screen\n"
876 " -h this [H]elp message\n"
877 " -m enable [M]usic\n"
878 " -p original [P]hysics (friction)\n"
879 " -s [S]ilent (no sound)\n");
894 if(init(fullscreen)) {
895 printf ("ta: '%s'\n",initerror);