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
38 #include <SDL/SDL_image.h>
46 // ************************************* VARS
47 // SDL_Surface global variables
49 *surf_screen, // Screen
50 *surf_b_variations, // "variations" banner
51 *surf_b_on, // "on" banner
52 *surf_b_rockdodger, // "rockdodger" banner
53 *surf_b_game, // Title element "game"
54 *surf_b_over, // Title element "over"
55 *surf_ship, // Spaceship element
56 *surf_life, // Indicator of number of ships remaining
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
74 struct shape shipshape;
75 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
76 float shipdx = SCREENDXMIN, shipdy = 0.0; // Change in X position per tick.
77 float screendx = SCREENDXMIN, screendy = 0.0;
78 float xscroll, yscroll;
79 float framelen; // this controls the speed of everything that moves.
81 float bangx, bangy, bangdx, bangdy;
83 int nships,score,ticks_since_last,last_ticks;
87 float fadetimer = 0, faderate;
89 int pausedown = 0, paused = 0;
91 // bangdot start (bd1) and end (bd2) position:
102 enum states state = TITLE_PAGE;
103 float state_timeout = 600.0;
107 "Press SPACE to start",
108 "http://herkamire.com/jason/vor"
111 int bangdotlife, nbangdots;
112 Uint16 heatcolor[W*3];
116 extern int optind, opterr, optopt;
118 // ************************************* FUNCS
122 return (float)random()/(float)RAND_MAX;
128 for(i = 0; i<MAXENGINEDOTS; i++) {
136 for(i = 0; i<MAXSPACEDOTS; i++) {
137 sdot[i].x = rnd()*(XSIZE-5);
138 sdot[i].y = rnd()*(YSIZE-5);
139 sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
140 b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
141 sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
146 make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
148 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
149 // TODO - generate and display dots in a circular buffer
156 begin_generate = SDL_GetTicks();
159 rawpixel = (Uint16 *) s->pixels;
161 //for(n = 0; n <= power/2; n++) {
165 for(x = 0; x<s->w; x++) {
166 for(y = 0; y<s->h; y++) {
167 c = rawpixel[s->pitch/2*y + x];
168 if(c && c != s->format->colorkey) {
170 theta = rnd()*M_PI*2;
174 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
175 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
176 bdot[bd2].x = x + xbang;
177 bdot[bd2].y = y + ybang;
179 // Replace the last few bang dots with the pixels from the exploding object
180 bdot[bd2].c = (endcount>0)?c:0;
181 bdot[bd2].life = 100;
182 bdot[bd2].decay = rnd()*3 + 1;
183 bdot[bd2].active = 1;
188 // If the circular buffer is filled, who cares? They've had their chance.
189 //if(bd2 == bd1-1) goto exitloop;
195 if(SDL_GetTicks() - begin_generate > 7) endcount++;
198 SDL_UnlockSurface(s);
203 draw_bang_dots(SDL_Surface *s) {
207 rawpixel = (Uint16 *) s->pixels;
212 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
216 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
217 // If the dot has drifted outside the perimeter, kill it
225 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)];
226 bdot[i].life -= bdot[i].decay;
227 bdot[i].x += bdot[i].dx*framelen - xscroll;
228 bdot[i].y += bdot[i].dy*framelen - yscroll;
248 draw_space_dots(SDL_Surface *s) {
251 rawpixel = (Uint16 *) s->pixels;
253 for(i = 0; i<MAXSPACEDOTS; i++) {
257 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
258 sdot[i].x -= xscroll / (1.3 + sdot[i].z);
259 sdot[i].y -= yscroll / (1.3 + sdot[i].z);
260 if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
261 else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
262 if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
263 else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
268 draw_engine_dots(SDL_Surface *s) {
271 rawpixel = (Uint16 *) s->pixels;
273 for(i = 0; i<MAXENGINEDOTS; i++) {
275 edot[i].x += edot[i].dx*framelen - xscroll;
276 edot[i].y += edot[i].dy*framelen - yscroll;
277 if((edot[i].life -= framelen*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
279 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
283 heatindex = edot[i].life * 6;
284 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
285 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
292 create_engine_dots(int newdots) {
294 double theta,r,dx,dy;
296 if(!opt_tail_engine) return;
298 if(state == GAMEPLAY) {
299 for(i = 0; i<newdots*framelen; i++) {
300 if(dotptr->active == 0) {
301 theta = rnd()*M_PI*2;
307 dotptr->x = shipx + surf_ship->w/2-14;
308 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*5-1;
309 dotptr->dx = 10*(dx-1.5) + shipdx;
310 dotptr->dy = 1*dy + shipdy;
311 dotptr->life = 45 + rnd(1)*5;
314 if(dotptr-edot >= MAXENGINEDOTS) {
323 create_engine_dots2(int newdots, int m) {
325 double theta, theta2, dx, dy, adx, ady;
327 // Don't create fresh engine dots when
328 // the game is not being played and a demo is not beng shown
329 if(state != GAMEPLAY) return;
331 for(i = 0; i<newdots; i++) {
332 if(dotptr->active == 0) {
333 theta = rnd()*M_PI*2;
334 theta2 = rnd()*M_PI*2;
336 dx = cos(theta) * fabs(cos(theta2));
337 dy = sin(theta) * fabs(cos(theta2));
343 dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
344 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
349 dotptr->dx = -20*adx + shipdx;
350 dotptr->dy = 2*dy + shipdy;
351 dotptr->life = 60 * adx;
354 dotptr->dx = 2*dx + shipdx;
355 dotptr->dy = -20*ady + shipdy;
356 dotptr->life = 60 * ady;
360 dotptr->dx = 20*adx + shipdx;
361 dotptr->dy = 2*dy + shipdy;
362 dotptr->life = 60 * adx;
365 dotptr->dx = 2*dx + shipdx;
366 dotptr->dy = 20*ady + shipdy;
367 dotptr->life = 60 * ady;
371 if(dotptr-edot >= MAXENGINEDOTS) {
379 drawdots(SDL_Surface *s) {
382 // Create more engine dots comin' out da back
383 if(!gameover) create_engine_dots(200);
385 // Create engine dots out the side we're moving from
386 for(m = 0; m<4; m++) {
387 if(maneuver & 1<<m) { // 'maneuver' is a bit field
388 create_engine_dots2(80,m);
396 SDL_UnlockSurface(s);
406 // Where are our data files?
407 if(!find_files()) exit(1);
408 read_high_score_table();
411 // Initialize SDL with audio and video
412 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
414 printf ("Can't open sound, starting without it\n");
418 atexit(SDL_CloseAudio);
419 opt_sound = init_sound();
422 // Initialize with video only
423 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
429 // Attempt to get the required video size
430 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
431 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
432 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
434 // Set the title bar text
435 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
437 NULLERROR(surf_screen);
439 // Set the heat color from the range 0 (cold) to 300 (blue-white)
440 for(i = 0; i<W*3; i++) {
441 heatcolor[i] = SDL_MapRGB(
443 (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?
448 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
449 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
451 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
452 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
454 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
455 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
457 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
458 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
460 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
461 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
463 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
464 g_font = SFont_InitFont(surf_font_big);
466 // Load the spaceship graphic.
467 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
468 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
469 get_shape(surf_ship, &shipshape);
471 // Load the life indicator (small ship) graphic.
472 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
473 NULLERROR(surf_life = SDL_DisplayFormat(temp));
480 // Remove the mouse cursor
482 SDL_ShowCursor(SDL_DISABLE);
494 float fadegame,fadeover;
498 // Draw a fully black background
499 SDL_FillRect(surf_screen,NULL,0);
501 // Draw the background dots
502 drawdots(surf_screen);
505 if(!gameover && state == GAMEPLAY ) {
508 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
513 // Draw the life indicators.
514 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
515 for(i = 0; i<nships-1; i++) {
516 dest.x = (i + 1)*(surf_life->w + 10);
518 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
522 snprintscore_line(topline, 50, score);
523 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
525 // If it's game over, show the game over graphic in the dead centre
528 if(fadetimer<3.0/faderate) {
529 fadegame = fadetimer/(3.0/faderate);
534 if(fadetimer<3.0/faderate) {
536 } else if(fadetimer<6.0/faderate) {
537 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
542 dest.x = (XSIZE-surf_b_game->w)/2;
543 dest.y = (YSIZE-surf_b_game->h)/2-40;
544 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += framelen/1.0))));
545 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
547 dest.x = (XSIZE-surf_b_over->w)/2;
548 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
549 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
550 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
555 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
556 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
557 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += framelen/2.0)));
558 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
560 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
561 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
562 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
563 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
565 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
566 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
567 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
568 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
570 text = "Version " VERSION;
571 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
572 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
574 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
575 //text = "Press SPACE to start!";
576 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
577 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
580 case HIGH_SCORE_ENTRY:
582 if(!process_score_input()) { // done inputting name
584 // Change state to briefly show high scores page
585 state = HIGH_SCORE_DISPLAY;
588 // Write the high score table to the file
589 write_high_score_table();
591 // Play the title page tune
595 case HIGH_SCORE_DISPLAY:
596 // Display de list o high scores mon.
597 display_scores(surf_screen, 150,50);
601 ; // no action necessary
604 if(!gameover && state == GAMEPLAY) {
605 bang = hit_rocks(shipx, shipy, &shipshape);
608 ticks_since_last = SDL_GetTicks()-last_ticks;
609 last_ticks = SDL_GetTicks();
610 if(ticks_since_last>200 || ticks_since_last<0) {
611 // We won't run at all below 5 frames per second.
614 framelen = opt_gamespeed*ticks_since_last/50.0;
615 if(state == GAMEPLAY) {
616 score += ticks_since_last;
620 // Update the surface
621 SDL_Flip(surf_screen);
635 // Count down the game loop timer, and change state when it gets to zero or less;
637 if((state_timeout -= framelen*3) < 0) {
640 // Create a new ship and start all over again
645 if(new_high_score(score)) {
647 state = HIGH_SCORE_ENTRY;
648 state_timeout = 5.0e6;
649 SDL_EnableUNICODE(1);
650 while(SDL_PollEvent(&e))
652 } else if(!keystate[SDLK_SPACE]) {
653 state = HIGH_SCORE_DISPLAY;
657 case HIGH_SCORE_DISPLAY:
659 state_timeout = 500.0;
661 case HIGH_SCORE_ENTRY:
662 // state = TITLE_PAGE;
664 // state_timeout = 100.0;
667 state = HIGH_SCORE_DISPLAY;
668 state_timeout = 200.0;
671 ; // no action necessary
674 if(state == DEAD_PAUSE) {
678 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
679 blast_radius = BLAST_RADIUS * 1.3;
682 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
685 blast_rocks(bangx, bangy, blast_radius, fixonly);
687 if(bangx < 60) bangx = 60;
695 shipdx *= pow((double)0.9,(double)framelen);
696 shipdy *= pow((double)0.9,(double)framelen);
700 shipx += shipdx*framelen;
701 shipy += shipdy*framelen;
704 tmp = shipy - (YSIZE / 2);
707 tmp = ((screendy * (framelen - 12)) + (tmp * framelen)) / 12;
709 tmp = shipx - (XSIZE / 3);
712 tmp = ((screendx * (framelen - 12)) + (tmp * framelen)) / 12;
714 if(screendx < SCREENDXMIN) screendx=SCREENDXMIN;
716 xscroll = screendx * framelen;
717 yscroll = screendy * framelen;
723 bangx += bangdx*framelen - xscroll;
724 bangy += bangdy*framelen - yscroll;
730 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
731 // BOUNCE from left and right wall
732 shipx -= (shipdx-screendx)*framelen;
733 shipdx = screendx - (shipdx-screendx)*opt_bounciness;
737 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
738 // BOUNCE from top and bottom wall
739 shipy -= (shipdy-screendy)*framelen;
740 shipdy = screendy - (shipdy-screendy)*opt_bounciness;
744 if(draw() && state == GAMEPLAY) {
746 play_sound(0); // Play the explosion sound
747 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
748 make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
749 shipdx *= 0.5; shipdy *= 0.5;
750 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
754 shipdx = 8; shipdy = 0;
755 state_timeout = 200.0;
761 state_timeout = DEAD_PAUSE_LENGTH;
766 keystate = SDL_GetKeyState(NULL);
769 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
780 shipx = XSIZE/2.2; shipy = YSIZE/2;
781 shipdx = screendx; shipdy = screendy;
787 keystate = SDL_GetKeyState(NULL);
790 if(state == GAMEPLAY) {
794 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*framelen; maneuver |= 1<<3;}
795 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*framelen; maneuver |= 1<<1;}
796 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*framelen; maneuver |= 1<<2;}
797 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*framelen; maneuver |= 1;}
798 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
801 if(keystate[SDLK_p] | keystate[SDLK_s]) {
815 } else if(state == GAME_OVER) {
816 if(keystate[SDLK_SPACE]) {
821 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
829 main(int argc, char **argv) {
831 argp_parse(&argp, argc, argv, 0, 0, 0);
834 printf ("ta: '%s'\n",initerror);