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

View File

@ -30,6 +30,7 @@ private:
std::shared_ptr<Window> m_window; std::shared_ptr<Window> m_window;
std::unique_ptr<Renderer> m_renderer; std::unique_ptr<Renderer> m_renderer;
std::shared_ptr<Scene> m_scene; std::shared_ptr<Scene> m_scene;
uint64_t m_elapsed;
bool m_running; 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/window/event.hpp"
#include "engine/renderer/wavefront.h" #include "engine/renderer/wavefront.h"
#include "engine/time/timestep.h"
namespace Core { namespace Core {
Engine* Engine::s_instance = nullptr; Engine* Engine::s_instance = nullptr;
@ -21,10 +23,18 @@ void Engine::Run(std::unique_ptr<IApplication> app) {
m_window->Subscribe(this); m_window->Subscribe(this);
uint64_t now = SDL_GetPerformanceCounter();
m_elapsed = 0;
while (m_running) { 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_window->ProcessEvents();
m_app->OnUpdate(); m_app->OnUpdate(dt);
m_renderer->Render(); m_renderer->Render();

View File

@ -83,24 +83,14 @@ public:
std::cout << "Game initialized" << std::endl; std::cout << "Game initialized" << std::endl;
m_angle = 3.45f; m_angle = 3.45f;
m_lastTicks = SDL_GetTicks();
m_paused = false; m_paused = false;
m_yaw = -90.0f; // looking along -Z initially m_yaw = -90.0f; // looking along -Z initially
m_pitch = 0.0f; // no vertical tilt m_pitch = 0.0f; // no vertical tilt
// FPS tracking
m_startTicks = SDL_GetTicks();
m_frameCount = 0;
} }
void OnUpdate() override { void OnUpdate(Timestep dt) override {
m_currentTicks = SDL_GetTicks();
float deltaTime = static_cast<float>(m_currentTicks - m_lastTicks) / 1000.0f; // seconds
m_lastTicks = m_currentTicks;
float mouseXRel, mouseYRel; float mouseXRel, mouseYRel;
SDL_GetRelativeMouseState(&mouseXRel, &mouseYRel); SDL_GetRelativeMouseState(&mouseXRel, &mouseYRel);
@ -137,19 +127,19 @@ public:
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f; if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
auto& camTransform = cameraEntity.GetComponent<transform>(); auto& camTransform = cameraEntity.GetComponent<transform>();
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; camTransform.rotation = cameraViewDirection;
// update rotation // update rotation
if (!m_paused) { 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<float>()) { if (m_angle > glm::two_pi<float>()) {
m_angle -= glm::two_pi<float>(); // keep value small m_angle -= glm::two_pi<float>(); // keep value small
} }
} }
// ---- Day-night simulation ---- // ---- Day-night simulation ----
m_dayTime += deltaTime; m_dayTime += dt;
if (m_dayTime > m_dayLength) if (m_dayTime > m_dayLength)
m_dayTime -= m_dayLength; // loop every "day" m_dayTime -= m_dayLength; // loop every "day"
@ -181,23 +171,12 @@ public:
l.intensity = intensity; l.intensity = intensity;
} }
// auto rotateEntts = m_scene->m_registry.view<transform, rotate>(); m_elapsed += dt.GetMilliseconds();
// for (auto [entity, t] : rotateEntts.each()) {
// // auto targetTransform = rotateEntts.get<transform>(entity);
// if (!m_scene->m_registry.all_of<light>(entity)) {
// t.rotation.y = m_angle;
// }
// }
m_frameCount++; if (m_elapsed >= 1000) { // one second passed
m_currentTicks = SDL_GetTicks(); m_elapsed = 0;
Uint64 elapsed = m_currentTicks - m_startTicks; double fps = 1 / dt;
if (elapsed >= 1000) { // one second passed
double fps = static_cast<double>(m_frameCount) / (static_cast<double>(elapsed) / 1000.0);
std::cout << "FPS: " << fps << std::endl; std::cout << "FPS: " << fps << std::endl;
m_frameCount = 0;
m_startTicks = m_currentTicks;
} }
} }
@ -211,6 +190,8 @@ public:
} }
} }
private: private:
int m_elapsed;
std::shared_ptr<Scene> m_scene; std::shared_ptr<Scene> m_scene;
Entity lightEntity; Entity lightEntity;
@ -218,7 +199,6 @@ private:
Entity modelEntity; Entity modelEntity;
float m_angle; float m_angle;
Uint64 m_lastTicks;
float m_dayTime = 0.0f; // accumulates time for day-night cycle float m_dayTime = 0.0f; // accumulates time for day-night cycle
float m_dayLength = 60.0f; // seconds per full day 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_yaw = -90.0f; // looking along -Z initially
float m_pitch = 0.0f; // no vertical tilt float m_pitch = 0.0f; // no vertical tilt
// FPS tracking
Uint64 m_startTicks;
int m_frameCount;
Uint64 m_currentTicks;
}; };
IApplication* CreateApplication() { IApplication* CreateApplication() {