Time
- Added Timer - Added DeltaTimer
This commit is contained in:
parent
91a0c92fe7
commit
45c5073deb
3 changed files with 69 additions and 1 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
13
Engine/src/Engine/Time/Timer.cpp
Normal file
13
Engine/src/Engine/Time/Timer.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}
|
37
Engine/src/Engine/Time/Timer.h
Normal file
37
Engine/src/Engine/Time/Timer.h
Normal 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; }
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue