added missing timestep files
authorMatthew Owens <matthew@owens.tech>
Thu, 1 Oct 2020 14:59:00 +0000 (15:59 +0100)
committerMatthew Owens <matthew@owens.tech>
Thu, 1 Oct 2020 14:59:00 +0000 (15:59 +0100)
src/timestep.c [new file with mode: 0644]
src/timestep.h [new file with mode: 0644]

diff --git a/src/timestep.c b/src/timestep.c
new file mode 100644 (file)
index 0000000..7ddc484
--- /dev/null
@@ -0,0 +1,51 @@
+#include "timestep.h"
+#include <SDL2/SDL.h>
+#include <math.h>
+
+static const int MAX_PHYSICS_STEPS = 6;
+static const float MAX_DELTA_TIME = 1.f;
+static const float MS_PER_SECOND = 1000.f;
+
+static float desiredFrameTime;
+static Uint32 currentT;
+static Uint32 prevT;
+static float frameTime;
+static float totalDeltaTime;
+static float deltaTime;
+
+float ts_delta()
+{
+       return deltaTime;
+}
+
+void ts_set_desired_ft(float desiredFPS)
+{
+       desiredFrameTime = MS_PER_SECOND/desiredFPS;
+}
+
+void ts_init(float desiredFPS)
+{
+       ts_set_desired_ft(desiredFPS);
+
+       currentT = SDL_GetTicks();
+       prevT = SDL_GetTicks();
+}
+
+void ts_update()
+{
+       prevT = currentT;
+       currentT = SDL_GetTicks();
+       frameTime = currentT - prevT;
+       totalDeltaTime = frameTime / desiredFrameTime;
+}
+
+bool ts_phys_required(int updatesThisFrame)
+{
+       if(totalDeltaTime <= 0.f || updatesThisFrame >= MAX_PHYSICS_STEPS){
+               return false;
+       }
+
+       deltaTime = fminf(totalDeltaTime, MAX_DELTA_TIME);
+       totalDeltaTime -= deltaTime;
+       return true;
+}
diff --git a/src/timestep.h b/src/timestep.h
new file mode 100644 (file)
index 0000000..5c70f91
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef TIMESTEP_H
+#define TIMESTEP_H
+#include <stdbool.h>
+
+void ts_init(float desiredFPS);
+void ts_set_desired_ft(float desiredUpdateFT);
+void ts_update();
+bool ts_phys_required(int updatesThisFrame);
+float ts_delta();
+#endif//TIMESTEP_H