diff --git a/src/game/game.c b/src/game/game.c index 06d8f9a..f3cde1d 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -5,17 +5,31 @@ #include #include #include +#include #include "gametime.h" -void game_init(gamedata* dat, gametime* gt) { +void game_init(gamedata* dat) { + // initialize the game time + struct gametime gt = {{0}, 0}; + gametime_get(>.ts); + + // initialize the game data *dat = (gamedata){ gt, true, }; } +static inline void update_gametime(gamedata* dat) { + struct timespec ts; + gametime_get(&ts); + dat->time.delta = (ts.tv_sec - dat->time.ts.tv_sec) + (ts.tv_nsec - dat->time.ts.tv_nsec) * 1e-9; + dat->time.ts = ts; +} + void game_update(gamedata* dat) { + update_gametime(dat); // update the game time uint8_t const* keys = SDL_GetKeyboardState(NULL); if (keys[SDL_SCANCODE_ESCAPE]) diff --git a/src/game/game.h b/src/game/game.h index 1a73cf2..4c74406 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -12,10 +12,10 @@ #define TAUf (M_PIf * 2.0F) // τ constant as a 32-bit floating point typedef struct { - gametime* time; + struct gametime time; bool run; } gamedata; -void game_init(gamedata*, gametime*); // initializes everything needed to start the game; outputs to game_data +void game_init(gamedata*); // initializes everything needed to start the game; outputs to game_data void game_update(gamedata*); // causes an update to occur within the game void game_free(gamedata*); // frees the resources associated with the game diff --git a/src/game/gametime.h b/src/game/gametime.h index 61daba3..77868ee 100644 --- a/src/game/gametime.h +++ b/src/game/gametime.h @@ -1,39 +1,24 @@ #pragma once - #include -#include "../util/attributes.h" - -typedef struct { - struct timespec ts; // stores the time at the current update - double sec; // stores the current time in seconds - float scale; // multiplier for the time calculation, default value is 1.0 - float delta; // the time that it took between updates -} gametime; - -// initializes the gametime struct -atrb_const static inline gametime gametime_new(void) { +struct gametime { struct timespec ts; - timespec_get(&ts, TIME_UTC); + float delta; +}; - return (gametime){ - ts, - 0.0, - 1.0F, - 0.0F, - }; +#if _POSIX_C_SOURCE >= 199309L +static inline void gametime_get(struct timespec* ts) { + clock_gettime(CLOCK_MONOTONIC, ts); } - -// updates the internal variables -static inline void gametime_update(gametime* gt) { - struct timespec ts; - timespec_get(&ts, TIME_UTC); - gt->sec = (double)ts.tv_nsec * 1e-9 + ts.tv_sec; // calculate the current time in seconds - gt->delta = ((double)(ts.tv_nsec - gt->ts.tv_nsec) * 1e-9) * gt->scale; // calculate how much time has passed between this and last frame - gt->ts = ts; // update the game's timespec -} - -// gets how many times the game updates per second -atrb_const static inline float gametime_get_ups(gametime const* gt) { - return 1.0F / gt->delta; +#elif defined _WIN32 +# include +static inline void gametime_get(struct timespec* ts) { + LARGE_INTEGER cnt, frq; + QueryPerformanceCounter(&cnt); + QueryPerformanceFrequency(&frq); + ts->tv_sec = (time_t)(cnt.QuadPart / frq.QuadPart); + ts->tv_nsec = (time_t)((cnt.QuadPart % frq.QuadPart) * 1000000000 / frq.QuadPart); } +#else +# error platform not supported +#endif diff --git a/src/main.c b/src/main.c index 27d8b79..a2c7614 100644 --- a/src/main.c +++ b/src/main.c @@ -5,11 +5,9 @@ #include "error.h" #include "game/game.h" -#include "game/gametime.h" #include "window/render.h" // initialized in init(), reading beforehand is undefined behaviour -static gametime gt; static gamedata gdat; static renderdata rdat; @@ -20,8 +18,7 @@ static void init(void) { fatal(ERROR_SDL_INIT, __FILE_NAME__, __LINE__, "SDL could not initialize! SDL Error: %s", SDL_GetError()); // initialize other game components - gt = gametime_new(); - game_init(&gdat, >); + game_init(&gdat); render_init(&rdat, &gdat); } @@ -40,7 +37,6 @@ static void update(void) { } // perform updates - gametime_update(>); game_update(&gdat); render_update(&rdat); }