31 lines
1.1 KiB
C++
31 lines
1.1 KiB
C++
#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);
|
|
}
|
|
}
|
|
}
|
|
} |