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>
36 #include "vorconfig.h"
46 #include "autopilot.h"
48 // ************************************* VARS
49 // SDL_Surface global variables
51 *surf_screen, // Screen
52 *surf_b_variations, // "variations" banner
53 *surf_b_on, // "on" banner
54 *surf_b_rockdodger, // "rockdodger" banner
55 *surf_b_game, // Title element "game"
56 *surf_b_over, // Title element "over"
57 *surf_life, // Indicator of number of ships remaining
58 *surf_rock[NROCKS], // THE ROCKS
59 *surf_font_big; // The big font
71 float mass; // in DOT_MASS_UNITs
72 float decay; // rate at which to reduce mass.
73 int heat; // heat multiplier (color).
74 uint8_t type; // BANGDOT or ENGINEDOT
79 struct dot edot[MAXENGINEDOTS], *dotptr = edot;
80 struct dot bdot[MAXBANGDOTS];
82 // Other global variables
85 int screenshot_number = 0;
87 struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, BARRIER_SPEED, 0.0 };
89 float screendx = BARRIER_SPEED, screendy = 0.0;
92 // all movement is based on t_frame.
93 unsigned long frames, start, end;
94 float t_frame; // length of this frame (in ticks = 1/20th second) adjusted for gamespeed
95 int ms_frame; // length of this frame (milliseconds)
96 int ms_end; // end of this frame (milliseconds)
98 float gamespeed = 1.00;
106 SDL_Joystick *joy = NULL;
108 // bangdot start (bd1) and end (bd2) position:
109 int bd1 = 0, bd2 = 0;
119 enum states state = TITLE_PAGE;
120 float state_timeout = 600.0;
125 "Press SPACE for normal game",
126 "Press '1' for easy game",
127 "http://jasonwoof.org/vor"
130 "Press SPACE for easy game",
131 "Press '2' for normal game",
132 "http://jasonwoof.org/vor"
136 int bangdotlife, nbangdots;
137 Uint16 heatcolor[W*3];
141 extern int optind, opterr, optopt;
143 #define TO_TICKS(seconds) ((seconds)*20*gamespeed)
145 // ************************************* FUNCS
147 #ifdef HAVE_NANOSLEEP
162 for(i = 0; i<MAXENGINEDOTS; i++) {
164 edot[i].type = ENGINEDOT;
166 for(i = 0; i<MAXBANGDOTS; i++) {
168 bdot[i].type = BANGDOT;
174 new_engine_dots(void) {
176 int n = t_frame * ENGINE_DOTS_PER_TIC;
177 float a, r; // angle, random length
179 float hx, hy; // half ship width/height.
180 static const int s[4] = { 2, 1, 0, 1 };
182 float accelh, accelv, past_ship_dx, past_ship_dy;
184 hx = ship.image->w / 2;
185 hy = ship.image->h / 2;
187 for(dir=0; dir<4; dir++) {
188 if(!(ship.jets & 1<<dir)) continue;
190 for(i = 0; i<n; i++) {
191 if(dotptr->active == 0) {
192 a = frnd()*M_PI + (dir-1)*M_PI_2;
193 r = sin(frnd()*M_PI);
195 dy = r * -sin(a); // screen y is "backwards".
200 // dot was created at a random time during the time span
201 time = frnd() * t_frame; // this is how long ago
203 // calculate how fast the ship was going when this engine dot was
204 // created (as if it had a smooth acceleration). This is used in
205 // determining the velocity of the dots, but not their starting
207 accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
208 accelh *= THRUSTER_STRENGTH * time;
209 past_ship_dx = ship.dx - accelh;
210 accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
211 accelv *= THRUSTER_STRENGTH * time;
212 past_ship_dy = ship.dy - accelv;
214 // the starting position (not speed) of the dot is calculated as
215 // though the ship were traveling at a constant speed for this
217 dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
218 dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
220 dotptr->dx = past_ship_dx + 2*dx;
221 dotptr->dy = past_ship_dy + 20*dy;
222 dotptr->mass = 60 * fabs(dy);
224 dotptr->dx = past_ship_dx + 20*dx;
225 dotptr->dy = past_ship_dy + 2*dy;
226 dotptr->mass = 60 * fabs(dx);
229 // move the dot as though it were created in the past
230 dotptr->x += (dotptr->dx - screendx) * time;
231 dotptr->y += (dotptr->dy - screendy) * time;
233 if(!fclip(dotptr->x, XSIZE) && !fclip(dotptr->y, YSIZE)) {
235 if(dotptr - edot < MAXENGINEDOTS-1) {
248 new_bang_dots(struct sprite *s)
255 SDL_Surface *img = s->image;
259 row_inc = img->pitch/sizeof(uint16_t) - img->w;
260 colorkey = img->format->colorkey;
262 if(SDL_MUSTLOCK(img)) { SDL_LockSurface(img); }
266 for(y=0; y<img->h; y++) {
267 for(x = 0; x<img->w; x++) {
269 if(c && c != colorkey) {
270 theta = frnd()*M_PI*2;
271 r = frnd(); r = 1 - r*r;
273 bdot[bd2].dx = 45*r*cos(theta) + s->dx;
274 bdot[bd2].dy = 45*r*sin(theta) + s->dy;
275 bdot[bd2].x = x + s->x;
276 bdot[bd2].y = y + s->y;
277 bdot[bd2].mass = frnd() * 99;
278 bdot[bd2].decay = frnd()*1.5 + 0.5;
280 bdot[bd2].active = 1;
282 bd2 = (bd2+1) % MAXBANGDOTS;
289 if(SDL_MUSTLOCK(img)) { SDL_UnlockSurface(img); }
293 kill_rock(struct rock *r) {
298 move_dot(struct dot *d)
304 d->x += (d->dx - screendx) * t_frame;
305 d->y += (d->dy - screendy) * t_frame;
306 d->mass -= t_frame * d->decay;
307 if(d->mass < 0 || fclip(d->x, XSIZE) || fclip(d->y, YSIZE))
310 hit = pixel_collides(d->x, d->y);
311 if(hit) if(hit->type != SHIP) {
313 mass = sprite_mass(hit);
314 if(d->type == BANGDOT) {
315 struct rock *rock = (struct rock*)hit;
316 rock->life -= (d->dx - hit->dx) * (d->dx - hit->dx) + (d->dy - hit->dy) * (d->dy - hit->dy);
321 hit->dx += DOT_MASS_UNIT * d->mass * (d->dx - hit->dx) / mass;
322 hit->dy += DOT_MASS_UNIT * d->mass * (d->dy - hit->dy) / mass;
333 for(i=0; i<MAXBANGDOTS; i++) move_dot(&bdot[i]);
334 for(i=0; i<MAXENGINEDOTS; i++) move_dot(&edot[i]);
339 draw_dot(struct dot *d)
341 uint16_t *pixels, *pixel;
345 pixels = (uint16_t *) surf_screen->pixels;
346 row_inc = surf_screen->pitch / sizeof(uint16_t);
347 pixel = pixels + (int)d->y * row_inc + (int)d->x;
348 *pixel = heatcolor[min(3*W-1, (int)(d->mass * d->heat))];
356 if(SDL_MUSTLOCK(surf_screen)) { SDL_LockSurface(surf_screen); }
358 for(i=0; i<MAXBANGDOTS; i++) draw_dot(&bdot[i]);
359 for(i=0; i<MAXENGINEDOTS; i++) draw_dot(&edot[i]);
360 if(SDL_MUSTLOCK(surf_screen)) { SDL_UnlockSurface(surf_screen); }
364 load_image(char *filename)
366 SDL_Surface *tmp, *img = NULL;
367 char *s = add_data_path(filename);
372 img = SDL_DisplayFormat(tmp);
373 SDL_FreeSurface(tmp);
382 load_sprite(SPRITE(&ship), "ship.png");
385 void font_cleanup() {
393 // Attempt to get the required video size
394 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
395 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
396 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
400 toggle_fullscreen() {
401 opt_fullscreen = 1 - opt_fullscreen;
415 // Where are our data files?
416 if(!find_files()) exit(1);
417 read_high_score_table();
420 // Initialize SDL with audio and video
421 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) != 0) {
423 fputs("Can't open sound, starting without it\n", stderr);
427 atexit(SDL_CloseAudio);
428 opt_sound = init_sound();
431 // Initialize with video only
432 CONDERROR(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0);
436 play_tune(TUNE_TITLE_PAGE);
439 // Attempt to get the required video size
442 // Set the title bar text
443 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
445 NULLERROR(surf_screen);
447 // Set the heat color from the range 0 (cold) to 300 (blue-white)
448 for(i = 0; i<W*3; i++) {
449 heatcolor[i] = SDL_MapRGB(
451 (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?
456 NULLERROR(surf_b_variations = load_image("b_variations.png"));
457 NULLERROR(surf_b_on = load_image("b_on.png"));
458 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
460 NULLERROR(surf_b_game = load_image("b_game.png"));
461 NULLERROR(surf_b_over = load_image("b_over.png"));
463 // Load the life indicator (small ship) graphic.
464 NULLERROR(surf_life = load_image("life.png"));
466 // Load the font image
467 s = add_data_path("font.png");
469 g_font = font_load(s);
470 atexit(&font_cleanup);
472 fprintf(stderr, "could create path to font\n");
476 if(SDL_NumJoysticks() > 0)
478 NULLERROR(joy = SDL_JoystickOpen(0));
485 add_sprite(SPRITE(&ship));
487 // Remove the mouse cursor
489 SDL_ShowCursor(SDL_DISABLE);
501 for(i=0; i<ship.lives-1; i++) {
502 dest.x = (i + 1)*(surf_life->w + 10);
504 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
515 fadetimer += t_frame;
517 dest.x = (XSIZE-surf_b_game->w)/2;
518 dest.y = (YSIZE-surf_b_game->h)/2-40;
519 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
521 dest.x = (XSIZE-surf_b_over->w)/2;
522 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
523 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
525 if(new_high_score(score)) {
526 text0 = "New High Score!";
527 text1 = "Press SPACE to continue";
529 text0 = msgs[g_easy][0];
530 text1 = msgs[g_easy][1];
533 x = (XSIZE-font_width(text0))/2 + cos(fadetimer/9)*10;
534 font_write(x,YSIZE-100 + cos(fadetimer/6)*5,text0);
536 x = (XSIZE-font_width(text1))/2 + sin(fadetimer/9)*10;
537 font_write(x,YSIZE-50 + sin(fadetimer/4)*5,text1);
541 draw_title_page(void)
547 fadetimer += t_frame/2.0;
549 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
550 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
551 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
553 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
554 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
555 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
557 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
558 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
559 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
561 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
562 x = (XSIZE-font_width(text))/2 + cos(fadetimer/4.5)*10;
563 font_write(x,YSIZE-100 + cos(fadetimer/3)*5,text);
565 text = "Version " PACKAGE_VERSION;
566 x = (XSIZE-font_width(text))/2 + sin(fadetimer/4.5)*10;
567 font_write(x,YSIZE-50 + sin(fadetimer/2)*5,text);
573 SDL_FillRect(surf_screen,NULL,0); // black background
574 draw_dots(); // background dots
575 draw_sprite(SPRITE(&ship));
590 case HIGH_SCORE_ENTRY:
591 case HIGH_SCORE_DISPLAY:
592 display_scores(150,50);
597 ; // no action necessary
600 // Update the surface
601 SDL_Flip(surf_screen);
605 kill_ship(struct ship *ship)
607 play_sound(SOUND_BANG);
608 new_bang_dots(SPRITE(ship));
611 state_timeout = DEAD_PAUSE_LENGTH;
612 // want ship to be invisible, but keep drifting at sqrt(speed)
613 // to leave it in the middle of the space from the explosion.
615 ship->dx = (ship->dx < 0) ? -sqrt(-ship->dx) : sqrt(ship->dx);
616 ship->dy = (ship->dy < 0) ? -sqrt(-ship->dy) : sqrt(ship->dy);
617 if(ship->dx < BARRIER_SPEED) ship->dx = BARRIER_SPEED;
620 play_tune(TUNE_TITLE_PAGE);
621 state_timeout = 200.0;
624 // scrolling is based on the ship speed, so we need to reset it.
625 ship->dx = BARRIER_SPEED; ship->dy = 0;
630 do_collision(Sprite *a, Sprite *b)
632 if(a->type == SHIP) kill_ship((struct ship *)a);
633 else if(b->type == SHIP) kill_ship((struct ship *)b);
638 init_score_entry(void)
641 state = HIGH_SCORE_ENTRY;
642 state_timeout = 5.0e6;
643 SDL_EnableUNICODE(1);
644 while(SDL_PollEvent(&e))
649 // Count down the state timer, and change state when it gets to zero or less;
653 state_timeout -= t_frame*3;
654 if(state_timeout > 0) return;
657 case GAMEPLAY: break; // no action necessary
659 // Restore the ship and continue playing
660 ship.flags = DRAW|MOVE|COLLIDE;
664 if(new_high_score(score)) init_score_entry();
666 state = HIGH_SCORE_DISPLAY;
670 case HIGH_SCORE_DISPLAY:
672 state_timeout = 600.0;
675 case HIGH_SCORE_ENTRY:
678 state = HIGH_SCORE_DISPLAY;
679 state_timeout = 200.0;
688 Sint16 x_move, y_move;
694 ms_frame = SDL_GetTicks() - ms_end;
699 t_frame = gamespeed * ms_frame / 50;
706 while(paused ? SDL_WaitEvent(&e) : SDL_PollEvent(&e)) {
708 case SDL_QUIT: return;
710 // even during high-score entry
711 if(e.key.keysym.sym == SDLK_ESCAPE) {
715 if(state == HIGH_SCORE_ENTRY) {
716 if(!process_score_input(&e.key.keysym)) {
717 // Write the high score table to the file
718 write_high_score_table();
719 // continue to display the scores briefly
720 state = HIGH_SCORE_DISPLAY;
724 switch(e.key.keysym.sym) {
732 char *screenshot_filename = &(tmp[0]);
734 snprintf(screenshot_filename, 30, "vor-screenshot-%02i.bmp", screenshot_number++);
735 screenshot_fp = fopen(screenshot_filename, "r");
737 fclose(screenshot_fp);
742 SDL_SaveBMP(surf_screen, screenshot_filename);
746 if(state != GAMEPLAY && state != DEAD_PAUSE) {
747 // don't conflict with space key to start a new game
758 ms_end = SDL_GetTicks();
766 // other keys are handled by checking keystate each frame
773 keystate = SDL_GetKeyState(NULL);
774 SDL_JoystickUpdate();
775 x_move = SDL_JoystickGetAxis(joy, 4);
776 y_move = SDL_JoystickGetAxis(joy, 5);
778 for(i = 1; i <= SDL_JoystickNumButtons(joy); i++)
780 if(SDL_JoystickGetButton(joy, i) == 1)
788 autopilot_fix_keystates(keystate);
791 if(state == GAMEPLAY) {
795 if(keystate[SDLK_LEFT] || keystate[SDLK_KP4]) {
796 ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;
798 if(keystate[SDLK_DOWN] || keystate[SDLK_KP5] || keystate[SDLK_KP2]) {
799 ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;
801 if(keystate[SDLK_RIGHT] || keystate[SDLK_KP6]) {
802 ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;
804 if(keystate[SDLK_UP] || keystate[SDLK_KP8]) {
805 ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;
807 if(x_move < -3000) { ship.dx += x_move*THRUSTER_STRENGTH*t_frame/32768; ship.jets |= 1<<0;}
808 if(y_move > 3000) { ship.dy += y_move*THRUSTER_STRENGTH*t_frame/32768; ship.jets |= 1<<1;}
809 if(x_move > 3000) { ship.dx += x_move*THRUSTER_STRENGTH*t_frame/32768; ship.jets |= 1<<2;}
810 if(y_move < -3000) { ship.dy += y_move*THRUSTER_STRENGTH*t_frame/32768; ship.jets |= 1<<3;}
812 ship.dx = fconstrain2(ship.dx, -50, 50);
813 ship.dy = fconstrain2(ship.dy, -50, 50);
823 tmp = (ship.y+ship.h/2 + ship.dy*t_frame - YSCROLLTO)/25 + (ship.dy-screendy);
824 screendy += tmp * t_frame/12;
825 tmp = (ship.x+ship.w/2 + ship.dx*t_frame - XSCROLLTO)/25 + (ship.dx-screendx);
826 screendx += tmp * t_frame/12;
827 // taper off so we don't hit the barrier abruptly.
828 // (if we would hit in < 2 seconds, adjust to 2 seconds).
829 if(dist_ahead + (screendx - BARRIER_SPEED)*TO_TICKS(2) < 0)
830 screendx = BARRIER_SPEED - (dist_ahead/TO_TICKS(2));
831 dist_ahead += (screendx - BARRIER_SPEED)*t_frame;
832 if(MAX_DIST_AHEAD >= 0) dist_ahead = min(dist_ahead, MAX_DIST_AHEAD);
840 // BOUNCE off left or right edge of screen
841 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
842 ship.x -= (ship.dx-screendx)*t_frame;
843 ship.dx = screendx - (ship.dx-screendx)*BOUNCINESS;
844 ship.x = fconstrain(ship.x, XSIZE - ship.w);
847 // BOUNCE off top or bottom of screen
848 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
849 ship.y -= (ship.dy-screendy)*t_frame;
850 ship.dy = screendy - (ship.dy-screendy)*BOUNCINESS;
851 ship.y = fconstrain(ship.y, YSIZE - ship.h);
856 collisions(); // must happen after ship bouncing because it puts pixels where the ship is (thus the ship must be on the screen)
862 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2] || button_pressed)
863 && (state == HIGH_SCORE_DISPLAY
864 || state == TITLE_PAGE
865 || state == GAME_OVER)) {
866 if(state == GAME_OVER && new_high_score(score))
869 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2] || button_pressed) {
871 initial_rocks = NORMAL_I_ROCKS;
872 final_rocks = NORMAL_F_ROCKS;
873 if(gamespeed == EASY_GAMESPEED)
874 gamespeed = NORMAL_GAMESPEED;
875 } else if(keystate[SDLK_1]) {
877 initial_rocks = EASY_I_ROCKS;
878 final_rocks = EASY_F_ROCKS;
879 gamespeed = EASY_GAMESPEED;
883 screendx = BARRIER_SPEED; screendy = 0;
885 ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
886 ship.dx = screendx; ship.dy = screendy;
888 ship.flags = MOVE|DRAW|COLLIDE;
889 add_sprite(SPRITE(&ship));
894 play_tune(TUNE_GAMEPLAY);
901 if(state == TITLE_PAGE && keystate[SDLK_h]) {
902 state = HIGH_SCORE_DISPLAY;
911 main(int argc, char **argv) {
912 if(!parse_opts(argc, argv)) return 1;
915 printf ("vor: SDL error: '%s'\n",initerror);
919 start = SDL_GetTicks();
922 end = SDL_GetTicks();
923 // printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));