- Added Timer
- Added DeltaTimer
This commit is contained in:
Light 2021-07-09 09:55:05 +04:30
parent 91a0c92fe7
commit 45c5073deb
3 changed files with 69 additions and 1 deletions

View file

@ -11,6 +11,8 @@
#include "UserInterface/UserInterface.h"
#include "Time/Timer.h"
#include <filesystem>
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();
}
}

View file

@ -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<std::chrono::milliseconds>(current - m_Previous)).count()) / 1000.0f;
m_Previous = current;
}
}

View file

@ -0,0 +1,37 @@
#pragma once
#include "Base.h"
#include <chrono>
namespace Light {
class Timer
{
private:
std::chrono::time_point<std::chrono::steady_clock> m_Start;
public:
Timer() : m_Start(std::chrono::steady_clock::now()) { }
inline float GetElapsedTime() const { return (std::chrono::duration_cast<std::chrono::milliseconds>(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<std::chrono::steady_clock> 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; }
};
}