diff options
author | HampusM <hampus@hampusmat.com> | 2022-05-22 21:31:44 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:59 +0200 |
commit | c05b299e016ea55fa21538e6bee1e913221a650d (patch) | |
tree | 0e0ba42dca81bf64726f5fb78c701f7d7552219e /src/engine | |
parent | 7041e81abbdae6b895bda149e5369e0d52dce6ee (diff) |
feat: add update speed limit
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/engine.cpp | 20 | ||||
-rw-r--r-- | src/engine/engine.hpp | 2 |
2 files changed, 22 insertions, 0 deletions
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 <chrono> #include <thread> #include <utility> @@ -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::milliseconds>( + 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 <memory> #include <unordered_map> +constexpr auto MIN_TIME_SINCE_LAST_UPDATE_MILLIS = 100; + class CLIGameEngine : public ICLIGameEngine, public yacppdic::AutoWirable< ICLIGameEngine, |