mirror of
https://github.com/thepigeongenerator/sdl_template.git
synced 2025-12-17 05:55:47 +01:00
update gametime to be less heavy and more flexible
This commit is contained in:
@@ -5,17 +5,31 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#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])
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,39 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#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 <windows.h>
|
||||
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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user