From 08a97e2f39961a79dd57e7310626408b20c5d9e8 Mon Sep 17 00:00:00 2001 From: taw27 Date: Thu, 7 Aug 2008 23:13:07 +0000 Subject: Measure and subtract physics time from the wait git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@233 84d2e878-0bd5-11dd-ad15-13eda11d74c5 --- src/game.c | 3 ++- src/main.c | 6 ++++-- src/physics.c | 13 +++++++++++++ src/render.c | 4 +++- src/types.h | 3 ++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/game.c b/src/game.c index a5727be..9ef3019 100644 --- a/src/game.c +++ b/src/game.c @@ -292,7 +292,8 @@ Game *game_new(int width, int height, GameOptions gameopts) { g->t_fps = SDL_GetTicks(); g->fps = 0; g->query_this_frame = 1; - g->time_to_render = 0; + g->time_render = 30000; + g->time_physics = 0; g->fuel = 1.0; g->radiation = 0.1; g->platform_rel_x = 0.0; diff --git a/src/main.c b/src/main.c index 49c0bd9..9456b56 100644 --- a/src/main.c +++ b/src/main.c @@ -292,8 +292,10 @@ int main(int argc, char *argv[]) { game->frames = 0; } - /* Wait for how long it takes to render the frame at the most recent measurement. */ - if ( !gameopts.no_framerate_limit ) usleep(game->time_to_render/1000); + /* Wait for how long it takes the graphics card to catch up, at the most recent measurement. */ + if ( game->time_physics < game->time_render ) { + if ( !gameopts.no_framerate_limit ) usleep(game->time_render-game->time_physics); + } } diff --git a/src/physics.c b/src/physics.c index 3472cc5..91505b9 100644 --- a/src/physics.c +++ b/src/physics.c @@ -15,6 +15,8 @@ #include #include +#include +#include #include "types.h" #include "model.h" @@ -403,9 +405,14 @@ static void physics_process(ModelInstance *obj, Uint32 dt, Game *game) { void physics_step(Game *game, Uint32 t) { Uint32 dt; + struct timeval tv; + suseconds_t us; dt = t - game->tlast; + gettimeofday(&tv, NULL); + us = tv.tv_usec; + /* Handle things specific to the lander craft */ if ( game->thrusting ) { if ( game->fuel > 0.0 ) { @@ -454,6 +461,12 @@ void physics_step(Game *game, Uint32 t) { game_check_handoff(game); game->tlast = t; + + gettimeofday(&tv, NULL); + us = tv.tv_usec - us; + + game->time_physics = us; + } diff --git a/src/render.c b/src/render.c index 4b95635..5f3445c 100644 --- a/src/render.c +++ b/src/render.c @@ -687,7 +687,9 @@ void render_draw(Game *game, Uint32 t) { GLint available; glGetQueryObjectiv(game->timer_query, GL_QUERY_RESULT_AVAILABLE, &available); if ( available ) { - glGetQueryObjectui64vEXT(game->timer_query, GL_QUERY_RESULT, &game->time_to_render); + GLuint64EXT time_to_render; + glGetQueryObjectui64vEXT(game->timer_query, GL_QUERY_RESULT, &time_to_render); + game->time_render = time_to_render / 1000; /* Convert ns to us */ game->query_this_frame = 1; glDeleteQueries(1, &game->timer_query); } diff --git a/src/types.h b/src/types.h index 37ccf1f..d45f474 100644 --- a/src/types.h +++ b/src/types.h @@ -265,7 +265,8 @@ typedef struct { int fps; int query_this_frame; GLuint timer_query; - GLuint64EXT time_to_render; + long long int time_render; /* Time taken to render, in us */ + long long int time_physics; /* Time taken for physics, in us */ GLfloat radiation; GLfloat fuel; -- cgit v1.2.3