summary refs log tree commit diff
path: root/src/Time.cpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2023-07-12 22:57:53 +0200
committerMel <einebeere@gmail.com>2023-07-12 22:58:34 +0200
commitc0556f76fc5c8271c2eaa7ca91ad1c92c691d8bc (patch)
treea7c10af8e912ae0fa4aec58b15d8a6496a288e4d /src/Time.cpp
parentf09e5791837bb003f7c5db8c0e3162636bc9a9c2 (diff)
downloadmeowcraft-c0556f76fc5c8271c2eaa7ca91ad1c92c691d8bc.tar.zst
meowcraft-c0556f76fc5c8271c2eaa7ca91ad1c92c691d8bc.zip
Δt calculation and usage
Diffstat (limited to 'src/Time.cpp')
-rw-r--r--src/Time.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/Time.cpp b/src/Time.cpp
new file mode 100644
index 0000000..7f59250
--- /dev/null
+++ b/src/Time.cpp
@@ -0,0 +1,33 @@
+#include "Time.hpp"
+#include <chrono>
+#include <algorithm>
+
+namespace MC {
+
+void Time::start_frame() {
+    m_current_frame_start = now();
+}
+
+void Time::end_frame() {
+    auto frame_end = now();
+    m_delta = (frame_end - m_current_frame_start) / 1000.0;
+    m_delta = std::clamp(m_delta, delta_min, delta_max);
+
+    m_total_frames++;
+}
+
+U64 Time::total_frames() const {
+    return m_total_frames;
+}
+
+Real Time::delta() const {
+    return m_delta;
+}
+
+Time::Timestamp Time::now() {
+    auto time = std::chrono::system_clock::now().time_since_epoch();
+    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(time);
+    return ms.count();
+}
+
+}