Nazaj na multithreadanje - fonti
This commit is contained in:
31
src/game/gamemanager.cpp
Normal file
31
src/game/gamemanager.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user