From b5c531532e3786a4e2f87ff549f8d02f341e7a44 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 13 Feb 2025 22:46:17 +0100 Subject: [PATCH] add gametime --- src/game/game.c | 11 +++++------ src/game/game.h | 3 ++- src/game/gametime.h | 31 +++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 src/game/gametime.h diff --git a/src/game/game.c b/src/game/game.c index 3bbb57e..231cba7 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -1,18 +1,17 @@ #include "game.h" -#include "../error.h" +#include "gametime.h" void game_init(game_data* dat) { - (void)dat; - //error(STATUS_ERROR, "function not defined"); + *dat = (game_data){ + gametime_new(), + }; } void game_update(game_data* dat) { - (void)dat; - //error(STATUS_ERROR, "function not defined"); + gametime_update(&dat->time); } void game_free(game_data* dat) { (void)dat; - error(STATUS_ERROR, "function not defined"); } diff --git a/src/game/game.h b/src/game/game.h index 6d4a4f6..59662d0 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -1,9 +1,10 @@ #pragma once #include +#include "gametime.h" typedef struct { - uint8_t tmp; + gametime time; } game_data; void game_init(game_data*); // initializes everything needed to start the game; outputs to game_data diff --git a/src/game/gametime.h b/src/game/gametime.h new file mode 100644 index 0000000..647060f --- /dev/null +++ b/src/game/gametime.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +typedef struct { + struct timespec ts; // stores the time at the current update + float timescale; // multiplier for the time calculation, default value is 1.0 + float deltatime; // the time that it took between updates +} gametime; + +// initializes the gametime struct +static inline gametime gametime_new(void) { + return (gametime){ + {0}, + 1.0F, + 0.0F, + }; +} + +// updates the internal variables +static inline void gametime_update(gametime* gt) { + struct timespec ts; + timespec_get(&ts, TIME_UTC); + gt->deltatime = ((double)(ts.tv_nsec - gt->ts.tv_nsec) * 1e-9) * gt->timescale; + gt->ts = ts; +} + +// gets how many times the +static inline float gametime_get_ups(gametime* gt) { + return 1.0F / gt->deltatime; +}