]> git.itanic.dy.fi Git - sdl-planets/commitdiff
main.c: Switch to microsecond resolution time source
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 30 Mar 2010 17:24:45 +0000 (20:24 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 30 Mar 2010 17:24:45 +0000 (20:24 +0300)
This is needed if the simulation step lenght is very short.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c

diff --git a/main.c b/main.c
index 6863b18c1970131993e6aaf1e593117f41deeb55..ebdfa90e978da043657a0f3cd6bcd2cd190d1b84 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,6 +1,7 @@
 #include <SDL.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <time.h>
 
 #include "random.h"
 #include "planet.h"
@@ -23,6 +24,21 @@ static void clear_buf(SDL_Surface *screen)
                buf[i] = 0;
 }
 
+/* Return time in microseconds */
+
+static unsigned long long gettime(void)
+{
+       struct timespec tp;
+       unsigned long long ret;
+
+       clock_gettime(CLOCK_MONOTONIC, &tp);
+
+       ret = tp.tv_sec * 1000 * 1000;
+       ret += tp.tv_nsec;
+
+       return ret;
+}
+
 static int poll_events(SDL_Surface **screen, struct camera *cam,
                       double *time_scale, double time)
 {
@@ -118,8 +134,9 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
        struct planet *planet, *pl1, *pl2;
        struct camera camera;
        int planets;
-       int old_ticks, ticks, framecount = 0, last_fps_time = 0;
+       int framecount = 0, last_fps_time = 0;
        int last_framecount = 0;
+       unsigned long long old_ticks, ticks;
        double time = 0, last_fps = 0, time_scale = 1;
 
        init_camera(&camera);
@@ -167,7 +184,7 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                        return;
 
                old_ticks = ticks;
-               ticks = SDL_GetTicks();
+               ticks = gettime();
                time = (ticks - old_ticks) / 1000.0;
                time = MIN(time, 0.02);