From 178089f47710ff90f71a8a2a553ed0700b198ed3 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sun, 13 Jun 2021 20:41:37 +0200 Subject: [PATCH] Public calculate_dtime function --- plugins/game/game.c | 8 ++++++-- plugins/game/game.h | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/game/game.c b/plugins/game/game.c index 5d5c31a..152a5c9 100644 --- a/plugins/game/game.c +++ b/plugins/game/game.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -125,6 +124,11 @@ void *make_buffer(void *ptr, size_t size) return buf; } +double calculate_dtime(struct timespec from, struct timespec to) +{ + return (double) (to.tv_sec - from.tv_sec) + (double) (to.tv_nsec - from.tv_nsec) / 1000000000.0; +} + /* Game-related utility functions */ void quit() @@ -517,7 +521,7 @@ void game() while (running) { clock_gettime(CLOCK_REALTIME, &ts); - double dtime = (double) (ts.tv_sec - ts_old.tv_sec) + (double) (ts.tv_nsec - ts_old.tv_nsec) / 1000000000.0; + double dtime = calculate_dtime(ts_old, ts); ts_old = ts; bool dead = player_dead(); diff --git a/plugins/game/game.h b/plugins/game/game.h index 7ae9344..60c1d61 100644 --- a/plugins/game/game.h +++ b/plugins/game/game.h @@ -4,6 +4,7 @@ #include #include #include +#include #define MAP_HEIGHT 1000 #define MAP_WIDTH 1000 #define LIGHT 10 @@ -111,6 +112,7 @@ int clamp(int v, int max, int min); int max(int a, int b); int min(int a, int b); void *make_buffer(void *ptr, size_t size); +double calculate_dtime(struct timespec from, struct timespec to); void quit(); bool player_dead(); -- 2.44.0