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
25 #include <SDL/SDL_image.h>
49 // ************************************* VARS
50 // SDL_Surface global variables
52 *surf_screen, // Screen
53 *surf_b_variations, // "variations" banner
54 *surf_b_on, // "on" banner
55 *surf_b_rockdodger, // "rockdodger" banner
56 *surf_b_game, // Title element "game"
57 *surf_b_over, // Title element "over"
58 *surf_ship, // Spaceship element
59 *surf_life, // Indicator of number of ships remaining
60 *surf_rock[NROCKS], // THE ROCKS
61 *surf_font_big; // The big font
66 // Structure global variables
67 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
68 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
70 // Other global variables
76 struct shape shipshape;
77 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
78 float shipdx = SCREENDXMIN, shipdy = 0.0; // Change in X position per tick.
79 float screendx = SCREENDXMIN, screendy = 0.0;
80 float xscroll, yscroll;
83 // all movement is based on t_frame.
84 float t_frame; // length of this frame (in ticks = 1/20th second)
85 float s_frame; // length of this frame (seconds)
86 int ms_frame; // length of this frame (milliseconds)
87 int ms_end; // end of this frame (milliseconds)
89 float bangx, bangy, bangdx, bangdy;
95 float fadetimer = 0, faderate;
97 int pausedown = 0, paused = 0;
99 // bangdot start (bd1) and end (bd2) position:
100 int bd1 = 0, bd2 = 0;
110 enum states state = TITLE_PAGE;
111 float state_timeout = 600.0;
115 "Press SPACE to start",
116 "http://herkamire.com/jason/vor"
119 int bangdotlife, nbangdots;
120 Uint16 heatcolor[W*3];
124 extern int optind, opterr, optopt;
126 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
128 // ************************************* FUNCS
133 for(i = 0; i<MAXENGINEDOTS; i++) {
139 new_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power)
146 begin_generate = SDL_GetTicks();
149 rawpixel = (Uint16 *) s->pixels;
151 //for(n = 0; n <= power/2; n++) {
155 for(x = 0; x<s->w; x++) {
156 for(y = 0; y<s->h; y++) {
157 c = rawpixel[s->pitch/2*y + x];
158 if(c && c != s->format->colorkey) {
160 theta = frnd()*M_PI*2;
162 r = 1-(frnd()*frnd());
164 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
165 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
166 bdot[bd2].x = x + xbang;
167 bdot[bd2].y = y + ybang;
169 // Replace the last few bang dots with the pixels from the exploding object
170 bdot[bd2].c = (endcount>0)?c:0;
171 bdot[bd2].life = 100;
172 bdot[bd2].decay = frnd()*3 + 1;
173 bdot[bd2].active = 1;
178 // If the circular buffer is filled, who cares? They've had their chance.
179 //if(bd2 == bd1-1) goto exitloop;
185 if(SDL_GetTicks() - begin_generate > 7) endcount++;
188 SDL_UnlockSurface(s);
193 draw_bang_dots(SDL_Surface *s) {
197 rawpixel = (Uint16 *) s->pixels;
202 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
206 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
207 // If the dot has drifted outside the perimeter, kill it
215 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)];
216 bdot[i].life -= bdot[i].decay;
217 bdot[i].x += bdot[i].dx*t_frame - xscroll;
218 bdot[i].y += bdot[i].dy*t_frame - yscroll;
238 draw_engine_dots(SDL_Surface *s) {
241 rawpixel = (Uint16 *) s->pixels;
243 for(i = 0; i<MAXENGINEDOTS; i++) {
245 edot[i].x += edot[i].dx*t_frame - xscroll;
246 edot[i].y += edot[i].dy*t_frame - yscroll;
247 if((edot[i].life -= t_frame*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
249 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
253 heatindex = edot[i].life * 6;
254 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
255 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
262 new_engine_dots(int n, int dir) {
264 float a, r; // angle, random length
266 float hx, hy; // half ship width/height.
267 static const int s[4] = { 2, 1, 0, 1 };
269 hx = surf_ship->w / 2;
270 hy = surf_ship->h / 2;
272 for(i = 0; i<n; i++) {
273 if(dotptr->active == 0) {
274 a = frnd()*M_PI + (dir-1)*M_PI_2;
275 r = sin(frnd()*M_PI);
277 dy = r * -sin(a); // screen y is "backwards".
280 dotptr->x = shipx + s[dir]*hx + (frnd()-0.5)*3;
281 dotptr->y = shipy + s[(dir+1)&3]*hy + (frnd()-0.5)*3;
283 dotptr->dx = shipdx + 2*dx;
284 dotptr->dy = shipdy + 20*dy;
285 dotptr->life = 60 * fabs(dy);
287 dotptr->dx = shipdx + 20*dx;
288 dotptr->dy = shipdy + 2*dy;
289 dotptr->life = 60 * fabs(dx);
293 if(dotptr-edot >= MAXENGINEDOTS) {
301 drawdots(SDL_Surface *s) {
304 // Create engine dots out the side we're moving from
305 for(m = 0; m<4; m++) {
306 if(jets & 1<<m) { // 'jets' is a bit field
307 new_engine_dots(80,m);
317 SDL_UnlockSurface(s);
327 // Where are our data files?
328 if(!find_files()) exit(1);
329 read_high_score_table();
332 // Initialize SDL with audio and video
333 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
335 printf ("Can't open sound, starting without it\n");
339 atexit(SDL_CloseAudio);
340 opt_sound = init_sound();
343 // Initialize with video only
344 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
348 play_tune(TUNE_TITLE_PAGE);
350 // Attempt to get the required video size
351 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
352 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
353 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
355 // Set the title bar text
356 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
358 NULLERROR(surf_screen);
360 // Set the heat color from the range 0 (cold) to 300 (blue-white)
361 for(i = 0; i<W*3; i++) {
362 heatcolor[i] = SDL_MapRGB(
364 (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?
369 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
370 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
372 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
373 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
375 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
376 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
378 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
379 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
381 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
382 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
384 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
385 g_font = SFont_InitFont(surf_font_big);
387 // Load the spaceship graphic.
388 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
389 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
390 get_shape(surf_ship, &shipshape);
392 // Load the life indicator (small ship) graphic.
393 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
394 NULLERROR(surf_life = SDL_DisplayFormat(temp));
401 // Remove the mouse cursor
403 SDL_ShowCursor(SDL_DISABLE);
415 float fadegame,fadeover;
419 // Draw a fully black background
420 SDL_FillRect(surf_screen,NULL,0);
422 // Draw the background dots
423 drawdots(surf_screen);
426 if(!gameover && state == GAMEPLAY ) {
429 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
434 // Draw the life indicators.
435 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
436 for(i = 0; i<nships-1; i++) {
437 dest.x = (i + 1)*(surf_life->w + 10);
439 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
443 snprintscore_line(topline, 50, score);
444 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
446 // If it's game over, show the game over graphic in the dead centre
449 if(fadetimer<3.0/faderate) {
450 fadegame = fadetimer/(3.0/faderate);
455 if(fadetimer<3.0/faderate) {
457 } else if(fadetimer<6.0/faderate) {
458 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
463 dest.x = (XSIZE-surf_b_game->w)/2;
464 dest.y = (YSIZE-surf_b_game->h)/2-40;
465 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += t_frame/1.0))));
466 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
468 dest.x = (XSIZE-surf_b_over->w)/2;
469 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
470 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
471 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
476 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
477 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
478 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += t_frame/2.0)));
479 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
481 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
482 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
483 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
484 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
486 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
487 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
488 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
489 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
491 text = "Version " VERSION;
492 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
493 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
495 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
496 //text = "Press SPACE to start!";
497 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
498 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
501 case HIGH_SCORE_ENTRY:
502 play_tune(TUNE_HIGH_SCORE_ENTRY);
503 if(!process_score_input()) { // done inputting name
505 // Change state to briefly show high scores page
506 state = HIGH_SCORE_DISPLAY;
509 // Write the high score table to the file
510 write_high_score_table();
512 play_tune(TUNE_TITLE_PAGE);
515 case HIGH_SCORE_DISPLAY:
516 // Display de list o high scores mon.
517 display_scores(surf_screen, 150,50);
521 ; // no action necessary
524 if(!gameover && state == GAMEPLAY) {
525 bang = hit_rocks(shipx, shipy, &shipshape);
528 ms_frame = SDL_GetTicks() - ms_end;
530 if(ms_frame>200 || ms_frame<0) {
531 // We won't run at all below 5 frames per second.
532 // This also happens if we were paused, grr.
536 s_frame = opt_gamespeed * ms_frame / 1000;
537 if(state == GAMEPLAY) score += ms_frame;
539 t_frame = s_frame * 20;
541 // Update the surface
542 SDL_Flip(surf_screen);
556 // Count down the game loop timer, and change state when it gets to zero or less;
558 if((state_timeout -= t_frame*3) < 0) {
561 // Create a new ship and start all over again
563 play_tune(TUNE_GAMEPLAY);
566 if(new_high_score(score)) {
568 state = HIGH_SCORE_ENTRY;
569 state_timeout = 5.0e6;
570 SDL_EnableUNICODE(1);
571 while(SDL_PollEvent(&e))
573 } else if(!keystate[SDLK_SPACE]) {
574 state = HIGH_SCORE_DISPLAY;
578 case HIGH_SCORE_DISPLAY:
580 state_timeout = 500.0;
582 case HIGH_SCORE_ENTRY:
585 state = HIGH_SCORE_DISPLAY;
586 state_timeout = 200.0;
589 ; // no action necessary
592 if(state == DEAD_PAUSE) {
596 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
597 blast_radius = BLAST_RADIUS * 1.3;
600 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
603 blast_rocks(bangx, bangy, blast_radius, fixonly);
605 if(bangx < 60) bangx = 60;
612 shipx += shipdx*t_frame;
613 shipy += shipdy*t_frame;
616 tmp = (shipy-YSCROLLTO)/25 + (shipdy-screendy);
617 screendy += tmp * t_frame/12;
618 tmp = (shipx-XSCROLLTO)/25 + (shipdx-screendx);
619 screendx += tmp * t_frame/12;
621 // taper off if we would hit the barrier in under 2 seconds.
622 if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0) {
623 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
626 xscroll = screendx * t_frame;
627 yscroll = screendy * t_frame;
628 back_dist += (screendx - SCREENDXMIN)*t_frame;
629 if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
635 bangx += bangdx*t_frame - xscroll;
636 bangy += bangdy*t_frame - yscroll;
642 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
643 // BOUNCE from left and right wall
644 shipx -= (shipdx-screendx)*t_frame;
645 shipdx = screendx - (shipdx-screendx)*opt_bounciness;
649 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
650 // BOUNCE from top and bottom wall
651 shipy -= (shipdy-screendy)*t_frame;
652 shipdy = screendy - (shipdy-screendy)*opt_bounciness;
656 if(draw() && state == GAMEPLAY) {
658 play_sound(SOUND_BANG); // Play the explosion sound
659 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
660 new_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
661 shipdx *= 0.5; shipdy *= 0.5;
662 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
666 shipdx = 8; shipdy = 0;
667 state_timeout = 200.0;
673 state_timeout = DEAD_PAUSE_LENGTH;
678 keystate = SDL_GetKeyState(NULL);
681 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
689 play_tune(TUNE_GAMEPLAY);
692 shipx = XSIZE/2.2; shipy = YSIZE/2;
693 shipdx = screendx; shipdy = screendy;
699 keystate = SDL_GetKeyState(NULL);
702 if(state == GAMEPLAY) {
706 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*t_frame; jets |= 1<<0;}
707 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*t_frame; jets |= 1<<1;}
708 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*t_frame; jets |= 1<<2;}
709 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*t_frame; jets |= 1<<3;}
710 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
713 if(keystate[SDLK_p] | keystate[SDLK_s]) {
727 } else if(state == GAME_OVER) {
728 if(keystate[SDLK_SPACE]) {
733 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
741 main(int argc, char **argv) {
743 argp_parse(&argp, argc, argv, 0, 0, 0);
746 printf ("ta: '%s'\n",initerror);