mirror of
https://github.com/thepigeongenerator/sdl_template.git
synced 2025-12-17 05:55:47 +01:00
add seconds to gametime so those may also be used
This commit is contained in:
@@ -4,14 +4,19 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
struct timespec ts; // stores the time at the current update
|
struct timespec ts; // stores the time at the current update
|
||||||
float timescale; // multiplier for the time calculation, default value is 1.0
|
double sec; // stores the current time in seconds
|
||||||
float deltatime; // the time that it took between updates
|
float scale; // multiplier for the time calculation, default value is 1.0
|
||||||
|
float delta; // the time that it took between updates
|
||||||
} gametime;
|
} gametime;
|
||||||
|
|
||||||
// initializes the gametime struct
|
// initializes the gametime struct
|
||||||
static inline gametime gametime_new(void) {
|
static inline gametime gametime_new(void) {
|
||||||
|
struct timespec ts;
|
||||||
|
timespec_get(&ts, TIME_UTC);
|
||||||
|
|
||||||
return (gametime){
|
return (gametime){
|
||||||
{0},
|
ts,
|
||||||
|
0.0,
|
||||||
1.0F,
|
1.0F,
|
||||||
0.0F,
|
0.0F,
|
||||||
};
|
};
|
||||||
@@ -21,11 +26,12 @@ static inline gametime gametime_new(void) {
|
|||||||
static inline void gametime_update(gametime* gt) {
|
static inline void gametime_update(gametime* gt) {
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
timespec_get(&ts, TIME_UTC);
|
timespec_get(&ts, TIME_UTC);
|
||||||
gt->deltatime = ((double)(ts.tv_nsec - gt->ts.tv_nsec) * 1e-9) * gt->timescale;
|
gt->sec = (double)ts.tv_nsec * 1e-9; // calculate the current time in seconds
|
||||||
gt->ts = ts;
|
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
|
// gets how many times the game updates per second
|
||||||
static inline float gametime_get_ups(gametime* gt) {
|
static inline float gametime_get_ups(gametime* gt) {
|
||||||
return 1.0F / gt->deltatime;
|
return 1.0F / gt->delta;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user