diff options
Diffstat (limited to 'game_time.c')
-rw-r--r-- | game_time.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/game_time.c b/game_time.c new file mode 100644 index 0000000..af116b6 --- /dev/null +++ b/game_time.c @@ -0,0 +1,21 @@ +#include <SDL2/SDL.h> +#include "./constants.h" + +int last_frame_time = 0; + +void delay(void) { + // logic to keep a fixed timestep + int time_to_wait = FRAME_TARGET_TIME - (SDL_GetTicks() - last_frame_time); + + if(time_to_wait > 0 && time_to_wait <= FRAME_TARGET_TIME) { + SDL_Delay(time_to_wait); + } +} + +float get_delta_time() { + // get a delta time factor converted to seconds + float delta_time = (SDL_GetTicks() - last_frame_time) / 1000.0f; + + last_frame_time = SDL_GetTicks(); + return delta_time; +} |