From c21dcf6c7fedc513967be6ca1046ae7d19de87fb Mon Sep 17 00:00:00 2001 From: Tom White Date: Wed, 13 Aug 2008 01:32:52 +0100 Subject: Use unified 'game time' concept, makes pause work properly --- src/audio.c | 8 ++++++++ src/audio.h | 2 ++ src/game.c | 3 ++- src/main.c | 30 ++++++++++++++++-------------- src/physics.c | 11 +++-------- src/physics.h | 2 +- src/render.c | 24 ++++++++++++------------ src/render.h | 2 +- src/types.h | 4 +++- 9 files changed, 48 insertions(+), 38 deletions(-) diff --git a/src/audio.c b/src/audio.c index 980bfe6..2a7cd37 100644 --- a/src/audio.c +++ b/src/audio.c @@ -507,3 +507,11 @@ void audio_shutdown(AudioContext *a) { } +void audio_pause(AudioContext *a) { + SDL_PauseAudio(1); +} + +void audio_unpause(AudioContext *a) { + SDL_PauseAudio(0); +} + diff --git a/src/audio.h b/src/audio.h index 0e159a5..fc90980 100644 --- a/src/audio.h +++ b/src/audio.h @@ -21,6 +21,8 @@ extern AudioContext *audio_setup(int debug, int no_music); extern void audio_shutdown(AudioContext *ctx); extern void audio_play(AudioContext *a, char *name, float volume, int repeat); +extern void audio_pause(AudioContext *a); +extern void audio_unpause(AudioContext *a); #endif /* AUDIO_H */ diff --git a/src/game.c b/src/game.c index 9ef3019..c77a3c6 100644 --- a/src/game.c +++ b/src/game.c @@ -426,10 +426,11 @@ void game_pause(Game *game) { if ( game->paused ) { game->paused = 0; - game->tlast = SDL_GetTicks(); + audio_unpause(game->audio); game->pause_rel = 0; } else { game->paused = 1; + audio_pause(game->audio); game->pause_rel = 0; } diff --git a/src/main.c b/src/main.c index 90b733c..de5917f 100644 --- a/src/main.c +++ b/src/main.c @@ -87,7 +87,6 @@ int main(int argc, char *argv[]) { int c; Uint32 video_flags; ScreenResolution res; - Uint32 t = 0; GameOptions gameopts; Uint16 cx, cy; double vyaw_scale, vang_scale; @@ -213,16 +212,21 @@ int main(int argc, char *argv[]) { /* Main loop */ finished = 0; + game->time = 0.0; + game->tlast = SDL_GetTicks(); while ( !finished ) { int mx, my; Uint8 buttons; int mouse_thrust = 0; + int dt; - /* Timer advances only when game is not paused */ + /* Tick size is measured, ... */ + dt = SDL_GetTicks() - game->tlast; + game->tlast = SDL_GetTicks(); + /* ... but timer advances only when game is not paused */ if ( !game->paused ) { - game->tlast = t; - t = SDL_GetTicks(); + game->time += dt; } SDL_PollEvent(&event); @@ -253,7 +257,7 @@ int main(int argc, char *argv[]) { break; case SDL_VIDEOEXPOSE : /* Don't bother redrawing if not paused - not long to wait! */ - if ( game->paused ) render_draw(game, t); + if ( game->paused ) render_draw(game); break; case SDL_QUIT : finished = 1; @@ -270,17 +274,15 @@ int main(int argc, char *argv[]) { mouse_thrust = 1; } if ( !game->paused ) { - physics_step(game, t); - render_draw(game, t); + physics_step(game, dt); + render_draw(game); } if ( mouse_thrust ) game->thrusting = 0; if ( gameopts.status_line ) { - printf("%+7.4f %+7.4f %+7.4f %+6.1f deg %+7.5f %+7.5f %+7.5f %2i %2i %2i %3i fps " - "(r:%6lli p:%6lli) \r", - game->lander->x, game->lander->y, game->lander->z, - rad2deg(game->lander->yaw), game->lander->vx, game->lander->vy, game->lander->vz, + printf("%10lli %+7.5f %+7.5f %+7.5f %2i %2i %2i %3i fps (r:%6lli p:%6lli) \r", + game->time, game->lander->vx, game->lander->vy, game->lander->vz, game->cur_room_x, game->cur_room_y, game->cur_room_z, game->fps, game->time_render, game->time_physics); fflush(stdout); @@ -288,9 +290,9 @@ int main(int argc, char *argv[]) { /* Calculate FPS every half a second */ game->frames++; - if ( t - game->t_fps > 500 ) { - game->fps = (500*game->frames) / (t - game->t_fps); - game->t_fps = t; + if ( game->time - game->t_fps > 500 ) { + game->fps = (500*game->frames) / (game->time - game->t_fps); + game->t_fps = game->time; game->frames = 0; } diff --git a/src/physics.c b/src/physics.c index 08295a9..7867fd1 100644 --- a/src/physics.c +++ b/src/physics.c @@ -358,7 +358,7 @@ static void physics_process(ModelInstance *obj, Uint32 dt, Game *game) { obj->recharging = 1; game->platform_rel_x = coll.obj->x - obj->x; game->platform_rel_y = coll.obj->y - obj->y; - game->time_of_landing_event = game->tlast + sttc; + game->time_of_landing_event = game->time + sttc; } else { @@ -404,15 +404,12 @@ static void physics_process(ModelInstance *obj, Uint32 dt, Game *game) { } -void physics_step(Game *game, Uint32 t) { +void physics_step(Game *game, int dt) { - Uint32 dt; struct timeval tv; suseconds_t us; time_t sec; - dt = t - game->tlast; - gettimeofday(&tv, NULL); us = tv.tv_usec; sec = tv.tv_sec; @@ -425,7 +422,7 @@ void physics_step(Game *game, Uint32 t) { game->lander->landed = 0; game->radiation = 0.1; if ( game->lander->recharging ) { - game->time_of_landing_event = game->tlast + dt; + game->time_of_landing_event = game->time + dt; game->lander->recharging = 0; } } @@ -464,8 +461,6 @@ void physics_step(Game *game, Uint32 t) { game_check_handoff(game); - game->tlast = t; - gettimeofday(&tv, NULL); us = tv.tv_usec - us; sec = tv.tv_sec - sec; diff --git a/src/physics.h b/src/physics.h index b6db660..e1e3ffc 100644 --- a/src/physics.h +++ b/src/physics.h @@ -18,7 +18,7 @@ #include "types.h" -extern void physics_step(Game *game, Uint32 t); +extern void physics_step(Game *game, int dt); #endif /* PHYSICS_H */ diff --git a/src/render.c b/src/render.c index 1bddac0..76821f4 100644 --- a/src/render.c +++ b/src/render.c @@ -228,7 +228,7 @@ static void render_placeholder_texture(RenderContext *r) { } -static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderContext *r, int *nvert, GLfloat alpha) { +static int render_model_instance_draw(ModelInstance *instance, RenderContext *r, Game *g, int *nvert, GLfloat alpha) { int j; Model *m; @@ -255,7 +255,7 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC if ( ((p->attribs & ATTRIB_PULSE) && !(p->attribs & ATTRIB_SWIRLY)) || ((p->attribs & ATTRIB_SWIRLY) && !(r->fbos && r->shaders)) ) { float s; - s = fabsf(0.4*cosf(t * 0.001)); + s = fabsf(0.4*cosf(g->time * 0.001)); GLfloat c[] = {s*p->col_r, s*p->col_g, s*p->col_b, 1.0}; glMaterialfv(GL_FRONT, GL_EMISSION, c); glColor4f(0.3, 0.3, 0.3, alpha); @@ -370,7 +370,7 @@ static void render_draw_line(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLf } -static void render_draw_stuff(Game *game, Uint32 t, GLfloat alpha) { +static void render_draw_stuff(Game *game, GLfloat alpha) { int i; int nvert = 0; @@ -398,7 +398,7 @@ static void render_draw_stuff(Game *game, Uint32 t, GLfloat alpha) { z = room->rz - game->cur_room_z; glPushMatrix(); glTranslatef(10.0*x, 10.0*y, 10.0*z); - render_model_instance_draw(room->objects[j], t, game->render, &nvert, alpha); + render_model_instance_draw(room->objects[j], game->render, game, &nvert, alpha); glPopMatrix(); } @@ -566,7 +566,7 @@ static void render_draw_2d(RenderContext *r, Game *game) { } -void render_draw(Game *game, Uint32 t) { +void render_draw(Game *game) { RenderContext *r; GLfloat amb[] = { 0.0, 0.0, 0.0, 1.0 }; @@ -591,15 +591,15 @@ void render_draw(Game *game, Uint32 t) { GLfloat rrb = 0.0; glUseProgram(r->swirly_program); - shaderutils_setunf(r->swirly_program, "time", t); + shaderutils_setunf(r->swirly_program, "time", game->time); shaderutils_setuni(r->swirly_program, "landed", game->lander->recharging); if ( game->lander->recharging ) { /* Fade in */ - rrb = fminf(((GLfloat)t-game->time_of_landing_event)/1500.0, 1.0); + rrb = fminf(((GLfloat)game->time-game->time_of_landing_event)/1500.0, 1.0); shaderutils_setuni(r->swirly_program, "rechargeripple", 1); - } else if ( game->time_of_landing_event - t < 750 ) { + } else if ( game->time_of_landing_event - game->time < 750 ) { /* Fade out */ - rrb = fmaxf(1.0 - ((GLfloat)t-game->time_of_landing_event)/750.0, 0.0); + rrb = fmaxf(1.0 - ((GLfloat)game->time-game->time_of_landing_event)/750.0, 0.0); shaderutils_setuni(r->swirly_program, "rechargeripple", 1); } else { shaderutils_setuni(r->swirly_program, "rechargeripple", 0); @@ -664,7 +664,7 @@ void render_draw(Game *game, Uint32 t) { render_setup_lighting(game); amb[0] = 0.02; amb[1] = 0.02; amb[2] = 0.02; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb); - render_draw_stuff(game, t, 1.0); + render_draw_stuff(game, 1.0); /* Draw the lander */ glEnable(GL_LIGHT2); @@ -675,7 +675,7 @@ void render_draw(Game *game, Uint32 t) { glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); if ( r->shaders ) glUseProgram(r->fill_program); - render_model_instance_draw(game->lander, t, r, NULL, 1.0); + render_model_instance_draw(game->lander, r, game, NULL, 1.0); glPopClientAttrib(); if ( r->shaders ) glUseProgram(0); render_draw_line(game->lander->x, game->lander->y, game->lander->z, @@ -690,7 +690,7 @@ void render_draw(Game *game, Uint32 t) { render_setup_lighting(game); amb[0] = 0.1; amb[1] = 0.1; amb[2] = 0.1; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb); - render_draw_stuff(game, t, 0.5); + render_draw_stuff(game, 0.5); glDisable(GL_POLYGON_OFFSET_FILL); glFrontFace(GL_CCW); diff --git a/src/render.h b/src/render.h index e9cb4ff..8dd00a4 100644 --- a/src/render.h +++ b/src/render.h @@ -20,7 +20,7 @@ extern RenderContext *render_setup(int width, int height, int disable_vbos, int disable_fbos, int disable_shaders); extern void render_shutdown(RenderContext *ctx); -extern void render_draw(Game *game, Uint32 t); +extern void render_draw(Game *game); extern void render_set_wireframe(int wireframe); #endif /* RENDER_H */ diff --git a/src/types.h b/src/types.h index a2cd40e..a1a5358 100644 --- a/src/types.h +++ b/src/types.h @@ -240,7 +240,9 @@ typedef struct { unsigned int forward; unsigned int reverse; - Uint32 tlast; /* Time at which the last physics step was performed */ + long long int tlast; + long long int time; /* Time in the game (milliseconds since start of game + * as measured at the start of the current frame */ ModelInstance *lander; -- cgit v1.2.3