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;
156 // r = 1 - frnd()*frnd();
158 bdot[bd2].dx = 45*r*cos(theta) + dx;
159 bdot[bd2].dy = 45*r*sin(theta) + dy;
160 bdot[bd2].x = x + xbang;
161 bdot[bd2].y = y + ybang;
163 bdot[bd2].life = 100;
164 bdot[bd2].decay = frnd()*3 + 1;
165 bdot[bd2].active = 1;
167 // Replace the last few bang dots with the pixels from the exploding object
168 if(endcount>0) bdot[bd2].c = c;
170 bd2 = (bd2+1) % MAXBANGDOTS;
175 if(SDL_GetTicks() - begin_generate > 7) endcount++;
178 SDL_UnlockSurface(s);
182 draw_bang_dots(SDL_Surface *s)
186 uint16_t *pixels, *pixel, c;
187 int row_inc = s->pitch/sizeof(uint16_t);
189 pixels = (uint16_t *) s->pixels;
193 for(i=0; i<MAXBANGDOTS; i++) {
194 if(!bdot[i].active) continue;
196 // If the dot has drifted outside the screen, kill it
197 if(bdot[i].x < 0 || bdot[i].x >= XSIZE || bdot[i].y < 0 || bdot[i].y >= YSIZE) {
201 if(pixel_hit_rocks(bdot[i].x, bdot[i].y)) { bdot[i].active = 0; continue; }
202 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
203 if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
205 bdot[i].life -= bdot[i].decay;
206 bdot[i].x += bdot[i].dx*t_frame - xscroll;
207 bdot[i].y += bdot[i].dy*t_frame - yscroll;
209 if(bdot[i].life<0) bdot[i].active = 0;
215 new_engine_dots(int n, int dir) {
217 float a, r; // angle, random length
219 float hx, hy; // half ship width/height.
220 static const int s[4] = { 2, 1, 0, 1 };
222 hx = surf_ship->w / 2;
223 hy = surf_ship->h / 2;
225 for(i = 0; i<n; i++) {
226 if(dotptr->active == 0) {
227 a = frnd()*M_PI + (dir-1)*M_PI_2;
228 r = sin(frnd()*M_PI);
230 dy = r * -sin(a); // screen y is "backwards".
233 dotptr->x = shipx + s[dir]*hx + (frnd()-0.5)*3;
234 dotptr->y = shipy + s[(dir+1)&3]*hy + (frnd()-0.5)*3;
236 dotptr->dx = shipdx + 2*dx;
237 dotptr->dy = shipdy + 20*dy;
238 dotptr->life = 60 * fabs(dy);
240 dotptr->dx = shipdx + 20*dx;
241 dotptr->dy = shipdy + 2*dy;
242 dotptr->life = 60 * fabs(dx);
245 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
252 draw_engine_dots(SDL_Surface *s) {
255 uint16_t *pixels = (uint16_t *) s->pixels;
256 int row_inc = s->pitch/sizeof(uint16_t);
259 for(i = 0; i<MAXENGINEDOTS; i++) {
260 if(!edot[i].active) continue;
261 edot[i].x += edot[i].dx*t_frame - xscroll;
262 edot[i].y += edot[i].dy*t_frame - yscroll;
263 edot[i].life -= t_frame*3;
265 || edot[i].x<0 || edot[i].x >= XSIZE
266 || edot[i].y<0 || edot[i].y >= YSIZE) {
270 if(pixel_hit_rocks(edot[i].x, edot[i].y)) { edot[i].active = 0; continue; }
271 heatindex = edot[i].life * 6;
272 c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
273 pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
278 drawdots(SDL_Surface *s) {
281 // Create engine dots out the side we're moving from
282 for(m = 0; m<4; m++) {
283 if(jets & 1<<m) { // 'jets' is a bit field
284 new_engine_dots(80,m);
294 SDL_UnlockSurface(s);
304 // Where are our data files?
305 if(!find_files()) exit(1);
306 read_high_score_table();
309 // Initialize SDL with audio and video
310 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
312 printf ("Can't open sound, starting without it\n");
316 atexit(SDL_CloseAudio);
317 opt_sound = init_sound();
320 // Initialize with video only
321 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
325 play_tune(TUNE_TITLE_PAGE);
327 // Attempt to get the required video size
328 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
329 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
330 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
332 // Set the title bar text
333 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
335 NULLERROR(surf_screen);
337 // Set the heat color from the range 0 (cold) to 300 (blue-white)
338 for(i = 0; i<W*3; i++) {
339 heatcolor[i] = SDL_MapRGB(
341 (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?
346 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
347 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
349 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
350 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
352 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
353 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
355 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
356 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
358 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
359 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
361 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
362 g_font = SFont_InitFont(surf_font_big);
364 // Load the spaceship graphic.
365 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
366 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
367 get_shape(surf_ship, &shipshape);
369 // Load the life indicator (small ship) graphic.
370 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
371 NULLERROR(surf_life = SDL_DisplayFormat(temp));
378 // Remove the mouse cursor
380 SDL_ShowCursor(SDL_DISABLE);
392 float fadegame,fadeover;
396 // Draw a fully black background
397 SDL_FillRect(surf_screen,NULL,0);
399 // Draw the background dots
400 drawdots(surf_screen);
403 if(state == GAMEPLAY ) {
406 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
411 // Draw the life indicators.
412 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER) {
413 for(i = 0; i<nships-1; i++) {
414 dest.x = (i + 1)*(surf_life->w + 10);
416 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
421 snprintscore_line(topline, 50, score);
422 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
424 // If it's game over, show the game over graphic in the dead centre
427 if(fadetimer<3.0/faderate) {
428 fadegame = fadetimer/(3.0/faderate);
433 if(fadetimer<3.0/faderate) {
435 } else if(fadetimer<6.0/faderate) {
436 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
441 dest.x = (XSIZE-surf_b_game->w)/2;
442 dest.y = (YSIZE-surf_b_game->h)/2-40;
443 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += t_frame/1.0))));
444 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
446 dest.x = (XSIZE-surf_b_over->w)/2;
447 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
448 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
449 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
454 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
455 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
456 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += t_frame/2.0)));
457 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
459 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
460 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
461 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
462 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
464 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
465 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
466 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
467 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
469 text = "Version " VERSION;
470 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
471 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
473 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
474 //text = "Press SPACE to start!";
475 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
476 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
479 case HIGH_SCORE_ENTRY:
480 play_tune(TUNE_HIGH_SCORE_ENTRY);
481 if(!process_score_input()) { // done inputting name
483 // Change state to briefly show high scores page
484 state = HIGH_SCORE_DISPLAY;
487 // Write the high score table to the file
488 write_high_score_table();
490 play_tune(TUNE_TITLE_PAGE);
493 case HIGH_SCORE_DISPLAY:
494 // Display de list o high scores mon.
495 display_scores(surf_screen, 150,50);
499 ; // no action necessary
502 if(state == GAMEPLAY) {
503 bang = hit_rocks(shipx, shipy, &shipshape);
506 ms_frame = SDL_GetTicks() - ms_end;
508 if(ms_frame>200 || ms_frame<0) {
509 // We won't run at all below 5 frames per second.
510 // This also happens if we were paused, grr.
514 t_frame = opt_gamespeed * ms_frame / 50;
515 if(state == GAMEPLAY) score += ms_frame;
518 // Update the surface
519 SDL_Flip(surf_screen);
533 // Count down the game loop timer, and change state when it gets to zero or less;
535 if((state_timeout -= t_frame*3) < 0) {
538 // Create a new ship and start all over again
540 play_tune(TUNE_GAMEPLAY);
543 if(new_high_score(score)) {
545 state = HIGH_SCORE_ENTRY;
546 state_timeout = 5.0e6;
547 SDL_EnableUNICODE(1);
548 while(SDL_PollEvent(&e))
550 } else if(!keystate[SDLK_SPACE]) {
551 state = HIGH_SCORE_DISPLAY;
555 case HIGH_SCORE_DISPLAY:
557 state_timeout = 500.0;
559 case HIGH_SCORE_ENTRY:
562 state = HIGH_SCORE_DISPLAY;
563 state_timeout = 200.0;
566 ; // no action necessary
569 if(state == DEAD_PAUSE) {
573 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
574 blast_radius = BLAST_RADIUS * 1.3;
577 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
580 blast_rocks(bangx, bangy, blast_radius, fixonly);
582 if(bangx < 60) bangx = 60;
589 shipx += shipdx*t_frame;
590 shipy += shipdy*t_frame;
593 tmp = (shipy-YSCROLLTO)/25 + (shipdy-screendy);
594 screendy += tmp * t_frame/12;
595 tmp = (shipx-XSCROLLTO)/25 + (shipdx-screendx);
596 screendx += tmp * t_frame/12;
597 // taper off so we don't hit the barrier abruptly.
598 // (if we would hit in < 2 seconds, adjust to 2 seconds).
599 if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0) {
600 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
603 back_dist += (screendx - SCREENDXMIN)*t_frame;
604 if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
606 xscroll = screendx * t_frame;
607 yscroll = screendy * t_frame;
613 bangx += bangdx*t_frame - xscroll;
614 bangy += bangdy*t_frame - yscroll;
620 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
621 // BOUNCE from left and right wall
622 shipx -= (shipdx-screendx)*t_frame;
623 shipdx = screendx - (shipdx-screendx)*opt_bounciness;
627 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
628 // BOUNCE from top and bottom wall
629 shipy -= (shipdy-screendy)*t_frame;
630 shipdy = screendy - (shipdy-screendy)*opt_bounciness;
634 if(draw() && state == GAMEPLAY) {
636 play_sound(SOUND_BANG); // Play the explosion sound
637 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
638 new_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship);
639 shipdx *= 0.5; shipdy *= 0.5;
640 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
643 shipdx = SCREENDXMIN; shipdy = 0;
644 state_timeout = 200.0;
650 state_timeout = DEAD_PAUSE_LENGTH;
655 keystate = SDL_GetKeyState(NULL);
658 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
666 play_tune(TUNE_GAMEPLAY);
668 shipx = XSIZE/2.2; shipy = YSIZE/2;
669 shipdx = screendx; shipdy = screendy;
675 keystate = SDL_GetKeyState(NULL);
678 if(state == GAMEPLAY) {
680 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*t_frame; jets |= 1<<0;}
681 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*t_frame; jets |= 1<<1;}
682 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*t_frame; jets |= 1<<2;}
683 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*t_frame; jets |= 1<<3;}
684 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
687 if(keystate[SDLK_p] | keystate[SDLK_s]) {
695 } else if(state == GAME_OVER) {
696 if(keystate[SDLK_SPACE]) {
701 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
709 main(int argc, char **argv) {
711 argp_parse(&argp, argc, argv, 0, 0, 0);
714 printf ("ta: '%s'\n",initerror);