feat: move fps and deltatime calculations in core engine

This commit is contained in:
2025-10-23 16:10:22 +02:00
parent ef498ba210
commit 7b9858cffa
5 changed files with 47 additions and 38 deletions

View File

@ -3,6 +3,8 @@
#include "engine/scene/scene.h"
#include "engine/window/event.hpp"
#include "engine/time/timestep.h"
#include "engine/export.h"
namespace Core {
@ -11,7 +13,7 @@ namespace Core {
virtual ~IApplication() = default;
virtual void OnInit(std::shared_ptr<Scene> scene) {};
virtual void OnUpdate() {};
virtual void OnUpdate(Timestep dt) {};
virtual void OnShutdown() {};
virtual void OnEvent(const Event& event) {};

View File

@ -30,6 +30,7 @@ private:
std::shared_ptr<Window> m_window;
std::unique_ptr<Renderer> m_renderer;
std::shared_ptr<Scene> m_scene;
uint64_t m_elapsed;
bool m_running;
};

View File

@ -0,0 +1,22 @@
#ifndef TIME_TIMESTEP_H_
#define TIME_TIMESTEP_H_
namespace Core {
class Timestep {
public:
Timestep(float time = 0.0f)
: m_time(time) {}
[[nodiscard]] float GetSeconds() const { return m_time; }
[[nodiscard]] float GetMilliseconds() const { return m_time * 1000.f; }
operator float() const { return m_time; }
public:
static Timestep FromMilliseconds(float milliseconds) { return Timestep(milliseconds * 0.001f); }
static Timestep FromSeconds(float seconds) { return Timestep(seconds); }
private:
float m_time;
};
}
#endif // TIME_TIMESTEP_H_

View File

@ -5,6 +5,8 @@
#include "engine/window/event.hpp"
#include "engine/renderer/wavefront.h"
#include "engine/time/timestep.h"
namespace Core {
Engine* Engine::s_instance = nullptr;
@ -21,10 +23,18 @@ void Engine::Run(std::unique_ptr<IApplication> app) {
m_window->Subscribe(this);
uint64_t now = SDL_GetPerformanceCounter();
m_elapsed = 0;
while (m_running) {
m_elapsed = now;
now = SDL_GetPerformanceCounter();
auto dt = Timestep::FromMilliseconds((float)((now - m_elapsed)*1000 / (float)SDL_GetPerformanceFrequency()));
m_window->ProcessEvents();
m_app->OnUpdate();
m_app->OnUpdate(dt);
m_renderer->Render();