aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <weiss@jade.(none)>2008-08-13 12:40:53 +0100
committerThomas White <weiss@jade.(none)>2008-08-13 12:40:53 +0100
commit877a8718f419efe6bfef4ba248741c1233e22a1d (patch)
tree525e9329ab75107c1542b4d51034e4553ebef325
parentc5844067aa6dea33ee7be800c8ab51baf1121848 (diff)
Increase resolution of game timer
-rw-r--r--src/game.c1
-rw-r--r--src/main.c12
-rw-r--r--src/types.h8
-rw-r--r--src/utils.c16
-rw-r--r--src/utils.h1
5 files changed, 28 insertions, 10 deletions
diff --git a/src/game.c b/src/game.c
index c77a3c6..dd309c5 100644
--- a/src/game.c
+++ b/src/game.c
@@ -289,7 +289,6 @@ Game *game_new(int width, int height, GameOptions gameopts) {
g->paused = 0;
g->pause_rel = 1;
g->frames = 0;
- g->t_fps = SDL_GetTicks();
g->fps = 0;
g->query_this_frame = 1;
g->time_render = 30000;
diff --git a/src/main.c b/src/main.c
index 5bc4886..2624485 100644
--- a/src/main.c
+++ b/src/main.c
@@ -213,17 +213,19 @@ int main(int argc, char *argv[]) {
/* Main loop */
finished = 0;
game->time = 0.0;
- game->tlast = SDL_GetTicks();
+ game->tlast = utils_highresms();
+ game->t_fps = game->tlast;
while ( !finished ) {
int mx, my;
Uint8 buttons;
int mouse_thrust = 0;
- int dt;
+ double us, dt;
/* Tick size is measured, ... */
- dt = SDL_GetTicks() - game->tlast;
- game->tlast = SDL_GetTicks();
+ us = utils_highresms();
+ dt = us - game->tlast;
+ game->tlast = us;
/* ... but timer advances only when game is not paused */
if ( !game->paused ) {
game->time += dt;
@@ -280,7 +282,7 @@ int main(int argc, char *argv[]) {
if ( mouse_thrust ) game->thrusting = 0;
if ( gameopts.status_line ) {
- printf("%10lli %4i %+7.5f %+7.5f %+7.5f %2i %2i %2i %3i fps (r:%6lli p:%6lli) \r",
+ printf("%+13.3f %+5.3f %+7.5f %+7.5f %+7.5f %2i %2i %2i %3i fps (r:%6lli p:%6lli) \r",
game->time, dt,
game->lander->vx, game->lander->vy, game->lander->vz,
game->cur_room_x, game->cur_room_y, game->cur_room_z, game->fps,
diff --git a/src/types.h b/src/types.h
index a1a5358..c9a4b0c 100644
--- a/src/types.h
+++ b/src/types.h
@@ -240,9 +240,9 @@ typedef struct {
unsigned int forward;
unsigned int reverse;
- 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 */
+ double tlast;
+ double time; /* Time in the game (milliseconds since start of game
+ / * as measured at the start of the current frame */
ModelInstance *lander;
@@ -265,7 +265,7 @@ typedef struct {
/* Performance monitoring stuff */
int frames;
- Uint32 t_fps;
+ double t_fps;
int fps;
int query_this_frame;
GLuint timer_query;
diff --git a/src/utils.c b/src/utils.c
index f9ac11a..e957f2a 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -15,6 +15,8 @@
#include <string.h>
#include <stdlib.h>
+#include <sys/time.h>
+#include <time.h>
#include "utils.h"
@@ -111,3 +113,17 @@ int assplode(const char *a, const char *delims, char ***pbits, AssplodeFlag flag
}
+double utils_highresms() {
+
+ struct timeval tv;
+ suseconds_t us;
+ time_t sec;
+
+ gettimeofday(&tv, NULL);
+ us = tv.tv_usec;
+ sec = tv.tv_sec;
+
+ return ((double)us+1000000.0*sec)/1000.0;
+
+}
+
diff --git a/src/utils.h b/src/utils.h
index 2ec62df..d25dc3c 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -28,6 +28,7 @@ typedef enum {
extern void chomp(char *a);
extern int assplode(const char *a, const char *delims, char ***pbits, AssplodeFlag flags);
+extern double utils_highresms(void);
/* So you can do assert(this_point_not_reached) */
extern int this_point_not_reached;