From 4c07694a2571577c86b3ef230643719fc584db22 Mon Sep 17 00:00:00 2001 From: DcruBro Date: Thu, 12 Mar 2026 21:23:31 +0100 Subject: [PATCH] deltatime --- include/game/agame/player.hpp | 2 +- include/game/gamemanager.hpp | 3 +++ include/object/entity.hpp | 2 +- src/game/agame/player.cpp | 8 +++++--- src/game/gamemanager.cpp | 12 ++++++++++-- src/main.cpp | 4 ++-- src/renderer/font.cpp | 10 ++++++---- 7 files changed, 28 insertions(+), 13 deletions(-) diff --git a/include/game/agame/player.hpp b/include/game/agame/player.hpp index 3142239..fba2832 100644 --- a/include/game/agame/player.hpp +++ b/include/game/agame/player.hpp @@ -11,6 +11,6 @@ namespace Game::AGame { public: ~Player() override = default; void start() override; - void update() override; + void update(float deltaTime) override; }; } \ No newline at end of file diff --git a/include/game/gamemanager.hpp b/include/game/gamemanager.hpp index 6351e29..cdb0db5 100644 --- a/include/game/gamemanager.hpp +++ b/include/game/gamemanager.hpp @@ -9,6 +9,8 @@ #include namespace Game { + using clock = std::chrono::steady_clock; + class GameManager { public: GameManager() { LOG("Created GameManager"); }; @@ -21,5 +23,6 @@ namespace Game { int getTargetUpdatesPerSecond() { return mTargetUpdatesPerSecond; } private: int mTargetUpdatesPerSecond = 60; + clock::time_point mLastUpdate; }; } \ No newline at end of file diff --git a/include/object/entity.hpp b/include/object/entity.hpp index 135a507..1ab34a4 100644 --- a/include/object/entity.hpp +++ b/include/object/entity.hpp @@ -25,7 +25,7 @@ namespace Game::Object { virtual ~Entity() = 0; virtual void start() = 0; - virtual void update() = 0; + virtual void update(float deltaTime) = 0; void render(Game::Renderer::Renderer* renderer); // Setters and getters diff --git a/src/game/agame/player.cpp b/src/game/agame/player.cpp index a5615b9..8f6b9d2 100644 --- a/src/game/agame/player.cpp +++ b/src/game/agame/player.cpp @@ -4,7 +4,7 @@ namespace Game::AGame { void Player::start() { LOG("Created the Player"); - if (mTex && mTex->getId() == "Arial") { + if (mTex && mTex->getId() == "Roboto") { LOG("Player texture is a font"); // Treat as Font and build it std::shared_ptr font = std::dynamic_pointer_cast(mTex); @@ -16,10 +16,12 @@ namespace Game::AGame { } } - void Player::update() { + void Player::update(float deltaTime) { if (!mIsActive) return; //LOG("Updated Player"); - mTransform.x += 0.5f; // Move right at a constant speed for testing + mTransform.x += 1.f; // Move right at a constant speed for testing mTransform.rotation += 1.f; // Rotate clockwise for testing + //LOG(mName << " position: " << mTransform.x << ' ' << mTransform.y); + //LOG("DeltaTime: " << deltaTime); } } \ No newline at end of file diff --git a/src/game/gamemanager.cpp b/src/game/gamemanager.cpp index ab97f0b..5a82a08 100644 --- a/src/game/gamemanager.cpp +++ b/src/game/gamemanager.cpp @@ -6,21 +6,29 @@ namespace Game { using namespace std::chrono_literals; LOG("GameManager thread started"); + mLastUpdate = clock::now(); // Get the update + while (!stopToken.stop_requested()) { + clock::time_point now = clock::now(); + std::chrono::duration elapsedDt = now - mLastUpdate; + float seconds = elapsedDt.count(); + const int updatesPerSecond = std::max(1, mTargetUpdatesPerSecond); const auto frameDuration = std::chrono::duration(1.0 / static_cast(updatesPerSecond)); const auto frameStart = std::chrono::steady_clock::now(); try { - State::GameState::getInstance().withEntitiesLocked([](auto& entities) { + State::GameState::getInstance().withEntitiesLocked([seconds](auto& entities) { for (auto& entity : entities) { - entity->update(); + entity->update(seconds); } }); } catch (const std::exception& e) { ERROR("Exception in GameManager thread: " << e.what()); } + mLastUpdate = now; + const auto elapsed = std::chrono::steady_clock::now() - frameStart; const auto remaining = frameDuration - elapsed; if (remaining > 0s) { diff --git a/src/main.cpp b/src/main.cpp index b86f0f2..2157edd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,9 +14,9 @@ int main() { Window::Window window = Window::Window(); window.init(1280, 720, "Game Window"); - Object::Transform t1{100.f, 100.f, 0.f, 1.f, 1.f}; + //Object::Transform t1{100.f, 100.f, 0.f, 1.f, 1.f}; State::GameState::getInstance().addEntity(std::make_unique("Player", std::make_shared("../resources/missing_texture.png", window.getRenderer()->getSDLRenderer()), Object::DEFAULT_TRANSFORM)); - State::GameState::getInstance().addEntity(std::make_unique("Player2", std::make_shared("../resources/roboto.ttf", window.getRenderer()->getSDLRenderer(), 128, "Arial"), t1)); + //State::GameState::getInstance().addEntity(std::make_unique("Player2", std::make_shared("../resources/roboto.ttf", window.getRenderer()->getSDLRenderer(), 128, "Roboto"), t1)); window.run(); diff --git a/src/renderer/font.cpp b/src/renderer/font.cpp index e378782..e11ae11 100644 --- a/src/renderer/font.cpp +++ b/src/renderer/font.cpp @@ -15,14 +15,16 @@ namespace Game::Renderer { } } - Font::Font(const Font& other) { - this->mTex = other.mTex; + Font::Font(const Font& other) + : Texture(other) { this->mFont = other.mFont; } Font& Font::operator=(const Font& other) { - this->mTex = other.mTex; - this->mFont = other.mFont; + if (this != &other) { + Texture::operator=(other); + this->mFont = other.mFont; + } return *this; }