deltatime

This commit is contained in:
2026-03-12 21:23:31 +01:00
parent 74159a6fda
commit 4c07694a25
7 changed files with 28 additions and 13 deletions

View File

@@ -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<float> elapsedDt = now - mLastUpdate;
float seconds = elapsedDt.count();
const int updatesPerSecond = std::max(1, mTargetUpdatesPerSecond);
const auto frameDuration = std::chrono::duration<double>(1.0 / static_cast<double>(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) {