From 7b9858cffa904e3324ea0db4c29f2ffc31fc2681 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 23 Oct 2025 16:10:22 +0200 Subject: [PATCH] feat: move fps and deltatime calculations in core engine --- engine/include/engine/app/app.h | 4 ++- engine/include/engine/renderer/core.h | 1 + engine/include/engine/time/timestep.h | 22 +++++++++++++ engine/src/renderer/core.cpp | 12 ++++++- sandbox/src/main.cpp | 46 ++++++--------------------- 5 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 engine/include/engine/time/timestep.h diff --git a/engine/include/engine/app/app.h b/engine/include/engine/app/app.h index 9b361a6..9d1a0be 100644 --- a/engine/include/engine/app/app.h +++ b/engine/include/engine/app/app.h @@ -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) {}; - virtual void OnUpdate() {}; + virtual void OnUpdate(Timestep dt) {}; virtual void OnShutdown() {}; virtual void OnEvent(const Event& event) {}; diff --git a/engine/include/engine/renderer/core.h b/engine/include/engine/renderer/core.h index f8e8513..05d351c 100644 --- a/engine/include/engine/renderer/core.h +++ b/engine/include/engine/renderer/core.h @@ -30,6 +30,7 @@ private: std::shared_ptr m_window; std::unique_ptr m_renderer; std::shared_ptr m_scene; + uint64_t m_elapsed; bool m_running; }; diff --git a/engine/include/engine/time/timestep.h b/engine/include/engine/time/timestep.h new file mode 100644 index 0000000..1f5aed6 --- /dev/null +++ b/engine/include/engine/time/timestep.h @@ -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_ \ No newline at end of file diff --git a/engine/src/renderer/core.cpp b/engine/src/renderer/core.cpp index c944158..e46e4d6 100644 --- a/engine/src/renderer/core.cpp +++ b/engine/src/renderer/core.cpp @@ -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 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(); diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index 836f506..aa8e816 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -83,24 +83,14 @@ public: std::cout << "Game initialized" << std::endl; m_angle = 3.45f; - m_lastTicks = SDL_GetTicks(); m_paused = false; m_yaw = -90.0f; // looking along -Z initially m_pitch = 0.0f; // no vertical tilt - - // FPS tracking - m_startTicks = SDL_GetTicks(); - m_frameCount = 0; } - void OnUpdate() override { - m_currentTicks = SDL_GetTicks(); - float deltaTime = static_cast(m_currentTicks - m_lastTicks) / 1000.0f; // seconds - - m_lastTicks = m_currentTicks; - + void OnUpdate(Timestep dt) override { float mouseXRel, mouseYRel; SDL_GetRelativeMouseState(&mouseXRel, &mouseYRel); @@ -137,19 +127,19 @@ public: if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f; auto& camTransform = cameraEntity.GetComponent(); - camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f + camTransform.position += velocity * (float)dt * 2.5f; // speed is e.g. 2.5f camTransform.rotation = cameraViewDirection; // update rotation if (!m_paused) { - m_angle += glm::radians(45.0f) * deltaTime; // 72° per second + m_angle += glm::radians(45.0f) * dt; // 72° per second if (m_angle > glm::two_pi()) { m_angle -= glm::two_pi(); // keep value small } } // ---- Day-night simulation ---- - m_dayTime += deltaTime; + m_dayTime += dt; if (m_dayTime > m_dayLength) m_dayTime -= m_dayLength; // loop every "day" @@ -181,23 +171,12 @@ public: l.intensity = intensity; } - // auto rotateEntts = m_scene->m_registry.view(); - // for (auto [entity, t] : rotateEntts.each()) { - // // auto targetTransform = rotateEntts.get(entity); - // if (!m_scene->m_registry.all_of(entity)) { - // t.rotation.y = m_angle; - // } - // } + m_elapsed += dt.GetMilliseconds(); - m_frameCount++; - m_currentTicks = SDL_GetTicks(); - Uint64 elapsed = m_currentTicks - m_startTicks; - - if (elapsed >= 1000) { // one second passed - double fps = static_cast(m_frameCount) / (static_cast(elapsed) / 1000.0); + if (m_elapsed >= 1000) { // one second passed + m_elapsed = 0; + double fps = 1 / dt; std::cout << "FPS: " << fps << std::endl; - m_frameCount = 0; - m_startTicks = m_currentTicks; } } @@ -211,6 +190,8 @@ public: } } private: + int m_elapsed; + std::shared_ptr m_scene; Entity lightEntity; @@ -218,7 +199,6 @@ private: Entity modelEntity; float m_angle; - Uint64 m_lastTicks; float m_dayTime = 0.0f; // accumulates time for day-night cycle float m_dayLength = 60.0f; // seconds per full day cycle @@ -227,12 +207,6 @@ private: float m_yaw = -90.0f; // looking along -Z initially float m_pitch = 0.0f; // no vertical tilt - - // FPS tracking - Uint64 m_startTicks; - int m_frameCount; - - Uint64 m_currentTicks; }; IApplication* CreateApplication() {