From 92c3761317891e2b90c937e63f8de468edc2b926 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 11 Sep 2021 15:07:34 +0100
Subject: [PATCH] add custom timer class

---
 build.sh             |  2 +-
 demo/main.cpp        | 24 +++++++++++++++++++-----
 fggl/util/chrono.hpp | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 6 deletions(-)
 create mode 100644 fggl/util/chrono.hpp

diff --git a/build.sh b/build.sh
index e2bb125..d6d7f38 100755
--- a/build.sh
+++ b/build.sh
@@ -41,5 +41,5 @@ if [ -x "$(command -v mangohud)" ]; then
 fi
 
 pushd demo
-$EXE ../build/demo/FgglDemo > /tmp/fggl.log 2>&1 &
+$EXE ../build/demo/FgglDemo
 popd
diff --git a/demo/main.cpp b/demo/main.cpp
index 9624350..25aa1c3 100644
--- a/demo/main.cpp
+++ b/demo/main.cpp
@@ -14,6 +14,7 @@
 #include <fggl/ecs/ecs.hpp>
 #include <fggl/debug/debug.h>
 #include <fggl/data/storage.hpp>
+#include <fggl/util/chrono.hpp>
 
 #include <imgui.h>
 
@@ -254,20 +255,29 @@ int main(int argc, char* argv[]) {
 
 	fggl::gfx::Input& input = fggl::gfx::Input::instance();
 
+
 	bool joystickWindow = true;
 	bool gamepadWindow = true;
 
-	float time = 0.0f;
-	float dt = 16.0f;
+	fggl::util::Timer time;
+	time.frequency( glfwGetTimerFrequency() );
+	time.setup( glfwGetTimerValue() );
+
 	while( !win.closeRequested() ) {
+
+		//
+		// Setup setup
+		//
+		time.tick( glfwGetTimerValue() );
+
 		input.frame();
 
 		glfwModule->context.pollEvents();
 		debug.frameStart();
 
+		//
 		// update step
-		time += dt;
-
+		// 
 		process_camera(win, ecs, input, camEnt);
 		if ( cam_mode == cam_arcball ) { 
 			if ( input.mouseDown( fggl::gfx::MOUSE_2 ) ) {
@@ -375,6 +385,8 @@ int main(int argc, char* argv[]) {
 		}
 		ImGui::End();
 
+		debug.showDemo();
+
 /*		float amount = glm::radians( time / 2048.0f * 360.0f );
 		auto spinners = ecs.getEntityWith<fggl::math::Transform>();
 		for ( auto entity : spinners ) {
@@ -382,12 +394,14 @@ int main(int argc, char* argv[]) {
 			transform->euler(glm::vec3(0.0f, amount, 0.0f));
 		}*/
 
+		//
 		// render step
+		// 
 		win.activate();
 		glModule->ogl.clear();
 
 		// model rendering system
-		fggl::gfx::renderMeshes(glModule, ecs, dt);
+		fggl::gfx::renderMeshes(glModule, ecs, time.delta());
 		debug.draw();
 
 		// swap the windows - frame rendering over
diff --git a/fggl/util/chrono.hpp b/fggl/util/chrono.hpp
new file mode 100644
index 0000000..bba1671
--- /dev/null
+++ b/fggl/util/chrono.hpp
@@ -0,0 +1,35 @@
+#ifndef FGGL_UTIL_CHRONO_H
+#define FGGL_UTIL_CHRONO_H
+
+namespace fggl::util {
+
+	class Timer {
+		public:
+			inline void frequency(long freq) {
+				m_freq = freq;
+			}
+
+			inline void setup(long epoch) {
+				m_first = epoch;
+				m_last = m_first;
+				m_curr = m_first;
+			}
+
+			inline void tick(long time) {
+				m_last = m_curr;
+				m_curr = time;
+			}
+
+			inline float delta() const {
+				return (m_curr - m_last) / m_freq;
+			}
+
+		private:
+			float m_freq;
+			float m_first;
+			float m_last;
+			float m_curr;
+	};
+};
+
+#endif
-- 
GitLab