diff --git a/src/context.h b/src/context.h index 7b4862d..578d1ab 100644 --- a/src/context.h +++ b/src/context.h @@ -39,6 +39,7 @@ typedef struct context { int64_t frame_accumulator; int64_t delta_averager_residual; int64_t time_averager[4]; + int64_t delta_time; /* preserves real time frame delta with no manipilation */ uint64_t tick_count; uint64_t step_count; diff --git a/src/main.c b/src/main.c index 053d789..66517a6 100644 --- a/src/main.c +++ b/src/main.c @@ -56,6 +56,7 @@ void main_loop(void) { int64_t current_frame_time = SDL_GetPerformanceCounter(); int64_t delta_time = current_frame_time - ctx.prev_frame_time; ctx.prev_frame_time = current_frame_time; + ctx.delta_time = delta_time; /* handle unexpected timer anomalies (overflow, extra slow frames, etc) */ if (delta_time > ctx.desired_frametime * 8) { /* ignore extra-slow frames */ diff --git a/src/util.c b/src/util.c index f8764cf..ba22cb2 100644 --- a/src/util.c +++ b/src/util.c @@ -1,4 +1,5 @@ #include "util.h" +#include "context.h" #include #include @@ -186,5 +187,18 @@ t_frect to_frect(t_rect rect) { void tick_timer(int *value) { - *value = MAX(*value - 1, 0); + *value = MAX(*value - 1, 0); +} + +void tick_ftimer(float *value) { + *value = MAX(*value - ((float)ctx.delta_time / (float)ctx.clocks_per_second), 0.0f); +} + +bool tick_ftimer_repeat(float *value, float at) { + *value -= (float)ctx.delta_time / (float)ctx.clocks_per_second; + if (*value < 0.0f) { + *value += at; + return true; + } + return false; } diff --git a/src/util.h b/src/util.h index b7c77e4..17f439f 100644 --- a/src/util.h +++ b/src/util.h @@ -118,4 +118,14 @@ typedef struct fvec2 { void tick_timer(int *value); +/* decrements a floating point second-based timer, stopping at 0.0 */ +/* meant for poll based real time logic in game logic */ +/* note that it should be decremented only on the next tick after its creation */ +void tick_ftimer(float *value); + +/* same as `tick_ftimer` but instaed of clamping it repeats */ +/* returns true if value was cycled */ +bool tick_ftimer_repeat(float *value, float at); + + #endif