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
24 #include <SDL_image.h>
44 // ************************************* VARS
45 // SDL_Surface global variables
47 *surf_screen, // Screen
48 *surf_b_variations, // "variations" banner
49 *surf_b_on, // "on" banner
50 *surf_b_rockdodger, // "rockdodger" banner
51 *surf_b_game, // Title element "game"
52 *surf_b_over, // Title element "over"
53 *surf_life, // Indicator of number of ships remaining
54 *surf_rock[NROCKS], // THE ROCKS
55 *surf_font_big; // The big font
60 // Structure global variables
61 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
62 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
64 // Other global variables
68 struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 };
70 float screendx = SCREENDXMIN, screendy = 0.0;
73 // all movement is based on t_frame.
74 float t_frame; // length of this frame (in ticks = 1/20th second)
75 int ms_frame; // length of this frame (milliseconds)
76 int ms_end; // end of this frame (milliseconds)
79 float bangx, bangy, bangdx, bangdy;
83 float fadetimer = 0, faderate;
85 int pausedown = 0, paused = 0;
87 // bangdot start (bd1) and end (bd2) position:
98 enum states state = TITLE_PAGE;
99 float state_timeout = 600.0;
104 "Press SPACE for normal game",
105 "Press '1' for easy game",
106 "http://jasonwoof.org/vor"
109 "Press SPACE for easy game",
110 "Press '2' for normal game",
111 "http://jasonwoof.org/vor"
115 int bangdotlife, nbangdots;
116 Uint16 heatcolor[W*3];
120 extern int optind, opterr, optopt;
122 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
124 // ************************************* FUNCS
129 for(i = 0; i<MAXENGINEDOTS; i++) {
135 new_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s)
144 begin_generate = SDL_GetTicks();
146 row_inc = s->pitch/sizeof(uint16_t) - s->w;
147 colorkey = s->format->colorkey;
154 for(y=0; y<s->h; y++) {
155 for(x = 0; x<s->w; x++) {
157 if(c && c != colorkey) {
158 theta = frnd()*M_PI*2;
159 r = frnd(); r = 1 - r*r;
160 // r = 1 - frnd()*frnd();
162 bdot[bd2].dx = 45*r*cos(theta) + dx;
163 bdot[bd2].dy = 45*r*sin(theta) + dy;
164 bdot[bd2].x = x + xbang;
165 bdot[bd2].y = y + ybang;
167 bdot[bd2].life = 100;
168 bdot[bd2].decay = frnd()*3 + 1;
169 bdot[bd2].active = 1;
171 // Replace the last few bang dots with the pixels from the exploding object
172 if(endcount>0) bdot[bd2].c = c;
174 bd2 = (bd2+1) % MAXBANGDOTS;
179 if(SDL_GetTicks() - begin_generate > 7) endcount++;
182 SDL_UnlockSurface(s);
186 draw_bang_dots(SDL_Surface *s)
190 uint16_t *pixels, *pixel, c;
191 int row_inc = s->pitch/sizeof(uint16_t);
194 pixels = (uint16_t *) s->pixels;
198 for(i=0; i<MAXBANGDOTS; i++) {
199 if(!bdot[i].active) continue;
201 // decrement life and maybe kill
202 bdot[i].life -= bdot[i].decay;
203 if(bdot[i].life<0) { bdot[i].active = 0; continue; }
206 bdot[i].x += (bdot[i].dx - screendx)*t_frame;
207 bdot[i].y += (bdot[i].dy - screendy)*t_frame;
208 if(bdot[i].x < 0 || bdot[i].x >= XSIZE || bdot[i].y < 0 || bdot[i].y >= YSIZE) {
214 if((hit = pixel_collides(bdot[i].x, bdot[i].y))) {
215 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
217 hit->dx += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dx / sprite_mass(hit);
218 hit->dy += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dy / sprite_mass(hit);
223 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
224 if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
231 new_engine_dots(int n, int dir) {
233 float a, r; // angle, random length
235 float hx, hy; // half ship width/height.
236 static const int s[4] = { 2, 1, 0, 1 };
238 hx = ship.image->w / 2;
239 hy = ship.image->h / 2;
241 for(i = 0; i<n; i++) {
242 if(dotptr->active == 0) {
243 a = frnd()*M_PI + (dir-1)*M_PI_2;
244 r = sin(frnd()*M_PI);
246 dy = r * -sin(a); // screen y is "backwards".
249 dotptr->x = ship.x + s[dir]*hx + (frnd()-0.5)*3;
250 dotptr->y = ship.y + s[(dir+1)&3]*hy + (frnd()-0.5)*3;
252 dotptr->dx = ship.dx + 2*dx;
253 dotptr->dy = ship.dy + 20*dy;
254 dotptr->life = 60 * fabs(dy);
256 dotptr->dx = ship.dx + 20*dx;
257 dotptr->dy = ship.dy + 2*dy;
258 dotptr->life = 60 * fabs(dx);
261 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
268 draw_engine_dots(SDL_Surface *s) {
271 uint16_t *pixels = (uint16_t *) s->pixels;
272 int row_inc = s->pitch/sizeof(uint16_t);
276 for(i = 0; i<MAXENGINEDOTS; i++) {
277 if(!edot[i].active) continue;
278 edot[i].x += (edot[i].dx - screendx)*t_frame;
279 edot[i].y += (edot[i].dy - screendy)*t_frame;
280 edot[i].life -= t_frame*3;
282 || edot[i].x<0 || edot[i].x >= XSIZE
283 || edot[i].y<0 || edot[i].y >= YSIZE) {
288 if((hit = pixel_collides(edot[i].x, edot[i].y))) {
289 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
291 hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit);
292 hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit);
296 heatindex = edot[i].life * 6;
297 c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
298 pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
303 drawdots(SDL_Surface *s) {
306 // Create engine dots out the side we're moving from
307 for(m = 0; m<4; m++) {
308 if(ship.jets & 1<<m) { // 'jets' is a bit field
309 new_engine_dots(80,m);
319 SDL_UnlockSurface(s);
323 load_image(char *filename)
325 SDL_Surface *tmp, *img = NULL;
326 char *s = add_data_path(filename);
331 img = SDL_DisplayFormat(tmp);
332 SDL_FreeSurface(tmp);
341 load_sprite(SPRITE(&ship), "ship.png");
351 // Where are our data files?
352 if(!find_files()) exit(1);
353 read_high_score_table();
356 // Initialize SDL with audio and video
357 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
359 fputs("Can't open sound, starting without it\n", stderr);
363 atexit(SDL_CloseAudio);
364 opt_sound = init_sound();
367 // Initialize with video only
368 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
372 play_tune(TUNE_TITLE_PAGE);
374 // Attempt to get the required video size
375 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
376 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
377 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
379 // Set the title bar text
380 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
382 NULLERROR(surf_screen);
384 // Set the heat color from the range 0 (cold) to 300 (blue-white)
385 for(i = 0; i<W*3; i++) {
386 heatcolor[i] = SDL_MapRGB(
388 (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?
393 NULLERROR(surf_b_variations = load_image("b_variations.png"));
394 NULLERROR(surf_b_on = load_image("b_on.png"));
395 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
397 NULLERROR(surf_b_game = load_image("b_game.png"));
398 NULLERROR(surf_b_over = load_image("b_over.png"));
400 // Load the life indicator (small ship) graphic.
401 NULLERROR(surf_life = load_image("life.png"));
403 // Load the font image
404 s = add_data_path("font.png");
406 NULLERROR(surf_font_big = IMG_Load(s));
408 g_font = SFont_InitFont(surf_font_big);
415 add_sprite(SPRITE(&ship));
417 // Remove the mouse cursor
419 SDL_ShowCursor(SDL_DISABLE);
431 for(i=0; i<ship.lives-1; i++) {
432 dest.x = (i + 1)*(surf_life->w + 10);
434 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
444 float a_game = 0, a_over = 0;
446 // fade in "GAME", then "OVER".
447 a_game = min(1.0, faderate*fadetimer/3.0);
448 if(a_game == 1.0) a_over = min(1.0, faderate*fadetimer/3.0 - 1);
450 fadetimer += t_frame;
452 dest.x = (XSIZE-surf_b_game->w)/2;
453 dest.y = (YSIZE-surf_b_game->h)/2-40;
454 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(a_game*(200 + 55*cos(fadetimer))));
455 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
457 dest.x = (XSIZE-surf_b_over->w)/2;
458 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
459 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(a_over*(200 + 55*sin(fadetimer))));
460 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
462 if(new_high_score(score)) {
463 text0 = "New High Score!";
464 text1 = "Press SPACE to continue";
466 text0 = msgs[g_easy][0];
467 text1 = msgs[g_easy][1];
470 x = (XSIZE-SFont_TextWidth(g_font,text0))/2 + cos(fadetimer/9)*10;
471 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/6)*5,text0);
473 x = (XSIZE-SFont_TextWidth(g_font,text1))/2 + sin(fadetimer/9)*10;
474 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/4)*5,text1);
478 draw_title_page(void)
484 fadetimer += t_frame/2.0;
486 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
487 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
488 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer)));
489 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
491 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
492 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
493 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
494 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
496 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
497 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
498 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
499 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
501 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
502 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
503 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
505 text = "Version " VERSION;
506 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
507 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
513 SDL_FillRect(surf_screen,NULL,0); // black background
514 drawdots(surf_screen); // background dots
515 draw_sprite(SPRITE(&ship));
521 // If it's game over, show the game over graphic in the dead centre
523 case GAME_OVER: draw_game_over(); break;
525 case TITLE_PAGE: draw_title_page(); break;
527 case HIGH_SCORE_ENTRY:
528 play_tune(TUNE_HIGH_SCORE_ENTRY);
529 if(!process_score_input()) { // done inputting name
531 // Change state to briefly show high scores page
532 state = HIGH_SCORE_DISPLAY;
535 // Write the high score table to the file
536 write_high_score_table();
538 play_tune(TUNE_TITLE_PAGE);
541 case HIGH_SCORE_DISPLAY:
542 // Display de list o high scores mon.
543 display_scores(surf_screen, 150,50);
547 ; // no action necessary
552 ms_frame = SDL_GetTicks() - ms_end;
554 t_frame = opt_gamespeed * ms_frame / 50;
555 if(state == GAMEPLAY) score += ms_frame;
557 // Update the surface
558 SDL_Flip(surf_screen);
562 kill_ship(Sprite *ship)
569 do_collision(Sprite *a, Sprite *b)
571 if(a->type == SHIP) kill_ship(a);
572 else if (b->type == SHIP) kill_ship(b);
577 init_score_entry(void)
580 state = HIGH_SCORE_ENTRY;
581 state_timeout = 5.0e6;
582 SDL_EnableUNICODE(1);
583 while(SDL_PollEvent(&e))
596 while(SDL_PollEvent(&e)) {
597 if(e.type == SDL_QUIT) return;
599 keystate = SDL_GetKeyState(NULL);
602 // Count down the game loop timer, and change state when it gets to zero or less;
604 if((state_timeout -= t_frame*3) < 0) {
607 // Restore the ship and continue playing
608 ship.flags = DRAW|MOVE|COLLIDE;
610 play_tune(TUNE_GAMEPLAY);
613 if(new_high_score(score)) init_score_entry();
615 state = HIGH_SCORE_DISPLAY;
619 case HIGH_SCORE_DISPLAY:
621 state_timeout = 600.0;
624 case HIGH_SCORE_ENTRY:
627 state = HIGH_SCORE_DISPLAY;
628 state_timeout = 200.0;
631 ; // no action necessary
634 if(state == DEAD_PAUSE) {
635 if(bangx < 60) bangx = 60;
642 tmp = (ship.y+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy);
643 screendy += tmp * t_frame/12;
644 tmp = (ship.x+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx);
645 screendx += tmp * t_frame/12;
646 // taper off so we don't hit the barrier abruptly.
647 // (if we would hit in < 2 seconds, adjust to 2 seconds).
648 if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0)
649 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
650 back_dist += (screendx - SCREENDXMIN)*t_frame;
651 if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
654 bangx += (bangdx - screendx)*t_frame;
655 bangy += (bangdy - screendy)*t_frame;
660 // BOUNCE off left or right edge of screen
661 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
662 ship.x -= (ship.dx-screendx)*t_frame;
663 ship.dx = screendx - (ship.dx-screendx)*opt_bounciness;
666 // BOUNCE off top or bottom of screen
667 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
668 ship.y -= (ship.dy-screendy)*t_frame;
669 ship.dy = screendy - (ship.dy-screendy)*opt_bounciness;
674 if(state == GAMEPLAY && bang) {
677 play_sound(SOUND_BANG); // Play the explosion sound
678 bangx = ship.x; bangy = ship.y; bangdx = ship.dx; bangdy = ship.dy;
679 new_bang_dots(ship.x,ship.y,ship.dx,ship.dy,ship.image);
683 state_timeout = DEAD_PAUSE_LENGTH;
684 ship.dx = (ship.dx < 0) ? -sqrt(-ship.dx) : sqrt(ship.dx);
685 ship.dy = (ship.dy < 0) ? -sqrt(-ship.dy) : sqrt(ship.dy);
686 if(ship.dx < SCREENDXMIN) ship.dx = SCREENDXMIN;
689 ship.dx = SCREENDXMIN; ship.dy = 0;
690 state_timeout = 200.0;
697 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
698 && (state == HIGH_SCORE_DISPLAY
699 || state == TITLE_PAGE
700 || state == GAME_OVER)) {
701 if(state == GAME_OVER && new_high_score(score))
704 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
706 initial_rocks = NORMAL_I_ROCKS;
707 final_rocks = NORMAL_F_ROCKS;
708 if(opt_gamespeed == EASY_GAMESPEED)
709 opt_gamespeed = NORMAL_GAMESPEED;
710 } else if(keystate[SDLK_1]) {
712 initial_rocks = EASY_I_ROCKS;
713 final_rocks = EASY_F_ROCKS;
714 opt_gamespeed = EASY_GAMESPEED;
718 screendx = SCREENDXMIN; screendy = 0;
720 ship.x = XSIZE/2.2; ship.y = YSIZE/2;
721 ship.dx = screendx; ship.dy = screendy;
723 ship.flags = MOVE|DRAW|COLLIDE;
724 add_sprite(SPRITE(&ship));
729 play_tune(TUNE_GAMEPLAY);
736 if(state == GAMEPLAY) {
738 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { ship.dx -= 1.5*t_frame; ship.jets |= 1<<0;}
739 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { ship.dy += 1.5*t_frame; ship.jets |= 1<<1;}
740 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { ship.dx += 1.5*t_frame; ship.jets |= 1<<2;}
741 if(keystate[SDLK_UP] | keystate[SDLK_c]) { ship.dy -= 1.5*t_frame; ship.jets |= 1<<3;}
742 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
745 if(keystate[SDLK_p] | keystate[SDLK_s]) {
749 if(!paused) ms_end = SDL_GetTicks();
756 if(state == TITLE_PAGE && keystate[SDLK_h]) {
757 state = HIGH_SCORE_DISPLAY;
761 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE]))
768 main(int argc, char **argv) {
769 if(!parse_opts(argc, argv)) return 1;
772 printf ("ta: '%s'\n",initerror);