Nazaj na multithreadanje - fonti

This commit is contained in:
2026-03-12 16:18:51 +01:00
parent 834f0b29c3
commit 74159a6fda
20 changed files with 254 additions and 43 deletions

31
src/game/gamemanager.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include <game/gamemanager.hpp>
#include <algorithm>
namespace Game {
void GameManager::run(std::stop_token stopToken) {
using namespace std::chrono_literals;
LOG("GameManager thread started");
while (!stopToken.stop_requested()) {
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) {
for (auto& entity : entities) {
entity->update();
}
});
} catch (const std::exception& e) {
ERROR("Exception in GameManager thread: " << e.what());
}
const auto elapsed = std::chrono::steady_clock::now() - frameStart;
const auto remaining = frameDuration - elapsed;
if (remaining > 0s) {
std::this_thread::sleep_for(remaining);
}
}
}
}