float back_dist;
// all movement is based on t_frame.
+unsigned long frames, start, end;
float t_frame; // length of this frame (in ticks = 1/20th second) adjusted for gamespeed
int ms_frame; // length of this frame (milliseconds)
int ms_end; // end of this frame (milliseconds)
void
-new_engine_dots(float time_span, int dir) {
- int i;
+new_engine_dots(float time_span) {
+ int dir, i;
int n = time_span * ENGINE_DOTS_PER_TIC;
float a, r; // angle, random length
float dx, dy;
hx = ship.image->w / 2;
hy = ship.image->h / 2;
- for(i = 0; i<n; i++) {
- if(dotptr->active == 0) {
- a = frnd()*M_PI + (dir-1)*M_PI_2;
- r = sin(frnd()*M_PI);
- dx = r * cos(a);
- dy = r * -sin(a); // screen y is "backwards".
-
- dotptr->active = 1;
-
- // dot was created at a random time during the time span
- time = frnd() * time_span; // this is how long ago
-
- // calculate how fast the ship was going when this engine dot was
- // created (as if it had a smooth acceleration). This is used in
- // determining the velocity of the dots, but not their starting
- // location.
- accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
- accelh *= THRUSTER_STRENGTH * time;
- past_ship_dx = ship.dx - accelh;
- accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
- accelv *= THRUSTER_STRENGTH * time;
- past_ship_dy = ship.dy - accelv;
-
- // the starting position (not speed) of the dot is calculated as
- // though the ship were traveling at a constant speed for this
- // time_span.
- dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
- dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
- if(dir&1) {
- dotptr->dx = past_ship_dx + 2*dx;
- dotptr->dy = past_ship_dy + 20*dy;
- dotptr->life = 60 * fabs(dy);
- } else {
- dotptr->dx = past_ship_dx + 20*dx;
- dotptr->dy = past_ship_dy + 2*dy;
- dotptr->life = 60 * fabs(dx);
- }
+ for(dir=0; dir<4; dir++) {
+ if(!(ship.jets & 1<<dir)) continue;
+
+ for(i = 0; i<n; i++) {
+ if(dotptr->active == 0) {
+ a = frnd()*M_PI + (dir-1)*M_PI_2;
+ r = sin(frnd()*M_PI);
+ dx = r * cos(a);
+ dy = r * -sin(a); // screen y is "backwards".
+
+ dotptr->active = 1;
+
+ // dot was created at a random time during the time span
+ time = frnd() * time_span; // this is how long ago
+
+ // calculate how fast the ship was going when this engine dot was
+ // created (as if it had a smooth acceleration). This is used in
+ // determining the velocity of the dots, but not their starting
+ // location.
+ accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
+ accelh *= THRUSTER_STRENGTH * time;
+ past_ship_dx = ship.dx - accelh;
+ accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
+ accelv *= THRUSTER_STRENGTH * time;
+ past_ship_dy = ship.dy - accelv;
+
+ // the starting position (not speed) of the dot is calculated as
+ // though the ship were traveling at a constant speed for this
+ // time_span.
+ dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
+ dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
+ if(dir&1) {
+ dotptr->dx = past_ship_dx + 2*dx;
+ dotptr->dy = past_ship_dy + 20*dy;
+ dotptr->life = 60 * fabs(dy);
+ } else {
+ dotptr->dx = past_ship_dx + 20*dx;
+ dotptr->dy = past_ship_dy + 2*dy;
+ dotptr->life = 60 * fabs(dx);
+ }
- // move the dot as though it were created in the past
- dotptr->x += (dotptr->dx - screendx) * time;
- dotptr->y += (dotptr->dy - screendy) * time;
+ // move the dot as though it were created in the past
+ dotptr->x += (dotptr->dx - screendx) * time;
+ dotptr->y += (dotptr->dy - screendy) * time;
- if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
- else dotptr = edot;
+ if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
+ else dotptr = edot;
+ }
}
}
}
void
draw_dots(SDL_Surface *s) {
- int m;
-
- // Create engine dots
- for(m = 0; m<4; m++) {
- if(ship.jets & 1<<m) { // 'jets' is a bit field
- new_engine_dots(t_frame,m);
- }
- }
-
SDL_LockSurface(s);
draw_dust(s);
draw_engine_dots(s);
void
draw(void) {
- SDL_FillRect(surf_screen,NULL,0); // black background
+ // SDL_FillRect(surf_screen,NULL,0); // black background
draw_dots(surf_screen); // background dots
draw_sprite(SPRITE(&ship));
draw_rocks();
collisions();
- ms_frame = SDL_GetTicks() - ms_end;
- ms_end += ms_frame;
- t_frame = opt_gamespeed * ms_frame / 50;
- if(state == GAMEPLAY) score += ms_frame;
-
// Update the surface
SDL_Flip(surf_screen);
}
insert_score(score);
}
+// Count down the state timer, and change state when it gets to zero or less;
+void
+update_state(float ticks)
+{
+ state_timeout -= ticks*3;
+ if(state_timeout > 0) return;
+
+ switch(state) {
+ case DEAD_PAUSE:
+ // Restore the ship and continue playing
+ ship.flags = DRAW|MOVE|COLLIDE;
+ state = GAMEPLAY;
+ play_tune(TUNE_GAMEPLAY);
+ break;
+ case GAME_OVER:
+ if(new_high_score(score)) init_score_entry();
+ else {
+ state = HIGH_SCORE_DISPLAY;
+ state_timeout = 400;
+ }
+ break;
+ case HIGH_SCORE_DISPLAY:
+ state = TITLE_PAGE;
+ state_timeout = 600.0;
+ fadetimer = 0.0;
+ break;
+ case HIGH_SCORE_ENTRY:
+ break;
+ case TITLE_PAGE:
+ state = HIGH_SCORE_DISPLAY;
+ state_timeout = 200.0;
+ break;
+ case GAMEPLAY:
+ ; // no action necessary
+ }
+}
+
void
gameloop() {
SDL_Event e;
Uint8 *keystate;
float tmp;
-
for(;;) {
+ ms_frame = SDL_GetTicks() - ms_end;
+ ms_end += ms_frame;
+ t_frame = opt_gamespeed * ms_frame / 50;
+ frames++;
+
while(SDL_PollEvent(&e)) {
switch(e.type) {
case SDL_QUIT: return;
}
keystate = SDL_GetKeyState(NULL);
- if(!paused) {
- // Count down the game loop timer, and change state when it gets to zero or less;
-
- if((state_timeout -= t_frame*3) < 0) {
- switch(state) {
- case DEAD_PAUSE:
- // Restore the ship and continue playing
- ship.flags = DRAW|MOVE|COLLIDE;
- state = GAMEPLAY;
- play_tune(TUNE_GAMEPLAY);
- break;
- case GAME_OVER:
- if(new_high_score(score)) init_score_entry();
- else {
- state = HIGH_SCORE_DISPLAY;
- state_timeout = 400;
- }
- break;
- case HIGH_SCORE_DISPLAY:
- state = TITLE_PAGE;
- state_timeout = 600.0;
- fadetimer = 0.0;
- break;
- case HIGH_SCORE_ENTRY:
- break;
- case TITLE_PAGE:
- state = HIGH_SCORE_DISPLAY;
- state_timeout = 200.0;
- break;
- case GAMEPLAY:
- ; // no action necessary
+ if(state == GAMEPLAY) {
+ if(!paused) {
+ score += ms_frame;
+
+ if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
+ if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
+ if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
+ if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
+ if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
+ }
+
+ if(keystate[SDLK_p] | keystate[SDLK_s]) {
+ if(!pausedown) {
+ paused = !paused;
+ pausedown = 1;
+ if(!paused) ms_end = SDL_GetTicks();
}
} else {
- if(state == DEAD_PAUSE) {
- if(bangx < 60) bangx = 60;
- }
+ pausedown = 0;
}
+ }
+
+ if(!paused) {
+ update_state(t_frame);
+
+ if(state == DEAD_PAUSE && bangx < 60) bangx = 60;
// SCROLLING
- tmp = (ship.y+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy);
+ tmp = (ship.y+ship.h/2+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy);
screendy += tmp * t_frame/12;
- tmp = (ship.x+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx);
+ tmp = (ship.x+ship.w/2+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx);
screendx += tmp * t_frame/12;
// taper off so we don't hit the barrier abruptly.
// (if we would hit in < 2 seconds, adjust to 2 seconds).
}
new_rocks(t_frame);
+ new_engine_dots(t_frame);
draw();
reset_rocks();
screendx = SCREENDXMIN; screendy = 0;
- ship.x = XSIZE/2.2; ship.y = YSIZE/2;
+ ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
ship.dx = screendx; ship.dy = screendy;
ship.lives = 4;
ship.flags = MOVE|DRAW|COLLIDE;
ship.jets = 0;
}
- if(state == GAMEPLAY) {
- if(!paused) {
- // FIXME why is this at the bottom? Shouldn't it be up before the ship movement?
- if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
- if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
- if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
- if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
- if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
- }
-
- if(keystate[SDLK_p] | keystate[SDLK_s]) {
- if(!pausedown) {
- paused = !paused;
- pausedown = 1;
- if(!paused) ms_end = SDL_GetTicks();
- }
- } else {
- pausedown = 0;
- }
- }
-
if(state == TITLE_PAGE && keystate[SDLK_h]) {
state = HIGH_SCORE_DISPLAY;
state_timeout = 400;
return 1;
}
+ start = SDL_GetTicks();
+ frames = 0;
gameloop();
+ end = SDL_GetTicks();
+ printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));
return 0;
}