--- /dev/null
+#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;
+}
--- /dev/null
+#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