aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-07 23:13:07 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-07 23:13:07 +0000
commit08a97e2f39961a79dd57e7310626408b20c5d9e8 (patch)
treef6cebdc6e07ae0b4af5c40d84451ad5cacf073f4
parentc55491b4c777c1ae835a16f8969e0e8dede9a030 (diff)
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
-rw-r--r--src/game.c3
-rw-r--r--src/main.c6
-rw-r--r--src/physics.c13
-rw-r--r--src/render.c4
-rw-r--r--src/types.h3
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 <SDL.h>
#include <math.h>
+#include <sys/time.h>
+#include <time.h>
#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;