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_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_life, // Indicator of number of ships remaining
55 *surf_rock[NROCKS], // THE ROCKS
56 *surf_font_big; // The big font
61 // Structure global variables
62 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
63 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
65 // Other global variables
69 struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 };
71 float screendx = SCREENDXMIN, screendy = 0.0;
74 // all movement is based on t_frame.
75 float t_frame; // length of this frame (in ticks = 1/20th second)
76 int ms_frame; // length of this frame (milliseconds)
77 int ms_end; // end of this frame (milliseconds)
80 float bangx, bangy, bangdx, bangdy;
84 float fadetimer = 0, faderate;
86 int pausedown = 0, paused = 0;
88 // bangdot start (bd1) and end (bd2) position:
99 enum states state = TITLE_PAGE;
100 float state_timeout = 600.0;
104 "Press SPACE to start",
105 "http://jasonwoof.org/vor"
108 int bangdotlife, nbangdots;
109 Uint16 heatcolor[W*3];
113 extern int optind, opterr, optopt;
115 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
117 // ************************************* FUNCS
122 for(i = 0; i<MAXENGINEDOTS; i++) {
128 new_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s)
137 begin_generate = SDL_GetTicks();
139 row_inc = s->pitch/sizeof(uint16_t) - s->w;
140 colorkey = s->format->colorkey;
147 for(y=0; y<s->h; y++) {
148 for(x = 0; x<s->w; x++) {
150 if(c && c != colorkey) {
151 theta = frnd()*M_PI*2;
152 r = frnd(); r = 1 - r*r;
153 // r = 1 - frnd()*frnd();
155 bdot[bd2].dx = 45*r*cos(theta) + dx;
156 bdot[bd2].dy = 45*r*sin(theta) + dy;
157 bdot[bd2].x = x + xbang;
158 bdot[bd2].y = y + ybang;
160 bdot[bd2].life = 100;
161 bdot[bd2].decay = frnd()*3 + 1;
162 bdot[bd2].active = 1;
164 // Replace the last few bang dots with the pixels from the exploding object
165 if(endcount>0) bdot[bd2].c = c;
167 bd2 = (bd2+1) % MAXBANGDOTS;
172 if(SDL_GetTicks() - begin_generate > 7) endcount++;
175 SDL_UnlockSurface(s);
179 draw_bang_dots(SDL_Surface *s)
183 uint16_t *pixels, *pixel, c;
184 int row_inc = s->pitch/sizeof(uint16_t);
186 pixels = (uint16_t *) s->pixels;
190 for(i=0; i<MAXBANGDOTS; i++) {
191 if(!bdot[i].active) continue;
193 // decrement life and maybe kill
194 bdot[i].life -= bdot[i].decay;
195 if(bdot[i].life<0) { bdot[i].active = 0; continue; }
198 bdot[i].x += (bdot[i].dx - screendx)*t_frame;
199 bdot[i].y += (bdot[i].dy - screendy)*t_frame;
200 if(bdot[i].x < 0 || bdot[i].x >= XSIZE || bdot[i].y < 0 || bdot[i].y >= YSIZE) {
206 if(pixel_collides(bdot[i].x, bdot[i].y)) { bdot[i].active = 0; continue; }
208 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
209 if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
216 new_engine_dots(int n, int dir) {
218 float a, r; // angle, random length
220 float hx, hy; // half ship width/height.
221 static const int s[4] = { 2, 1, 0, 1 };
223 hx = ship.image->w / 2;
224 hy = ship.image->h / 2;
226 for(i = 0; i<n; i++) {
227 if(dotptr->active == 0) {
228 a = frnd()*M_PI + (dir-1)*M_PI_2;
229 r = sin(frnd()*M_PI);
231 dy = r * -sin(a); // screen y is "backwards".
234 dotptr->x = ship.x + s[dir]*hx + (frnd()-0.5)*3;
235 dotptr->y = ship.y + s[(dir+1)&3]*hy + (frnd()-0.5)*3;
237 dotptr->dx = ship.dx + 2*dx;
238 dotptr->dy = ship.dy + 20*dy;
239 dotptr->life = 60 * fabs(dy);
241 dotptr->dx = ship.dx + 20*dx;
242 dotptr->dy = ship.dy + 2*dy;
243 dotptr->life = 60 * fabs(dx);
246 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
253 draw_engine_dots(SDL_Surface *s) {
256 uint16_t *pixels = (uint16_t *) s->pixels;
257 int row_inc = s->pitch/sizeof(uint16_t);
260 for(i = 0; i<MAXENGINEDOTS; i++) {
261 if(!edot[i].active) continue;
262 edot[i].x += (edot[i].dx - screendx)*t_frame;
263 edot[i].y += (edot[i].dy - screendy)*t_frame;
264 edot[i].life -= t_frame*3;
266 || edot[i].x<0 || edot[i].x >= XSIZE
267 || edot[i].y<0 || edot[i].y >= YSIZE) {
271 if(pixel_collides(edot[i].x, edot[i].y)) { edot[i].active = 0; continue; }
272 heatindex = edot[i].life * 6;
273 c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
274 pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
279 drawdots(SDL_Surface *s) {
282 // Create engine dots out the side we're moving from
283 for(m = 0; m<4; m++) {
284 if(ship.jets & 1<<m) { // 'jets' is a bit field
285 new_engine_dots(80,m);
295 SDL_UnlockSurface(s);
299 load_image(char *filename)
301 SDL_Surface *tmp, *img = NULL;
302 char *s = add_data_path(filename);
307 img = SDL_DisplayFormat(tmp);
308 SDL_FreeSurface(tmp);
317 load_sprite(SPRITE(&ship), "sprites/ship.png");
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 fputs("Can't open sound, starting without it\n", stderr);
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(surf_b_variations = load_image("banners/variations.png"));
370 NULLERROR(surf_b_on = load_image("banners/on.png"));
371 NULLERROR(surf_b_rockdodger = load_image("banners/rockdodger.png"));
373 NULLERROR(surf_b_game = load_image("banners/game.png"));
374 NULLERROR(surf_b_over = load_image("banners/over.png"));
376 // Load the life indicator (small ship) graphic.
377 NULLERROR(surf_life = load_image("indicators/life.png"));
379 // Load the font image
380 s = add_data_path(BIG_FONT_FILE);
382 NULLERROR(surf_font_big = IMG_Load(s));
384 g_font = SFont_InitFont(surf_font_big);
391 add_sprite(SPRITE(&ship));
393 // Remove the mouse cursor
395 SDL_ShowCursor(SDL_DISABLE);
407 for(i=0; i<ship.lives-1; i++) {
408 dest.x = (i + 1)*(surf_life->w + 10);
410 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
417 float a_game = 0, a_over = 0;
420 // fade in "GAME", then "OVER".
421 a_game = min(1.0, faderate*fadetimer/3.0);
422 if(a_game == 1.0) a_over = min(1.0, faderate*fadetimer/3.0 - 1);
424 fadetimer += t_frame;
426 dest.x = (XSIZE-surf_b_game->w)/2;
427 dest.y = (YSIZE-surf_b_game->h)/2-40;
428 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(a_game*(200 + 55*cos(fadetimer))));
429 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
431 dest.x = (XSIZE-surf_b_over->w)/2;
432 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
433 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(a_over*(200 + 55*sin(fadetimer))));
434 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
438 draw_title_page(void)
444 fadetimer += t_frame/2.0;
446 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
447 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
448 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer)));
449 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
451 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
452 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
453 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
454 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
456 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
457 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
458 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
459 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
461 text = "Version " VERSION;
462 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
463 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
465 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
466 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
467 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
473 SDL_FillRect(surf_screen,NULL,0); // black background
474 drawdots(surf_screen); // background dots
475 draw_sprite(SPRITE(&ship));
481 // If it's game over, show the game over graphic in the dead centre
483 case GAME_OVER: draw_game_over(); break;
485 case TITLE_PAGE: draw_title_page(); break;
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
512 ms_frame = SDL_GetTicks() - ms_end;
514 t_frame = opt_gamespeed * ms_frame / 50;
515 if(state == GAMEPLAY) score += ms_frame;
517 // Update the surface
518 SDL_Flip(surf_screen);
522 kill_ship(Sprite *ship)
525 // ship->flags = MOVE|DRAW; // FADE SHIP
526 // SDL_SetAlpha(ship->image, SDL_SRCALPHA, 0); // FADE SHIP
531 do_collision(Sprite *a, Sprite *b)
533 if(a->type == SHIP) kill_ship(a);
534 else if (b->type == SHIP) kill_ship(b);
539 init_score_entry(void)
542 state = HIGH_SCORE_ENTRY;
543 state_timeout = 5.0e6;
544 SDL_EnableUNICODE(1);
545 while(SDL_PollEvent(&e))
551 Uint8 *keystate = SDL_GetKeyState(NULL);
557 keystate = SDL_GetKeyState(NULL);
560 // Count down the game loop timer, and change state when it gets to zero or less;
562 if((state_timeout -= t_frame*3) < 0) {
565 // Restore the ship and continue playing
566 ship.flags = DRAW|MOVE|COLLIDE;
568 play_tune(TUNE_GAMEPLAY);
571 if(new_high_score(score)) init_score_entry();
573 state = HIGH_SCORE_DISPLAY;
577 case HIGH_SCORE_DISPLAY:
579 state_timeout = 500.0;
581 case HIGH_SCORE_ENTRY:
584 state = HIGH_SCORE_DISPLAY;
585 state_timeout = 200.0;
588 ; // no action necessary
591 if(state == DEAD_PAUSE) {
593 // float alpha; // FADE SHIP
594 if(state_timeout >= DEAD_PAUSE_LENGTH - 20.0) {
595 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
596 blast_rocks(bangx, bangy, blast_radius);
599 if(bangx < 60) bangx = 60;
602 // alpha = 255.0 * (DEAD_PAUSE_LENGTH - state_timeout) / DEAD_PAUSE_LENGTH;
603 // SDL_SetAlpha(ship.image, SDL_SRCALPHA, (uint8_t)alpha);
610 tmp = (ship.y+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy);
611 screendy += tmp * t_frame/12;
612 tmp = (ship.x+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx);
613 screendx += tmp * t_frame/12;
614 // taper off so we don't hit the barrier abruptly.
615 // (if we would hit in < 2 seconds, adjust to 2 seconds).
616 if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0)
617 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
618 back_dist += (screendx - SCREENDXMIN)*t_frame;
619 if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
622 bangx += (bangdx - screendx)*t_frame;
623 bangy += (bangdy - screendy)*t_frame;
628 // BOUNCE off left or right edge of screen
629 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
630 ship.x -= (ship.dx-screendx)*t_frame;
631 ship.dx = screendx - (ship.dx-screendx)*opt_bounciness;
634 // BOUNCE off top or bottom of screen
635 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
636 ship.y -= (ship.dy-screendy)*t_frame;
637 ship.dy = screendy - (ship.dy-screendy)*opt_bounciness;
642 if(state == GAMEPLAY && bang) {
645 play_sound(SOUND_BANG); // Play the explosion sound
646 bangx = ship.x; bangy = ship.y; bangdx = ship.dx; bangdy = ship.dy;
647 new_bang_dots(ship.x,ship.y,ship.dx,ship.dy,ship.image);
651 state_timeout = DEAD_PAUSE_LENGTH;
652 ship.dx = (ship.dx < 0) ? -sqrt(-ship.dx) : sqrt(ship.dx);
653 ship.dy = (ship.dy < 0) ? -sqrt(-ship.dy) : sqrt(ship.dy);
654 if(ship.dx < SCREENDXMIN) ship.dx = SCREENDXMIN;
657 ship.dx = SCREENDXMIN; ship.dy = 0;
658 state_timeout = 200.0;
665 if(keystate[SDLK_SPACE]
666 && (state == HIGH_SCORE_DISPLAY
667 || state == TITLE_PAGE
668 || state == GAME_OVER)) {
669 if(state == GAME_OVER && new_high_score(score))
674 screendx = SCREENDXMIN; screendy = 0;
676 ship.x = XSIZE/2.2; ship.y = YSIZE/2;
677 ship.dx = screendx; ship.dy = screendy;
679 ship.flags = MOVE|DRAW|COLLIDE;
680 // SDL_SetAlpha(ship.image, SDL_SRCALPHA, SDL_ALPHA_OPAQUE); // FADE SHIP
681 add_sprite(SPRITE(&ship));
686 play_tune(TUNE_GAMEPLAY);
693 if(state == GAMEPLAY) {
695 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { ship.dx -= 1.5*t_frame; ship.jets |= 1<<0;}
696 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { ship.dy += 1.5*t_frame; ship.jets |= 1<<1;}
697 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { ship.dx += 1.5*t_frame; ship.jets |= 1<<2;}
698 if(keystate[SDLK_UP] | keystate[SDLK_c]) { ship.dy -= 1.5*t_frame; ship.jets |= 1<<3;}
699 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
702 if(keystate[SDLK_p] | keystate[SDLK_s]) {
706 if(!paused) ms_end = SDL_GetTicks();
713 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE]))
720 main(int argc, char **argv) {
722 argp_parse(&argp, argc, argv, 0, 0, 0);
725 printf ("ta: '%s'\n",initerror);