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>
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;
66 // Other global variables
70 struct shape shipshape;
71 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
72 float shipdx = SCREENDXMIN, shipdy = 0.0; // Change in X position per tick.
73 float screendx = SCREENDXMIN, screendy = 0.0;
74 float xscroll, yscroll;
77 // all movement is based on t_frame.
78 float t_frame; // length of this frame (in ticks = 1/20th second)
79 int ms_frame; // length of this frame (milliseconds)
80 int ms_end; // end of this frame (milliseconds)
82 float bangx, bangy, bangdx, bangdy;
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 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
120 // ************************************* FUNCS
125 for(i = 0; i<MAXENGINEDOTS; i++) {
131 new_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s)
140 begin_generate = SDL_GetTicks();
142 row_inc = s->pitch/sizeof(uint16_t) - s->w;
143 colorkey = s->format->colorkey;
150 for(y=0; y<s->h; y++) {
151 for(x = 0; x<s->w; x++) {
153 if(c && c != colorkey) {
154 theta = frnd()*M_PI*2;
155 r = frnd(); r = 1 - r*r;
157 bdot[bd2].dx = 45*r*cos(theta) + dx;
158 bdot[bd2].dy = 45*r*sin(theta) + dy;
159 bdot[bd2].x = x + xbang;
160 bdot[bd2].y = y + ybang;
162 bdot[bd2].life = 100;
163 bdot[bd2].decay = frnd()*3 + 1;
164 bdot[bd2].active = 1;
166 // Replace the last few bang dots with the pixels from the exploding object
167 if(endcount>0) bdot[bd2].c = c;
169 bd2 = (bd2+1) % MAXBANGDOTS;
174 if(SDL_GetTicks() - begin_generate > 7) endcount++;
177 SDL_UnlockSurface(s);
181 draw_bang_dots(SDL_Surface *s) {
185 rawpixel = (Uint16 *) s->pixels;
190 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
194 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
195 // If the dot has drifted outside the perimeter, kill it
202 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)];
203 bdot[i].life -= bdot[i].decay;
204 bdot[i].x += bdot[i].dx*t_frame - xscroll;
205 bdot[i].y += bdot[i].dy*t_frame - yscroll;
207 if(bdot[i].life<0) bdot[i].active = 0;
224 draw_engine_dots(SDL_Surface *s) {
227 rawpixel = (Uint16 *) s->pixels;
229 for(i = 0; i<MAXENGINEDOTS; i++) {
231 edot[i].x += edot[i].dx*t_frame - xscroll;
232 edot[i].y += edot[i].dy*t_frame - yscroll;
233 edot[i].life -= t_frame*3;
235 || edot[i].y<0 || edot[i].y >= YSIZE
236 || edot[i].x<0 || edot[i].x >= XSIZE) {
240 heatindex = edot[i].life * 6;
241 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
242 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
249 new_engine_dots(int n, int dir) {
251 float a, r; // angle, random length
253 float hx, hy; // half ship width/height.
254 static const int s[4] = { 2, 1, 0, 1 };
256 hx = surf_ship->w / 2;
257 hy = surf_ship->h / 2;
259 for(i = 0; i<n; i++) {
260 if(dotptr->active == 0) {
261 a = frnd()*M_PI + (dir-1)*M_PI_2;
262 r = sin(frnd()*M_PI);
264 dy = r * -sin(a); // screen y is "backwards".
267 dotptr->x = shipx + s[dir]*hx + (frnd()-0.5)*3;
268 dotptr->y = shipy + s[(dir+1)&3]*hy + (frnd()-0.5)*3;
270 dotptr->dx = shipdx + 2*dx;
271 dotptr->dy = shipdy + 20*dy;
272 dotptr->life = 60 * fabs(dy);
274 dotptr->dx = shipdx + 20*dx;
275 dotptr->dy = shipdy + 2*dy;
276 dotptr->life = 60 * fabs(dx);
279 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
286 drawdots(SDL_Surface *s) {
289 // Create engine dots out the side we're moving from
290 for(m = 0; m<4; m++) {
291 if(jets & 1<<m) { // 'jets' is a bit field
292 new_engine_dots(80,m);
302 SDL_UnlockSurface(s);
312 // Where are our data files?
313 if(!find_files()) exit(1);
314 read_high_score_table();
317 // Initialize SDL with audio and video
318 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
320 printf ("Can't open sound, starting without it\n");
324 atexit(SDL_CloseAudio);
325 opt_sound = init_sound();
328 // Initialize with video only
329 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
333 play_tune(TUNE_TITLE_PAGE);
335 // Attempt to get the required video size
336 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
337 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
338 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
340 // Set the title bar text
341 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
343 NULLERROR(surf_screen);
345 // Set the heat color from the range 0 (cold) to 300 (blue-white)
346 for(i = 0; i<W*3; i++) {
347 heatcolor[i] = SDL_MapRGB(
349 (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?
354 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
355 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
357 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
358 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
360 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
361 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
363 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
364 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
366 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
367 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
369 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
370 g_font = SFont_InitFont(surf_font_big);
372 // Load the spaceship graphic.
373 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
374 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
375 get_shape(surf_ship, &shipshape);
377 // Load the life indicator (small ship) graphic.
378 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
379 NULLERROR(surf_life = SDL_DisplayFormat(temp));
386 // Remove the mouse cursor
388 SDL_ShowCursor(SDL_DISABLE);
400 float fadegame,fadeover;
404 // Draw a fully black background
405 SDL_FillRect(surf_screen,NULL,0);
407 // Draw the background dots
408 drawdots(surf_screen);
411 if(state == GAMEPLAY ) {
414 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
419 // Draw the life indicators.
420 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER) {
421 for(i = 0; i<nships-1; i++) {
422 dest.x = (i + 1)*(surf_life->w + 10);
424 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
429 snprintscore_line(topline, 50, score);
430 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
432 // If it's game over, show the game over graphic in the dead centre
435 if(fadetimer<3.0/faderate) {
436 fadegame = fadetimer/(3.0/faderate);
441 if(fadetimer<3.0/faderate) {
443 } else if(fadetimer<6.0/faderate) {
444 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
449 dest.x = (XSIZE-surf_b_game->w)/2;
450 dest.y = (YSIZE-surf_b_game->h)/2-40;
451 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += t_frame/1.0))));
452 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
454 dest.x = (XSIZE-surf_b_over->w)/2;
455 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
456 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
457 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
462 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
463 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
464 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += t_frame/2.0)));
465 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
467 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
468 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
469 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
470 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
472 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
473 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
474 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
475 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
477 text = "Version " VERSION;
478 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
479 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
481 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
482 //text = "Press SPACE to start!";
483 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
484 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
487 case HIGH_SCORE_ENTRY:
488 play_tune(TUNE_HIGH_SCORE_ENTRY);
489 if(!process_score_input()) { // done inputting name
491 // Change state to briefly show high scores page
492 state = HIGH_SCORE_DISPLAY;
495 // Write the high score table to the file
496 write_high_score_table();
498 play_tune(TUNE_TITLE_PAGE);
501 case HIGH_SCORE_DISPLAY:
502 // Display de list o high scores mon.
503 display_scores(surf_screen, 150,50);
507 ; // no action necessary
510 if(state == GAMEPLAY) {
511 bang = hit_rocks(shipx, shipy, &shipshape);
514 ms_frame = SDL_GetTicks() - ms_end;
516 if(ms_frame>200 || ms_frame<0) {
517 // We won't run at all below 5 frames per second.
518 // This also happens if we were paused, grr.
522 t_frame = opt_gamespeed * ms_frame / 50;
523 if(state == GAMEPLAY) score += ms_frame;
526 // Update the surface
527 SDL_Flip(surf_screen);
541 // Count down the game loop timer, and change state when it gets to zero or less;
543 if((state_timeout -= t_frame*3) < 0) {
546 // Create a new ship and start all over again
548 play_tune(TUNE_GAMEPLAY);
551 if(new_high_score(score)) {
553 state = HIGH_SCORE_ENTRY;
554 state_timeout = 5.0e6;
555 SDL_EnableUNICODE(1);
556 while(SDL_PollEvent(&e))
558 } else if(!keystate[SDLK_SPACE]) {
559 state = HIGH_SCORE_DISPLAY;
563 case HIGH_SCORE_DISPLAY:
565 state_timeout = 500.0;
567 case HIGH_SCORE_ENTRY:
570 state = HIGH_SCORE_DISPLAY;
571 state_timeout = 200.0;
574 ; // no action necessary
577 if(state == DEAD_PAUSE) {
581 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
582 blast_radius = BLAST_RADIUS * 1.3;
585 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
588 blast_rocks(bangx, bangy, blast_radius, fixonly);
590 if(bangx < 60) bangx = 60;
597 shipx += shipdx*t_frame;
598 shipy += shipdy*t_frame;
601 tmp = (shipy-YSCROLLTO)/25 + (shipdy-screendy);
602 screendy += tmp * t_frame/12;
603 tmp = (shipx-XSCROLLTO)/25 + (shipdx-screendx);
604 screendx += tmp * t_frame/12;
605 // taper off so we don't hit the barrier abruptly.
606 // (if we would hit in < 2 seconds, adjust to 2 seconds).
607 if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0) {
608 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
611 back_dist += (screendx - SCREENDXMIN)*t_frame;
612 if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
614 xscroll = screendx * t_frame;
615 yscroll = screendy * t_frame;
621 bangx += bangdx*t_frame - xscroll;
622 bangy += bangdy*t_frame - yscroll;
628 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
629 // BOUNCE from left and right wall
630 shipx -= (shipdx-screendx)*t_frame;
631 shipdx = screendx - (shipdx-screendx)*opt_bounciness;
635 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
636 // BOUNCE from top and bottom wall
637 shipy -= (shipdy-screendy)*t_frame;
638 shipdy = screendy - (shipdy-screendy)*opt_bounciness;
642 if(draw() && state == GAMEPLAY) {
644 play_sound(SOUND_BANG); // Play the explosion sound
645 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
646 new_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship);
647 shipdx *= 0.5; shipdy *= 0.5;
648 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
651 shipdx = SCREENDXMIN; shipdy = 0;
652 state_timeout = 200.0;
658 state_timeout = DEAD_PAUSE_LENGTH;
663 keystate = SDL_GetKeyState(NULL);
666 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
674 play_tune(TUNE_GAMEPLAY);
676 shipx = XSIZE/2.2; shipy = YSIZE/2;
677 shipdx = screendx; shipdy = screendy;
683 keystate = SDL_GetKeyState(NULL);
686 if(state == GAMEPLAY) {
688 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*t_frame; jets |= 1<<0;}
689 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*t_frame; jets |= 1<<1;}
690 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*t_frame; jets |= 1<<2;}
691 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*t_frame; jets |= 1<<3;}
692 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
695 if(keystate[SDLK_p] | keystate[SDLK_s]) {
703 } else if(state == GAME_OVER) {
704 if(keystate[SDLK_SPACE]) {
709 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
717 main(int argc, char **argv) {
719 argp_parse(&argp, argc, argv, 0, 0, 0);
722 printf ("ta: '%s'\n",initerror);