]> git.draconx.ca Git - rrace.git/blobdiff - src/game.c
Track elapsed time of the game.
[rrace.git] / src / game.c
index f7cc2bb706b519c7b4ed60b4f7ec1b29bcae6f6f..c478eef5b55e97bb2043fcc936640914c7bcb93a 100644 (file)
@@ -24,6 +24,7 @@
 #include <limits.h>
 #include <string.h>
 #include <time.h>
+#include <gethrxtime.h>
 #include "game.h"
 
 #define B64(x) ((x) & 0xffffffffffffffff)
@@ -123,8 +124,15 @@ void game_reset(struct board *board)
        unsigned char tiles[25];
        unsigned i;
 
-       if (!rng_is_seeded())
-               game_reseed(time(NULL));
+       if (!rng_is_seeded()) {
+               unsigned long long seed;
+
+               seed = time(NULL);
+               seed += gethrxtime();
+               seed += seed << 32;
+
+               game_reseed(seed);
+       }
 
        for (i = 0; i < 24; i++) {
                tiles[i] = (i%6) + 1;
@@ -200,8 +208,19 @@ int game_check_goal(struct board *board)
        return ret;
 }
 
-void game_finish(struct board *board)
+void game_begin(struct board *board)
+{
+       board->time_start = gethrxtime();
+}
+
+static int_fast32_t elapsed(struct board *board)
 {
+       return (gethrxtime() - board->time_start) / 1000000;
+}
+
+int_fast32_t game_finish(struct board *board)
+{
+       int_fast32_t t = elapsed(board);
        int i;
 
        for (i = 0; i < 4; i++) {
@@ -213,4 +232,6 @@ void game_finish(struct board *board)
         * disable the game since there will be no valid moves.
         */
        board->x = board->y = -1;
+
+       return t;
 }