diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index c8441e6..5546dbc 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -11,6 +11,8 @@ #include "UserInterface/UserInterface.h" +#include "Time/Timer.h" + #include namespace Light { @@ -40,11 +42,24 @@ namespace Light { // reveal window m_Window->SetVisibility(true); + DeltaTimer deltaTimer; + Timer timer; + int frames = 0; + //** GAMELOOP **// while (!m_Window->IsClosed()) { // update layers - m_LayerStack.OnUpdate(60.0f / 10000.0f); // #todo: implement time + m_LayerStack.OnUpdate(deltaTimer.GetDeltaTime()); + + frames++; + if (timer.GetElapsedTime() > 1.0f) + { + LT_ENGINE_INFO(frames); + + frames = 0; + timer.Reset(); + } // render layers m_Window->GetGfxContext()->GetRenderer()->BeginFrame(); @@ -57,6 +72,9 @@ namespace Light { // poll events m_Window->PollEvents(); + + /// update delta time + deltaTimer.Update(); } } diff --git a/Engine/src/Engine/Time/Timer.cpp b/Engine/src/Engine/Time/Timer.cpp new file mode 100644 index 0000000..8a645a0 --- /dev/null +++ b/Engine/src/Engine/Time/Timer.cpp @@ -0,0 +1,13 @@ +#include "ltpch.h" +#include "Timer.h" + +namespace Light { + + void DeltaTimer::Update() + { + auto current = std::chrono::steady_clock::now(); + m_DeltaTime = ((std::chrono::duration_cast(current - m_Previous)).count()) / 1000.0f; + m_Previous = current; + } + +} \ No newline at end of file diff --git a/Engine/src/Engine/Time/Timer.h b/Engine/src/Engine/Time/Timer.h new file mode 100644 index 0000000..84f363f --- /dev/null +++ b/Engine/src/Engine/Time/Timer.h @@ -0,0 +1,37 @@ +#pragma once + +#include "Base.h" + +#include + +namespace Light { + + class Timer + { + private: + std::chrono::time_point m_Start; + + public: + Timer() : m_Start(std::chrono::steady_clock::now()) { } + + inline float GetElapsedTime() const { return (std::chrono::duration_cast(std::chrono::steady_clock::now() - m_Start).count()) / 1000.; } + + inline void Reset() { m_Start = std::chrono::steady_clock::now(); } + }; + + class DeltaTimer + { + private: + std::chrono::time_point m_Previous; + + float m_DeltaTime = 60.0f / 1000.0f; + + public: + DeltaTimer(): m_Previous(std::chrono::steady_clock::now()) { } + + void Update(); + + inline float GetDeltaTime() const { return m_DeltaTime; } + }; + +} \ No newline at end of file