From 9d71171f71f0c80e9b98c07c81bbee95ad010775 Mon Sep 17 00:00:00 2001
From: Matthew Owens <matthew@owens.tech>
Date: Thu, 1 Oct 2020 15:59:00 +0100
Subject: [PATCH] added missing timestep files

---
 src/timestep.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/timestep.h | 10 ++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 src/timestep.c
 create mode 100644 src/timestep.h

diff --git a/src/timestep.c b/src/timestep.c
new file mode 100644
index 0000000..7ddc484
--- /dev/null
+++ b/src/timestep.c
@@ -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
index 0000000..5c70f91
--- /dev/null
+++ b/src/timestep.h
@@ -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
-- 
2.20.1