1 /* Variations on RockDodger
2 * Space Rocks copyright (C) 2001 Paul Holt <pad@pcholt.com>
3 *
4 * Project fork 2004, Jason Woofenden and Joshua Grams.
5 * (a whole bunch of modifications and project rename)
6
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.
11 *
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.
16 *
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
20 */
21
22 #include <math.h>
23 #include <SDL.h>
24 #include <SDL_image.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "font.h"
32
33 #include "args.h"
34 #include "common.h"
35 #include <config.h>
36 #include "vorconfig.h"
37 #include "dust.h"
38 #include "file.h"
39 #include "float.h"
40 #include "globals.h"
41 #include "mt.h"
42 #include "rocks.h"
43 #include "score.h"
44 #include "sprite.h"
45 #include "sound.h"
46 #include "autopilot.h"
47
48 // ************************************* VARS
49 // SDL_Surface global variables
50 SDL_Surface
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
60
61
62 font *g_font;
63
64 #define ENGINEDOT 0
65 #define BANGDOT 1
66
67 struct dot {
68 int active;
69 float x, y;
70 float dx, dy;
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
75 };
76
77 void draw(void);
78
79 struct dot edot[MAXENGINEDOTS], *dotptr = edot;
80 struct dot bdot[MAXBANGDOTS];
81
82 // Other global variables
83 char topline[1024];
84 char *initerror = "";
85 int screenshot_number = 0;
86
87 struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, BARRIER_SPEED, 0.0 };
88
89 float screendx = BARRIER_SPEED, screendy = 0.0;
90 float dist_ahead;
91
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)
97
98 float gamespeed = 1.00;
99
100 int score;
101
102 float fadetimer = 0;
103
104 int paused = 0;
105
106 // bangdot start (bd1) and end (bd2) position:
107 int bd1 = 0, bd2 = 0;
108
109 enum states {
110 TITLE_PAGE,
111 GAMEPLAY,
112 DEAD_PAUSE,
113 GAME_OVER,
114 HIGH_SCORE_ENTRY,
115 HIGH_SCORE_DISPLAY
116 };
117 enum states state = TITLE_PAGE;
118 float state_timeout = 600.0;
119
120 #define NSEQUENCE 3
121 char *msgs[2][3] = {
122 {
123 "Press SPACE for normal game",
124 "Press '1' for easy game",
125 "https://sametwice.com/vor"
126 },
127 {
128 "Press SPACE for easy game",
129 "Press '2' for normal game",
130 "https://sametwice.com/vor"
131 }
132 };
133
134 int bangdotlife, nbangdots;
135 Uint16 heatcolor[W*3];
136
137 char *data_dir;
138 extern char *optarg;
139 extern int optind, opterr, optopt;
140
141 #define TO_TICKS(seconds) ((seconds)*20*gamespeed)
142
143 // ************************************* FUNCS
144
145 #ifdef HAVE_NANOSLEEP
146 void
tiny_sleep()147 tiny_sleep() {
148 struct timespec t;
149 t.tv_sec = 0;
150 t.tv_nsec = 1;
151 nanosleep(&t, 0);
152 }
153 #else
154 #define tiny_sleep()
155 #endif
156
157 void
init_dots()158 init_dots() {
159 int i;
160 for(i = 0; i<MAXENGINEDOTS; i++) {
161 edot[i].active = 0;
162 edot[i].type = ENGINEDOT;
163 }
164 for(i = 0; i<MAXBANGDOTS; i++) {
165 bdot[i].active = 0;
166 bdot[i].type = BANGDOT;
167 }
168 }
169
170
171 void
new_engine_dots(void)172 new_engine_dots(void) {
173 int dir, i;
174 int n = t_frame * ENGINE_DOTS_PER_TIC;
175 float a, r; // angle, random length
176 float dx, dy;
177 float hx, hy; // half ship width/height.
178 static const int s[4] = { 2, 1, 0, 1 };
179 float time;
180 float accelh, accelv, past_ship_dx, past_ship_dy;
181
182 hx = ship.image->w / 2;
183 hy = ship.image->h / 2;
184
185 for(dir=0; dir<4; dir++) {
186 if(!(ship.jets & 1<<dir)) continue;
187
188 for(i = 0; i<n; i++) {
189 if(dotptr->active == 0) {
190 a = frnd()*M_PI + (dir-1)*M_PI_2;
191 r = sin(frnd()*M_PI);
192 dx = r * cos(a);
193 dy = r * -sin(a); // screen y is "backwards".
194
195 dotptr->decay = 3;
196 dotptr->heat = 6;
197
198 // dot was created at a random time during the time span
199 time = frnd() * t_frame; // this is how long ago
200
201 // calculate how fast the ship was going when this engine dot was
202 // created (as if it had a smooth acceleration). This is used in
203 // determining the velocity of the dots, but not their starting
204 // location.
205 accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
206 accelh *= THRUSTER_STRENGTH * time;
207 past_ship_dx = ship.dx - accelh;
208 accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
209 accelv *= THRUSTER_STRENGTH * time;
210 past_ship_dy = ship.dy - accelv;
211
212 // the starting position (not speed) of the dot is calculated as
213 // though the ship were traveling at a constant speed for this
214 // t_frame.
215 dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
216 dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
217 if(dir&1) {
218 dotptr->dx = past_ship_dx + 2*dx;
219 dotptr->dy = past_ship_dy + 20*dy;
220 dotptr->mass = 60 * fabs(dy);
221 } else {
222 dotptr->dx = past_ship_dx + 20*dx;
223 dotptr->dy = past_ship_dy + 2*dy;
224 dotptr->mass = 60 * fabs(dx);
225 }
226
227 // move the dot as though it were created in the past
228 dotptr->x += (dotptr->dx - screendx) * time;
229 dotptr->y += (dotptr->dy - screendy) * time;
230
231 if(!fclip(dotptr->x, XSIZE) && !fclip(dotptr->y, YSIZE)) {
232 dotptr->active = 1;
233 if(dotptr - edot < MAXENGINEDOTS-1) {
234 dotptr++;
235 } else {
236 dotptr = edot;
237 }
238 }
239 }
240 }
241 }
242 }
243
244
245 void
new_bang_dots(struct sprite * s)246 new_bang_dots(struct sprite *s)
247 {
248 int i, n, x, y;
249 uint16_t *pixel, c;
250 uint32_t colorkey;
251 int row_inc;
252 double theta, r;
253 SDL_Surface *img = s->image;
254
255 n = 20;
256 pixel = img->pixels;
257 row_inc = img->pitch/sizeof(uint16_t) - img->w;
258 colorkey = img->format->colorkey;
259
260 if(SDL_MUSTLOCK(img)) { SDL_LockSurface(img); }
261
262 for(i=0; i<n; i++) {
263 pixel = img->pixels;
264 for(y=0; y<img->h; y++) {
265 for(x = 0; x<img->w; x++) {
266 c = *pixel++;
267 if(c && c != colorkey) {
268 theta = frnd()*M_PI*2;
269 r = frnd(); r = 1 - r*r;
270
271 bdot[bd2].dx = 45*r*cos(theta) + s->dx;
272 bdot[bd2].dy = 45*r*sin(theta) + s->dy;
273 bdot[bd2].x = x + s->x;
274 bdot[bd2].y = y + s->y;
275 bdot[bd2].mass = frnd() * 99;
276 bdot[bd2].decay = frnd()*1.5 + 0.5;
277 bdot[bd2].heat = 3;
278 bdot[bd2].active = 1;
279
280 bd2 = (bd2+1) % MAXBANGDOTS;
281 }
282 pixel += row_inc;
283 }
284 }
285 }
286
287 if(SDL_MUSTLOCK(img)) { SDL_UnlockSurface(img); }
288 }
289
290 void
kill_rock(struct rock * r)291 kill_rock(struct rock *r) {
292 r->x = -200;
293 }
294
295 void
move_dot(struct dot * d)296 move_dot(struct dot *d)
297 {
298 Sprite *hit;
299 float mass;
300
301 if(d->active) {
302 d->x += (d->dx - screendx) * t_frame;
303 d->y += (d->dy - screendy) * t_frame;
304 d->mass -= t_frame * d->decay;
305 if(d->mass < 0 || fclip(d->x, XSIZE) || fclip(d->y, YSIZE))
306 d->active = 0;
307 else {
308 hit = pixel_collides(d->x, d->y);
309 if(hit) if(hit->type != SHIP) {
310 d->active = 0;
311 mass = sprite_mass(hit);
312 if(d->type == BANGDOT) {
313 struct rock *rock = (struct rock*)hit;
314 rock->life -= (d->dx - hit->dx) * (d->dx - hit->dx) + (d->dy - hit->dy) * (d->dy - hit->dy);
315 if(rock->life < 0) {
316 kill_rock(rock);
317 }
318 }
319 hit->dx += DOT_MASS_UNIT * d->mass * (d->dx - hit->dx) / mass;
320 hit->dy += DOT_MASS_UNIT * d->mass * (d->dy - hit->dy) / mass;
321 }
322 }
323 }
324 }
325
326 void
move_dots(void)327 move_dots(void)
328 {
329 int i;
330
331 for(i=0; i<MAXBANGDOTS; i++) move_dot(&bdot[i]);
332 for(i=0; i<MAXENGINEDOTS; i++) move_dot(&edot[i]);
333 }
334
335
336 void
draw_dot(struct dot * d)337 draw_dot(struct dot *d)
338 {
339 uint16_t *pixels, *pixel;
340 int row_inc;
341
342 if(d->active) {
343 pixels = (uint16_t *) surf_screen->pixels;
344 row_inc = surf_screen->pitch / sizeof(uint16_t);
345 pixel = pixels + (int)d->y * row_inc + (int)d->x;
346 *pixel = heatcolor[min(3*W-1, (int)(d->mass * d->heat))];
347 }
348 }
349
350 void
draw_dots(void)351 draw_dots(void) {
352 int i;
353
354 if(SDL_MUSTLOCK(surf_screen)) { SDL_LockSurface(surf_screen); }
355 draw_dust();
356 for(i=0; i<MAXBANGDOTS; i++) draw_dot(&bdot[i]);
357 for(i=0; i<MAXENGINEDOTS; i++) draw_dot(&edot[i]);
358 if(SDL_MUSTLOCK(surf_screen)) { SDL_UnlockSurface(surf_screen); }
359 }
360
361 SDL_Surface *
load_image(char * filename)362 load_image(char *filename)
363 {
364 SDL_Surface *tmp, *img = NULL;
365 char *s = add_data_path(filename);
366 if(s) {
367 tmp = IMG_Load(s);
368 free(s);
369 if(tmp) {
370 img = SDL_DisplayFormat(tmp);
371 SDL_FreeSurface(tmp);
372 }
373 }
374 return img;
375 }
376
377 void
load_ship(void)378 load_ship(void)
379 {
380 load_sprite(SPRITE(&ship), "ship.png");
381 }
382
font_cleanup()383 void font_cleanup() {
384 font_free(g_font);
385 }
386
387 void
set_video_mode()388 set_video_mode() {
389 Uint32 flag;
390
391 // Attempt to get the required video size
392 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
393 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
394 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
395 }
396
397 void
toggle_fullscreen()398 toggle_fullscreen() {
399 opt_fullscreen = 1 - opt_fullscreen;
400 set_video_mode();
401 if(paused) {
402 draw();
403 }
404 }
405
406
407 int
init(void)408 init(void) {
409
410 int i;
411 char *s;
412
413 // Where are our data files?
414 if(!find_files()) exit(1);
415 read_high_score_table();
416
417 if(opt_sound) {
418 // Initialize SDL with audio and video
419 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
420 opt_sound = 0;
421 fputs("Can't open sound, starting without it\n", stderr);
422 atexit(SDL_Quit);
423 } else {
424 atexit(SDL_Quit);
425 atexit(SDL_CloseAudio);
426 opt_sound = init_sound();
427 }
428 } else {
429 // Initialize with video only
430 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
431 atexit(SDL_Quit);
432 }
433
434 play_tune(TUNE_TITLE_PAGE);
435
436
437 // Attempt to get the required video size
438 set_video_mode();
439
440 // Set the title bar text
441 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
442
443 NULLERROR(surf_screen);
444
445 // Set the heat color from the range 0 (cold) to 300 (blue-white)
446 for(i = 0; i<W*3; i++) {
447 heatcolor[i] = SDL_MapRGB(
448 surf_screen->format,
449 (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?
450 );
451 }
452
453 // Load the banners
454 NULLERROR(surf_b_variations = load_image("b_variations.png"));
455 NULLERROR(surf_b_on = load_image("b_on.png"));
456 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
457
458 NULLERROR(surf_b_game = load_image("b_game.png"));
459 NULLERROR(surf_b_over = load_image("b_over.png"));
460
461 // Load the life indicator (small ship) graphic.
462 NULLERROR(surf_life = load_image("life.png"));
463
464 // Load the font image
465 s = add_data_path("font.png");
466 if(s) {
467 g_font = font_load(s);
468 atexit(&font_cleanup);
469 } else {
470 fprintf(stderr, "could create path to font\n");
471 exit(1);
472 }
473
474 init_dots();
475 init_dust();
476
477 init_sprites();
478 add_sprite(SPRITE(&ship));
479
480 // Remove the mouse cursor
481 #ifdef SDL_DISABLE
482 SDL_ShowCursor(SDL_DISABLE);
483 #endif
484
485 return 0;
486 }
487
488 void
show_lives(void)489 show_lives(void)
490 {
491 int i;
492 SDL_Rect dest;
493
494 for(i=0; i<ship.lives-1; i++) {
495 dest.x = (i + 1)*(surf_life->w + 10);
496 dest.y = 20;
497 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
498 }
499 }
500
501 void
draw_game_over(void)502 draw_game_over(void)
503 {
504 int x;
505 char *text0, *text1;
506 SDL_Rect dest;
507
508 fadetimer += t_frame;
509
510 dest.x = (XSIZE-surf_b_game->w)/2;
511 dest.y = (YSIZE-surf_b_game->h)/2-40;
512 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
513
514 dest.x = (XSIZE-surf_b_over->w)/2;
515 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
516 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
517
518 if(new_high_score(score)) {
519 text0 = "New High Score!";
520 text1 = "Press SPACE to continue";
521 } else {
522 text0 = msgs[g_easy][0];
523 text1 = msgs[g_easy][1];
524 }
525
526 x = (XSIZE-font_width(text0))/2 + cos(fadetimer/9)*10;
527 font_write(x,YSIZE-100 + cos(fadetimer/6)*5,text0);
528
529 x = (XSIZE-font_width(text1))/2 + sin(fadetimer/9)*10;
530 font_write(x,YSIZE-50 + sin(fadetimer/4)*5,text1);
531 }
532
533 void
draw_title_page(void)534 draw_title_page(void)
535 {
536 int x;
537 char *text;
538 SDL_Rect dest;
539
540 fadetimer += t_frame/2.0;
541
542 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
543 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
544 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
545
546 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
547 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
548 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
549
550 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
551 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
552 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
553
554 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
555 x = (XSIZE-font_width(text))/2 + cos(fadetimer/4.5)*10;
556 font_write(x,YSIZE-100 + cos(fadetimer/3)*5,text);
557
558 text = "Version " PACKAGE_VERSION;
559 x = (XSIZE-font_width(text))/2 + sin(fadetimer/4.5)*10;
560 font_write(x,YSIZE-50 + sin(fadetimer/2)*5,text);
561 }
562
563 void
draw(void)564 draw(void)
565 {
566 SDL_FillRect(surf_screen,NULL,0); // black background
567 draw_dots(); // background dots
568 draw_sprite(SPRITE(&ship));
569 draw_rocks();
570
571 show_lives();
572 show_score();
573
574 switch (state) {
575 case GAME_OVER:
576 draw_game_over();
577 break;
578
579 case TITLE_PAGE:
580 draw_title_page();
581 break;
582
583 case HIGH_SCORE_ENTRY:
584 case HIGH_SCORE_DISPLAY:
585 display_scores(150,50);
586 break;
587
588 case GAMEPLAY:
589 case DEAD_PAUSE:
590 ; // no action necessary
591 }
592
593 // Update the surface
594 SDL_Flip(surf_screen);
595 }
596
597 static inline void
kill_ship(struct ship * ship)598 kill_ship(struct ship *ship)
599 {
600 play_sound(SOUND_BANG);
601 new_bang_dots(SPRITE(ship));
602 if(--ship->lives) {
603 state = DEAD_PAUSE;
604 state_timeout = DEAD_PAUSE_LENGTH;
605 // want ship to be invisible, but keep drifting at sqrt(speed)
606 // to leave it in the middle of the space from the explosion.
607 ship->flags = MOVE;
608 ship->dx = (ship->dx < 0) ? -sqrt(-ship->dx) : sqrt(ship->dx);
609 ship->dy = (ship->dy < 0) ? -sqrt(-ship->dy) : sqrt(ship->dy);
610 if(ship->dx < BARRIER_SPEED) ship->dx = BARRIER_SPEED;
611 } else {
612 state = GAME_OVER;
613 play_tune(TUNE_TITLE_PAGE);
614 state_timeout = 200.0;
615 fadetimer = 0.0;
616 ship->flags = 0;
617 // scrolling is based on the ship speed, so we need to reset it.
618 ship->dx = BARRIER_SPEED; ship->dy = 0;
619 }
620 }
621
622 void
do_collision(Sprite * a,Sprite * b)623 do_collision(Sprite *a, Sprite *b)
624 {
625 if(a->type == SHIP) kill_ship((struct ship *)a);
626 else if(b->type == SHIP) kill_ship((struct ship *)b);
627 else bounce(a, b);
628 }
629
630 void
init_score_entry(void)631 init_score_entry(void)
632 {
633 SDL_Event e;
634 state = HIGH_SCORE_ENTRY;
635 state_timeout = 5.0e6;
636 SDL_EnableUNICODE(1);
637 while(SDL_PollEvent(&e))
638 ;
639 insert_score(score);
640 }
641
642 // Count down the state timer, and change state when it gets to zero or less;
643 void
update_state(void)644 update_state(void)
645 {
646 state_timeout -= t_frame*3;
647 if(state_timeout > 0) return;
648
649 switch(state) {
650 case GAMEPLAY: break; // no action necessary
651 case DEAD_PAUSE:
652 // Restore the ship and continue playing
653 ship.flags = DRAW|MOVE|COLLIDE;
654 state = GAMEPLAY;
655 break;
656 case GAME_OVER:
657 if(new_high_score(score)) init_score_entry();
658 else {
659 state = HIGH_SCORE_DISPLAY;
660 state_timeout = 400;
661 }
662 break;
663 case HIGH_SCORE_DISPLAY:
664 state = TITLE_PAGE;
665 state_timeout = 600.0;
666 fadetimer = 0.0;
667 break;
668 case HIGH_SCORE_ENTRY:
669 break;
670 case TITLE_PAGE:
671 state = HIGH_SCORE_DISPLAY;
672 state_timeout = 200.0;
673 break;
674 }
675 }
676
677 void
gameloop()678 gameloop() {
679 SDL_Event e;
680 Uint8 *keystate;
681 float tmp;
682
683 for(;;) {
684 ms_frame = SDL_GetTicks() - ms_end;
685 ms_end += ms_frame;
686 if(ms_frame > 50) {
687 ms_frame = 50;
688 }
689 t_frame = gamespeed * ms_frame / 50;
690 frames++;
691
692 if(opt_autopilot) {
693 autopilot(t_frame);
694 }
695
696 while(paused ? SDL_WaitEvent(&e) : SDL_PollEvent(&e)) {
697 switch(e.type) {
698 case SDL_QUIT: return;
699 case SDL_KEYDOWN:
700 // even during high-score entry
701 if(e.key.keysym.sym == SDLK_ESCAPE) {
702 return;
703 }
704
705 if(state == HIGH_SCORE_ENTRY) {
706 if(!process_score_input(&e.key.keysym)) {
707 // Write the high score table to the file
708 write_high_score_table();
709 // continue to display the scores briefly
710 state = HIGH_SCORE_DISPLAY;
711 state_timeout = 200;
712 }
713 } else {
714 switch(e.key.keysym.sym) {
715 case SDLK_q:
716 return;
717 case SDLK_3:
718 case SDLK_PRINT:
719 {
720 FILE *screenshot_fp;
721 char tmp[30];
722 char *screenshot_filename = &(tmp[0]);
723 for(;;) {
724 snprintf(screenshot_filename, 30, "vor-screenshot-%02i.bmp", screenshot_number++);
725 screenshot_fp = fopen(screenshot_filename, "r");
726 if(screenshot_fp) {
727 fclose(screenshot_fp);
728 } else {
729 break;
730 }
731 }
732 SDL_SaveBMP(surf_screen, screenshot_filename);
733 }
734 break;
735 case SDLK_SPACE:
736 if(state != GAMEPLAY && state != DEAD_PAUSE) {
737 // don't conflict with space key to start a new game
738 break;
739 }
740 // else fall through
741 case SDLK_p:
742 case SDLK_PAUSE:
743 paused = !paused;
744 if(paused) {
745 pause_tune();
746 } else {
747 resume_tune();
748 ms_end = SDL_GetTicks();
749 }
750 break;
751 case SDLK_f:
752 case SDLK_F11:
753 toggle_fullscreen();
754 break;
755 default:
756 // other keys are handled by checking keystate each frame
757 break;
758 }
759 }
760 break;
761 }
762 }
763 keystate = SDL_GetKeyState(NULL);
764 if(opt_autopilot) {
765 autopilot_fix_keystates(keystate);
766 }
767
768 if(state == GAMEPLAY) {
769 if(!paused) {
770 score += ms_frame;
771
772 if(keystate[SDLK_LEFT] || keystate[SDLK_KP4]) {
773 ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;
774 }
775 if(keystate[SDLK_DOWN] || keystate[SDLK_KP5] || keystate[SDLK_KP2]) {
776 ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;
777 }
778 if(keystate[SDLK_RIGHT] || keystate[SDLK_KP6]) {
779 ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;
780 }
781 if(keystate[SDLK_UP] || keystate[SDLK_KP8]) {
782 ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;
783 }
784 if(ship.jets) {
785 ship.dx = fconstrain2(ship.dx, -50, 50);
786 ship.dy = fconstrain2(ship.dy, -50, 50);
787 }
788 }
789
790 }
791
792 if(!paused) {
793 update_state();
794
795 // SCROLLING
796 tmp = (ship.y+ship.h/2 + ship.dy*t_frame - YSCROLLTO)/25 + (ship.dy-screendy);
797 screendy += tmp * t_frame/12;
798 tmp = (ship.x+ship.w/2 + ship.dx*t_frame - XSCROLLTO)/25 + (ship.dx-screendx);
799 screendx += tmp * t_frame/12;
800 // taper off so we don't hit the barrier abruptly.
801 // (if we would hit in < 2 seconds, adjust to 2 seconds).
802 if(dist_ahead + (screendx - BARRIER_SPEED)*TO_TICKS(2) < 0)
803 screendx = BARRIER_SPEED - (dist_ahead/TO_TICKS(2));
804 dist_ahead += (screendx - BARRIER_SPEED)*t_frame;
805 if(MAX_DIST_AHEAD >= 0) dist_ahead = min(dist_ahead, MAX_DIST_AHEAD);
806
807 move_sprites();
808 move_dots();
809 move_dust();
810
811 new_rocks();
812
813 // BOUNCE off left or right edge of screen
814 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
815 ship.x -= (ship.dx-screendx)*t_frame;
816 ship.dx = screendx - (ship.dx-screendx)*BOUNCINESS;
817 ship.x = fconstrain(ship.x, XSIZE - ship.w);
818 }
819
820 // BOUNCE off top or bottom of screen
821 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
822 ship.y -= (ship.dy-screendy)*t_frame;
823 ship.dy = screendy - (ship.dy-screendy)*BOUNCINESS;
824 ship.y = fconstrain(ship.y, YSIZE - ship.h);
825 }
826
827 new_engine_dots();
828
829 collisions(); // must happen after ship bouncing because it puts pixels where the ship is (thus the ship must be on the screen)
830
831
832 draw();
833
834 // new game
835 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
836 && (state == HIGH_SCORE_DISPLAY
837 || state == TITLE_PAGE
838 || state == GAME_OVER)) {
839 if(state == GAME_OVER && new_high_score(score))
840 init_score_entry();
841 else {
842 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
843 g_easy = 0;
844 initial_rocks = NORMAL_I_ROCKS;
845 final_rocks = NORMAL_F_ROCKS;
846 if(gamespeed == EASY_GAMESPEED)
847 gamespeed = NORMAL_GAMESPEED;
848 } else if(keystate[SDLK_1]) {
849 g_easy = 1;
850 initial_rocks = EASY_I_ROCKS;
851 final_rocks = EASY_F_ROCKS;
852 gamespeed = EASY_GAMESPEED;
853 }
854 reset_sprites();
855 reset_rocks();
856 screendx = BARRIER_SPEED; screendy = 0;
857
858 ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
859 ship.dx = screendx; ship.dy = screendy;
860 ship.lives = 4;
861 ship.flags = MOVE|DRAW|COLLIDE;
862 add_sprite(SPRITE(&ship));
863
864 score = 0;
865
866 state = GAMEPLAY;
867 play_tune(TUNE_GAMEPLAY);
868 }
869 }
870
871 ship.jets = 0;
872 }
873
874 if(state == TITLE_PAGE && keystate[SDLK_h]) {
875 state = HIGH_SCORE_DISPLAY;
876 state_timeout = 400;
877 }
878
879 tiny_sleep();
880 }
881 }
882
883 int
main(int argc,char ** argv)884 main(int argc, char **argv) {
885 if(!parse_opts(argc, argv)) return 1;
886
887 if(init()) {
888 printf ("vor: SDL error: '%s'\n",initerror);
889 return 1;
890 }
891
892 start = SDL_GetTicks();
893 frames = 0;
894 gameloop();
895 end = SDL_GetTicks();
896 // printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));
897
898 return 0;
899 }
900