deltatime
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user