From ad98d7dc497a4f2bf443923bb64440103ca7642e Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Wed, 15 Jun 2022 01:01:00 -0400 Subject: [PATCH] Added in frame timestep, should be used in some way with scalers to generate rates --- NavProject/src/SPSInputLayer.cpp | 2 +- NavProject/src/SPSInputLayer.h | 2 +- Navigator/src/Navigator/Core/Application.cpp | 12 ++++++++- Navigator/src/Navigator/Core/Layer.h | 3 ++- Navigator/src/Navigator/Core/Timestep.h | 25 +++++++++++++++++++ .../src/Navigator/Editor/EditorLayer.cpp | 2 +- Navigator/src/Navigator/Editor/EditorLayer.h | 2 +- .../src/Navigator/Physics/PhysicsLayer.cpp | 2 +- .../src/Navigator/Physics/PhysicsLayer.h | 2 +- .../src/Navigator/Renderer/RenderCommand.h | 1 + .../src/Navigator/Renderer/RendererAPI.h | 1 + .../src/Navigator/Utils/TestServerLayer.cpp | 2 +- .../src/Navigator/Utils/TestServerLayer.h | 2 +- .../src/Platform/OpenGL/OpenGLRendererAPI.cpp | 6 +++++ .../src/Platform/OpenGL/OpenGLRendererAPI.h | 1 + 15 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 Navigator/src/Navigator/Core/Timestep.h diff --git a/NavProject/src/SPSInputLayer.cpp b/NavProject/src/SPSInputLayer.cpp index 910fe4a..245fc77 100644 --- a/NavProject/src/SPSInputLayer.cpp +++ b/NavProject/src/SPSInputLayer.cpp @@ -39,7 +39,7 @@ namespace Navigator { void SPSInputLayer::OnDetach() {} - void SPSInputLayer::OnUpdate() {} + void SPSInputLayer::OnUpdate(Timestep& step) {} void SPSInputLayer::OnEvent(Event& event) {} diff --git a/NavProject/src/SPSInputLayer.h b/NavProject/src/SPSInputLayer.h index 032598d..6cc4575 100644 --- a/NavProject/src/SPSInputLayer.h +++ b/NavProject/src/SPSInputLayer.h @@ -27,7 +27,7 @@ namespace Navigator { virtual void OnAttach() override; virtual void OnDetach() override; - virtual void OnUpdate() override; + virtual void OnUpdate(Timestep& step) override; virtual void OnEvent(Event& event) override; //If you want to respond to events virtual void OnImGuiRender() override; //"Main" function diff --git a/Navigator/src/Navigator/Core/Application.cpp b/Navigator/src/Navigator/Core/Application.cpp index 5868a95..7698742 100644 --- a/Navigator/src/Navigator/Core/Application.cpp +++ b/Navigator/src/Navigator/Core/Application.cpp @@ -11,6 +11,7 @@ #include "Application.h" #include "Navigator/Renderer/RenderCommand.h" #include "Navigator/Editor/EditorLayer.h" +#include "Timestep.h" namespace Navigator { @@ -77,13 +78,22 @@ namespace Navigator { void Application::Run() { NAV_PROFILE_FUNCTION(); + + float lastFrameTime = 0; + float time; + Timestep step; + while(m_runFlag) { RenderCommand::SetClearColor(m_bckgnd_color); RenderCommand::Clear(); + time = RenderCommand::GetFrameTime(); + step.SetTime(time - lastFrameTime); + lastFrameTime = time; + for(auto layer : m_stack) - layer->OnUpdate(); + layer->OnUpdate(step); m_imgui_layer->Begin(); for(auto layer : m_stack) diff --git a/Navigator/src/Navigator/Core/Layer.h b/Navigator/src/Navigator/Core/Layer.h index fa0219c..0c6ed74 100644 --- a/Navigator/src/Navigator/Core/Layer.h +++ b/Navigator/src/Navigator/Core/Layer.h @@ -9,6 +9,7 @@ #include "NavCore.h" #include "Navigator/Events/Event.h" +#include "Timestep.h" namespace Navigator { @@ -21,7 +22,7 @@ namespace Navigator { virtual void OnAttach() {} virtual void OnDetach() {} virtual void OnImGuiRender() {} - virtual void OnUpdate() {} + virtual void OnUpdate(Timestep& step) {} virtual void OnEvent(Event& event) {} inline const std::string& GetName() { return m_name; } diff --git a/Navigator/src/Navigator/Core/Timestep.h b/Navigator/src/Navigator/Core/Timestep.h new file mode 100644 index 0000000..59e7d27 --- /dev/null +++ b/Navigator/src/Navigator/Core/Timestep.h @@ -0,0 +1,25 @@ +#ifndef TIMESTEP_H +#define TIMESTEP_H + +namespace Navigator { + + class Timestep + { + public: + Timestep(float time=0.0f) : + m_time(time) + { + } + + void SetTime(float time) { m_time = time; } + + operator float() { return m_time; } + + float GetElapsedSeconds() const { return m_time; } + float GetElapsedMilliseconds() const { return m_time * 1000.0f; } + private: + float m_time; + }; +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/Editor/EditorLayer.cpp b/Navigator/src/Navigator/Editor/EditorLayer.cpp index 43d14bd..d4727ab 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.cpp +++ b/Navigator/src/Navigator/Editor/EditorLayer.cpp @@ -38,7 +38,7 @@ namespace Navigator { { } - void EditorLayer::OnUpdate() + void EditorLayer::OnUpdate(Timestep& step) { } diff --git a/Navigator/src/Navigator/Editor/EditorLayer.h b/Navigator/src/Navigator/Editor/EditorLayer.h index 4b2a603..cb21f87 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.h +++ b/Navigator/src/Navigator/Editor/EditorLayer.h @@ -33,7 +33,7 @@ namespace Navigator { virtual void OnAttach() override; virtual void OnDetach() override; virtual void OnImGuiRender() override; - virtual void OnUpdate() override; + virtual void OnUpdate(Timestep& step) override; virtual void OnEvent(Event& event) override; diff --git a/Navigator/src/Navigator/Physics/PhysicsLayer.cpp b/Navigator/src/Navigator/Physics/PhysicsLayer.cpp index 6f88d61..1a41b42 100644 --- a/Navigator/src/Navigator/Physics/PhysicsLayer.cpp +++ b/Navigator/src/Navigator/Physics/PhysicsLayer.cpp @@ -87,7 +87,7 @@ namespace Navigator { m_physStack.PushStage(stage); } - void PhysicsLayer::OnUpdate() {} + void PhysicsLayer::OnUpdate(Timestep& step) {} /*Threaded functions*/ diff --git a/Navigator/src/Navigator/Physics/PhysicsLayer.h b/Navigator/src/Navigator/Physics/PhysicsLayer.h index 2a23fc3..6408409 100644 --- a/Navigator/src/Navigator/Physics/PhysicsLayer.h +++ b/Navigator/src/Navigator/Physics/PhysicsLayer.h @@ -31,7 +31,7 @@ namespace Navigator { virtual ~PhysicsLayer(); virtual void OnAttach() override; - virtual void OnUpdate() override; + virtual void OnUpdate(Timestep& step) override; virtual void OnDetach() override; virtual void OnImGuiRender() override {}; virtual void OnEvent(Event& event) override; diff --git a/Navigator/src/Navigator/Renderer/RenderCommand.h b/Navigator/src/Navigator/Renderer/RenderCommand.h index 8095a88..28b0162 100644 --- a/Navigator/src/Navigator/Renderer/RenderCommand.h +++ b/Navigator/src/Navigator/Renderer/RenderCommand.h @@ -18,6 +18,7 @@ namespace Navigator { public: inline static void SetClearColor(const glm::vec4& color_array) { s_api->SetClearColor(color_array); } inline static void Clear() { s_api->Clear(); } + inline static float GetFrameTime() { return s_api->GetFrameTime(); } private: static RendererAPI* s_api; diff --git a/Navigator/src/Navigator/Renderer/RendererAPI.h b/Navigator/src/Navigator/Renderer/RendererAPI.h index ab464d5..a8de3e4 100644 --- a/Navigator/src/Navigator/Renderer/RendererAPI.h +++ b/Navigator/src/Navigator/Renderer/RendererAPI.h @@ -24,6 +24,7 @@ namespace Navigator { virtual void Clear() = 0; virtual void SetClearColor(const glm::vec4& color) = 0; + virtual float GetFrameTime() = 0; inline static API GetAPI() { return s_api; } diff --git a/Navigator/src/Navigator/Utils/TestServerLayer.cpp b/Navigator/src/Navigator/Utils/TestServerLayer.cpp index 78d249d..2770c8c 100644 --- a/Navigator/src/Navigator/Utils/TestServerLayer.cpp +++ b/Navigator/src/Navigator/Utils/TestServerLayer.cpp @@ -314,7 +314,7 @@ namespace Navigator { } //Tell to write, then poll actions - void TestServerLayer::OnUpdate() + void TestServerLayer::OnUpdate(Timestep& step) { m_connection->Start(); m_context.poll(); diff --git a/Navigator/src/Navigator/Utils/TestServerLayer.h b/Navigator/src/Navigator/Utils/TestServerLayer.h index 00867ad..d54ec55 100644 --- a/Navigator/src/Navigator/Utils/TestServerLayer.h +++ b/Navigator/src/Navigator/Utils/TestServerLayer.h @@ -66,7 +66,7 @@ namespace Navigator { virtual void OnAttach() override; virtual void OnDetach() override; - virtual void OnUpdate() override; + virtual void OnUpdate(Timestep& step) override; private: asio::io_context m_context; diff --git a/Navigator/src/Platform/OpenGL/OpenGLRendererAPI.cpp b/Navigator/src/Platform/OpenGL/OpenGLRendererAPI.cpp index cc81730..9418d2e 100644 --- a/Navigator/src/Platform/OpenGL/OpenGLRendererAPI.cpp +++ b/Navigator/src/Platform/OpenGL/OpenGLRendererAPI.cpp @@ -8,6 +8,7 @@ #include "OpenGLRendererAPI.h" #include "glad/glad.h" +#include "GLFW/glfw3.h" namespace Navigator { @@ -20,4 +21,9 @@ namespace Navigator { { glClearColor(color_array[0], color_array[1], color_array[2], color_array[3]); } + + float OpenGLRendererAPI::GetFrameTime() + { + return (float)glfwGetTime(); + } } \ No newline at end of file diff --git a/Navigator/src/Platform/OpenGL/OpenGLRendererAPI.h b/Navigator/src/Platform/OpenGL/OpenGLRendererAPI.h index 21e8118..cb4f193 100644 --- a/Navigator/src/Platform/OpenGL/OpenGLRendererAPI.h +++ b/Navigator/src/Platform/OpenGL/OpenGLRendererAPI.h @@ -18,6 +18,7 @@ namespace Navigator { public: virtual void Clear() override; virtual void SetClearColor(const glm::vec4& color_array) override; + virtual float GetFrameTime() override; }; }