From c05b299e016ea55fa21538e6bee1e913221a650d Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 22 May 2022 21:31:44 +0200 Subject: feat: add update speed limit --- src/engine/engine.cpp | 20 ++++++++++++++++++++ src/engine/engine.hpp | 2 ++ 2 files changed, 22 insertions(+) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index fda1fc2..774220a 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -2,6 +2,7 @@ #include "util/function.hpp" +#include #include #include @@ -45,9 +46,28 @@ void CLIGameEngine::start() noexcept _input_handler->listen(); })); + auto last_update_time = std::chrono::system_clock::now(); + + const auto get_millis_since_update = [&last_update_time]() + { + return std::chrono::duration_cast( + std::chrono::system_clock::now() - last_update_time); + }; + while (true) { + const auto time_since_last_update = get_millis_since_update(); + + if (time_since_last_update.count() < MIN_TIME_SINCE_LAST_UPDATE_MILLIS) + { + while (get_millis_since_update().count() < MIN_TIME_SINCE_LAST_UPDATE_MILLIS) + { + } + } + game->on_update(); + + last_update_time = std::chrono::system_clock::now(); } } diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 7b322b5..1e8a217 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -12,6 +12,8 @@ #include #include +constexpr auto MIN_TIME_SINCE_LAST_UPDATE_MILLIS = 100; + class CLIGameEngine : public ICLIGameEngine, public yacppdic::AutoWirable< ICLIGameEngine, -- cgit v1.2.3-18-g5258