summaryrefslogtreecommitdiff
path: root/game_time.c
diff options
context:
space:
mode:
authorbanana <delgadillo.tyler@gmail.com>2024-03-22 13:46:36 -0700
committerbanana <delgadillo.tyler@gmail.com>2024-03-22 13:46:36 -0700
commitad22993557296b02bc0c947cb3bb6247fac8f47f (patch)
tree2c83ab589acab8a5074583d625d54ec44677e5d0 /game_time.c
Initial commitHEADmaster
Diffstat (limited to 'game_time.c')
-rw-r--r--game_time.c21
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;
+}