aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/engine/engine.cpp20
-rw-r--r--src/engine/engine.hpp2
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,