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>
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, BARRIER_SPEED, 0.0 };
71 float screendx = BARRIER_SPEED, screendy = 0.0;
74 // all movement is based on t_frame.
75 unsigned long frames, start, end;
76 float t_frame; // length of this frame (in ticks = 1/20th second) adjusted for gamespeed
77 int ms_frame; // length of this frame (milliseconds)
78 int ms_end; // end of this frame (milliseconds)
80 float gamespeed = 1.00;
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;
105 "Press SPACE for normal game",
106 "Press '1' for easy game",
107 "http://jasonwoof.org/vor"
110 "Press SPACE for easy game",
111 "Press '2' for normal game",
112 "http://jasonwoof.org/vor"
116 int bangdotlife, nbangdots;
117 Uint16 heatcolor[W*3];
121 extern int optind, opterr, optopt;
123 #define TO_TICKS(seconds) ((seconds)*20*gamespeed)
125 // ************************************* FUNCS
130 for(i = 0; i<MAXENGINEDOTS; i++) {
136 new_bang_dots(struct sprite *s)
143 SDL_Surface *img = s->image;
147 row_inc = img->pitch/sizeof(uint16_t) - img->w;
148 colorkey = img->format->colorkey;
150 if(SDL_MUSTLOCK(img)) { SDL_LockSurface(img); }
154 for(y=0; y<img->h; y++) {
155 for(x = 0; x<img->w; x++) {
157 if(c && c != colorkey) {
158 theta = frnd()*M_PI*2;
159 r = frnd(); r = 1 - r*r;
161 bdot[bd2].dx = 45*r*cos(theta) + s->dx;
162 bdot[bd2].dy = 45*r*sin(theta) + s->dy;
163 bdot[bd2].x = x + s->x;
164 bdot[bd2].y = y + s->y;
165 //bdot[bd2].c = (i < n-3) ? 0 : c;
166 bdot[bd2].life = frnd() * 99;
167 bdot[bd2].decay = frnd()*3 + 1;
168 bdot[bd2].active = 1;
170 bd2 = (bd2+1) % MAXBANGDOTS;
177 if(SDL_MUSTLOCK(img)) { SDL_UnlockSurface(img); }
181 move_bang_dots(float ticks)
186 for(i=0; i<MAXBANGDOTS; i++) {
187 if(!bdot[i].active) continue;
189 // decrement life and maybe kill
190 bdot[i].life -= bdot[i].decay * ticks/2.0;
191 if(bdot[i].life < 0) { bdot[i].active = 0; continue; }
194 bdot[i].x += (bdot[i].dx - screendx)*ticks;
195 bdot[i].y += (bdot[i].dy - screendy)*ticks;
196 if(fclip(bdot[i].x, XSIZE) || fclip(bdot[i].y, YSIZE)) {
202 if((hit = pixel_collides(bdot[i].x, bdot[i].y))) {
203 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
205 hit->dx += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dx / sprite_mass(hit);
206 hit->dy += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dy / sprite_mass(hit);
215 draw_bang_dots(SDL_Surface *s)
218 uint16_t *pixels, *pixel, c;
219 int row_inc = s->pitch/sizeof(uint16_t);
221 pixels = (uint16_t *) s->pixels;
223 for(i=0; i<MAXBANGDOTS; i++) {
224 if(!bdot[i].active) continue;
226 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
227 if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
234 new_engine_dots(float time_span) {
236 int n = time_span * ENGINE_DOTS_PER_TIC;
237 float a, r; // angle, random length
239 float hx, hy; // half ship width/height.
240 static const int s[4] = { 2, 1, 0, 1 };
242 float accelh, accelv, past_ship_dx, past_ship_dy;
244 hx = ship.image->w / 2;
245 hy = ship.image->h / 2;
247 for(dir=0; dir<4; dir++) {
248 if(!(ship.jets & 1<<dir)) continue;
250 for(i = 0; i<n; i++) {
251 if(dotptr->active == 0) {
252 a = frnd()*M_PI + (dir-1)*M_PI_2;
253 r = sin(frnd()*M_PI);
255 dy = r * -sin(a); // screen y is "backwards".
259 // dot was created at a random time during the time span
260 time = frnd() * time_span; // this is how long ago
262 // calculate how fast the ship was going when this engine dot was
263 // created (as if it had a smooth acceleration). This is used in
264 // determining the velocity of the dots, but not their starting
266 accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
267 accelh *= THRUSTER_STRENGTH * time;
268 past_ship_dx = ship.dx - accelh;
269 accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
270 accelv *= THRUSTER_STRENGTH * time;
271 past_ship_dy = ship.dy - accelv;
273 // the starting position (not speed) of the dot is calculated as
274 // though the ship were traveling at a constant speed for this
276 dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
277 dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
279 dotptr->dx = past_ship_dx + 2*dx;
280 dotptr->dy = past_ship_dy + 20*dy;
281 dotptr->life = 60 * fabs(dy);
283 dotptr->dx = past_ship_dx + 20*dx;
284 dotptr->dy = past_ship_dy + 2*dy;
285 dotptr->life = 60 * fabs(dx);
288 // move the dot as though it were created in the past
289 dotptr->x += (dotptr->dx - screendx) * time;
290 dotptr->y += (dotptr->dy - screendy) * time;
292 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
300 move_engine_dots(float ticks) {
304 for(i = 0; i<MAXENGINEDOTS; i++) {
305 if(!edot[i].active) continue;
307 edot[i].x += (edot[i].dx - screendx)*ticks;
308 edot[i].y += (edot[i].dy - screendy)*ticks;
309 edot[i].life -= t_frame*3;
310 if(edot[i].life < 0 || fclip(edot[i].x, XSIZE) || fclip(edot[i].y, YSIZE)) {
316 if((hit = pixel_collides(edot[i].x, edot[i].y))) {
317 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
319 hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit);
320 hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit);
328 draw_engine_dots(SDL_Surface *s) {
331 uint16_t *pixels = (uint16_t *) s->pixels;
332 int row_inc = s->pitch/sizeof(uint16_t);
335 for(i = 0; i<MAXENGINEDOTS; i++) {
336 if(!edot[i].active) continue;
338 heatindex = edot[i].life * 6;
339 c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
340 pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
345 draw_dots(SDL_Surface *s) {
346 if(SDL_MUSTLOCK(s)) { SDL_LockSurface(s); }
350 if(SDL_MUSTLOCK(s)) { SDL_UnlockSurface(s); }
354 load_image(char *filename)
356 SDL_Surface *tmp, *img = NULL;
357 char *s = add_data_path(filename);
362 img = SDL_DisplayFormat(tmp);
363 SDL_FreeSurface(tmp);
372 load_sprite(SPRITE(&ship), "ship.png");
375 void font_cleanup() {
386 // Where are our data files?
387 if(!find_files()) exit(1);
388 read_high_score_table();
391 // Initialize SDL with audio and video
392 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
394 fputs("Can't open sound, starting without it\n", stderr);
398 atexit(SDL_CloseAudio);
399 opt_sound = init_sound();
402 // Initialize with video only
403 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
407 play_tune(TUNE_TITLE_PAGE);
409 // Attempt to get the required video size
410 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
411 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
412 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
414 // Set the title bar text
415 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
417 NULLERROR(surf_screen);
419 // Set the heat color from the range 0 (cold) to 300 (blue-white)
420 for(i = 0; i<W*3; i++) {
421 heatcolor[i] = SDL_MapRGB(
423 (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?
428 NULLERROR(surf_b_variations = load_image("b_variations.png"));
429 NULLERROR(surf_b_on = load_image("b_on.png"));
430 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
432 NULLERROR(surf_b_game = load_image("b_game.png"));
433 NULLERROR(surf_b_over = load_image("b_over.png"));
435 // Load the life indicator (small ship) graphic.
436 NULLERROR(surf_life = load_image("life.png"));
438 // Load the font image
439 s = add_data_path("font.png");
441 g_font = font_load(s);
442 atexit(&font_cleanup);
444 fprintf(stderr, "could create path to font\n");
452 add_sprite(SPRITE(&ship));
454 // Remove the mouse cursor
456 SDL_ShowCursor(SDL_DISABLE);
468 for(i=0; i<ship.lives-1; i++) {
469 dest.x = (i + 1)*(surf_life->w + 10);
471 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
482 fadetimer += t_frame;
484 dest.x = (XSIZE-surf_b_game->w)/2;
485 dest.y = (YSIZE-surf_b_game->h)/2-40;
486 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
488 dest.x = (XSIZE-surf_b_over->w)/2;
489 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
490 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
492 if(new_high_score(score)) {
493 text0 = "New High Score!";
494 text1 = "Press SPACE to continue";
496 text0 = msgs[g_easy][0];
497 text1 = msgs[g_easy][1];
500 x = (XSIZE-font_width(text0))/2 + cos(fadetimer/9)*10;
501 font_write(x,YSIZE-100 + cos(fadetimer/6)*5,text0);
503 x = (XSIZE-font_width(text1))/2 + sin(fadetimer/9)*10;
504 font_write(x,YSIZE-50 + sin(fadetimer/4)*5,text1);
508 draw_title_page(void)
514 fadetimer += t_frame/2.0;
516 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
517 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
518 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
520 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
521 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
522 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
524 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
525 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
526 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
528 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
529 x = (XSIZE-font_width(text))/2 + cos(fadetimer/4.5)*10;
530 font_write(x,YSIZE-100 + cos(fadetimer/3)*5,text);
532 text = "Version " VERSION;
533 x = (XSIZE-font_width(text))/2 + sin(fadetimer/4.5)*10;
534 font_write(x,YSIZE-50 + sin(fadetimer/2)*5,text);
540 SDL_FillRect(surf_screen,NULL,0); // black background
541 draw_dots(surf_screen); // background dots
542 draw_sprite(SPRITE(&ship));
549 case GAME_OVER: draw_game_over(); break;
551 case TITLE_PAGE: draw_title_page(); break;
553 case HIGH_SCORE_ENTRY: play_tune(TUNE_HIGH_SCORE_ENTRY);
554 // and fall through to
555 case HIGH_SCORE_DISPLAY:
556 // Display de list o high scores mon.
557 display_scores(surf_screen, 150,50);
561 ; // no action necessary
564 // Update the surface
565 SDL_Flip(surf_screen);
569 kill_ship(struct ship *ship)
571 play_sound(SOUND_BANG);
572 new_bang_dots(SPRITE(ship));
575 state_timeout = DEAD_PAUSE_LENGTH;
576 // want ship to be invisible, but keep drifting at sqrt(speed)
577 // to leave it in the middle of the space from the explosion.
579 ship->dx = (ship->dx < 0) ? -sqrt(-ship->dx) : sqrt(ship->dx);
580 ship->dy = (ship->dy < 0) ? -sqrt(-ship->dy) : sqrt(ship->dy);
581 if(ship->dx < BARRIER_SPEED) ship->dx = BARRIER_SPEED;
584 state_timeout = 200.0;
587 // scrolling is based on the ship speed, so we need to reset it.
588 ship->dx = BARRIER_SPEED; ship->dy = 0;
593 do_collision(Sprite *a, Sprite *b)
595 if(a->type == SHIP) kill_ship((struct ship *)a);
596 else if(b->type == SHIP) kill_ship((struct ship *)b);
601 init_score_entry(void)
604 state = HIGH_SCORE_ENTRY;
605 state_timeout = 5.0e6;
606 SDL_EnableUNICODE(1);
607 while(SDL_PollEvent(&e))
612 // Count down the state timer, and change state when it gets to zero or less;
614 update_state(float ticks)
616 state_timeout -= ticks*3;
617 if(state_timeout > 0) return;
620 case GAMEPLAY: break; // no action necessary
622 // Restore the ship and continue playing
623 ship.flags = DRAW|MOVE|COLLIDE;
625 play_tune(TUNE_GAMEPLAY);
628 if(new_high_score(score)) init_score_entry();
630 state = HIGH_SCORE_DISPLAY;
634 case HIGH_SCORE_DISPLAY:
636 state_timeout = 600.0;
639 case HIGH_SCORE_ENTRY:
642 state = HIGH_SCORE_DISPLAY;
643 state_timeout = 200.0;
655 ms_frame = SDL_GetTicks() - ms_end;
657 if(ms_frame > 1000) {
660 t_frame = gamespeed * ms_frame / 50;
663 while(SDL_PollEvent(&e)) {
665 case SDL_QUIT: return;
667 if(state == HIGH_SCORE_ENTRY) {
668 if(!process_score_input(&e.key.keysym)) {
669 // Write the high score table to the file
670 write_high_score_table();
671 // continue to display the scores briefly
672 state = HIGH_SCORE_DISPLAY;
674 play_tune(TUNE_TITLE_PAGE);
676 } else if(e.key.keysym.sym == SDLK_q) {
680 if(e.key.keysym.sym == SDLK_ESCAPE) {
686 keystate = SDL_GetKeyState(NULL);
688 if(state == GAMEPLAY) {
692 if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
693 if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
694 if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
695 if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
696 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
699 if(keystate[SDLK_p] | keystate[SDLK_s]) {
703 if(!paused) ms_end = SDL_GetTicks();
711 update_state(t_frame);
714 tmp = (ship.y+ship.h/2 + ship.dy*t_frame - YSCROLLTO)/25 + (ship.dy-screendy);
715 screendy += tmp * t_frame/12;
716 tmp = (ship.x+ship.w/2 + ship.dx*t_frame - XSCROLLTO)/25 + (ship.dx-screendx);
717 screendx += tmp * t_frame/12;
718 // taper off so we don't hit the barrier abruptly.
719 // (if we would hit in < 2 seconds, adjust to 2 seconds).
720 if(dist_ahead + (screendx - BARRIER_SPEED)*TO_TICKS(2) < 0)
721 screendx = BARRIER_SPEED - (dist_ahead/TO_TICKS(2));
722 dist_ahead += (screendx - BARRIER_SPEED)*t_frame;
723 if(MAX_DIST_AHEAD >= 0) dist_ahead = min(dist_ahead, MAX_DIST_AHEAD);
725 move_sprites(t_frame); new_rocks(t_frame);
726 move_engine_dots(t_frame); new_engine_dots(t_frame);
727 move_bang_dots(t_frame);
732 // BOUNCE off left or right edge of screen
733 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
734 ship.x -= (ship.dx-screendx)*t_frame;
735 ship.dx = screendx - (ship.dx-screendx)*BOUNCINESS;
738 // BOUNCE off top or bottom of screen
739 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
740 ship.y -= (ship.dy-screendy)*t_frame;
741 ship.dy = screendy - (ship.dy-screendy)*BOUNCINESS;
747 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
748 && (state == HIGH_SCORE_DISPLAY
749 || state == TITLE_PAGE
750 || state == GAME_OVER)) {
751 if(state == GAME_OVER && new_high_score(score))
754 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
756 initial_rocks = NORMAL_I_ROCKS;
757 final_rocks = NORMAL_F_ROCKS;
758 if(gamespeed == EASY_GAMESPEED)
759 gamespeed = NORMAL_GAMESPEED;
760 } else if(keystate[SDLK_1]) {
762 initial_rocks = EASY_I_ROCKS;
763 final_rocks = EASY_F_ROCKS;
764 gamespeed = EASY_GAMESPEED;
768 screendx = BARRIER_SPEED; screendy = 0;
770 ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
771 ship.dx = screendx; ship.dy = screendy;
773 ship.flags = MOVE|DRAW|COLLIDE;
774 add_sprite(SPRITE(&ship));
779 play_tune(TUNE_GAMEPLAY);
786 if(state == TITLE_PAGE && keystate[SDLK_h]) {
787 state = HIGH_SCORE_DISPLAY;
794 main(int argc, char **argv) {
795 if(!parse_opts(argc, argv)) return 1;
798 printf ("vor: SDL error: '%s'\n",initerror);
802 start = SDL_GetTicks();
805 end = SDL_GetTicks();
806 // printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));