diff options
author | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-08-07 23:13:05 +0000 |
---|---|---|
committer | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-08-07 23:13:05 +0000 |
commit | 722aa81936c786ad072cec2d189c551a6e246986 (patch) | |
tree | 737c1ee10adc12f78da6cbf80a2d1890932609d9 | |
parent | 71566053936013a578e8882d01e6ecb4c29717fb (diff) |
Sleep for the right amount of time
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@231 84d2e878-0bd5-11dd-ad15-13eda11d74c5
-rw-r--r-- | src/game.c | 4 | ||||
-rw-r--r-- | src/main.c | 7 | ||||
-rw-r--r-- | src/render.c | 25 | ||||
-rw-r--r-- | src/types.h | 4 |
4 files changed, 22 insertions, 18 deletions
@@ -291,12 +291,14 @@ Game *game_new(int width, int height, GameOptions gameopts) { g->frames = 0; g->t_fps = SDL_GetTicks(); g->fps = 0; + g->query_this_frame = 1; + g->time_to_render = 0; g->fuel = 1.0; g->radiation = 0.1; g->platform_rel_x = 0.0; g->platform_rel_y = 0.0; g->time_of_landing_event = -1500.0; /* Force the platform recharge ripple to be 'bright' */ - + /* Renderer setup */ g->render = render_setup(width, height, gameopts.disable_vbos, gameopts.disable_fbos, gameopts.disable_shaders); if ( g->render == NULL ) { @@ -292,11 +292,8 @@ int main(int argc, char *argv[]) { game->frames = 0; } - /* Sleep for a bit to avoid hogging the CPU. - * This is a fudge - ideally this delay would adapt so that the CPU is not hogged when the GPU is the - * limiting factor, and be zero when the CPU is limiting. I don't know of a sensible way to tell which - * is the case. */ - if ( !gameopts.no_framerate_limit ) usleep(30000); + /* 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); } diff --git a/src/render.c b/src/render.c index f7a2be3..4b95635 100644 --- a/src/render.c +++ b/src/render.c @@ -555,13 +555,12 @@ void render_draw(Game *game, Uint32 t) { GLfloat amb[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat fx, fy, fz; GLfloat fxt, fyt, fzt; - GLuint timer_query; r = game->render; - if ( game->frames == 1 ) { - glGenQueries(1, &timer_query); - glBeginQuery(GL_TIME_ELAPSED_EXT, timer_query); + if ( game->query_this_frame ) { + glGenQueries(1, &game->timer_query); + glBeginQuery(GL_TIME_ELAPSED_EXT, game->timer_query); } if ( r->fbos ) { @@ -680,18 +679,20 @@ void render_draw(Game *game, Uint32 t) { render_draw_2d(r, game); - if ( game->frames == 1 ) glEndQuery(GL_TIME_ELAPSED_EXT); + if ( game->query_this_frame) glEndQuery(GL_TIME_ELAPSED_EXT); SDL_GL_SwapBuffers(); - if ( game->frames == 1 ) { - GLint available = 0; - GLuint64EXT timeelapsed; - while (!available) { - glGetQueryObjectiv(timer_query, GL_QUERY_RESULT_AVAILABLE, &available); + if ( !game->query_this_frame ) { + GLint available; + glGetQueryObjectiv(game->timer_query, GL_QUERY_RESULT_AVAILABLE, &available); + if ( available ) { + glGetQueryObjectui64vEXT(game->timer_query, GL_QUERY_RESULT, &game->time_to_render); + game->query_this_frame = 1; + glDeleteQueries(1, &game->timer_query); } - glGetQueryObjectui64vEXT(timer_query, GL_QUERY_RESULT, &timeelapsed); - printf("Frame took %lli ms to render\n", (long long int)(timeelapsed/1e6)); + } else { + game->query_this_frame = 0; } } diff --git a/src/types.h b/src/types.h index 550d931..37ccf1f 100644 --- a/src/types.h +++ b/src/types.h @@ -259,9 +259,13 @@ typedef struct { int paused; int pause_rel; + /* Performance monitoring stuff */ int frames; Uint32 t_fps; int fps; + int query_this_frame; + GLuint timer_query; + GLuint64EXT time_to_render; GLfloat radiation; GLfloat fuel; |