summary refs log tree commit diff
path: root/src
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
parentf09e5791837bb003f7c5db8c0e3162636bc9a9c2 (diff)
downloadmeowcraft-c0556f76fc5c8271c2eaa7ca91ad1c92c691d8bc.tar.zst
meowcraft-c0556f76fc5c8271c2eaa7ca91ad1c92c691d8bc.zip
Δt calculation and usage
Diffstat (limited to 'src')
-rw-r--r--src/Time.cpp33
-rw-r--r--src/Time.hpp31
-rw-r--r--src/World/Clouds.cpp5
-rw-r--r--src/World/Clouds.hpp3
-rw-r--r--src/World/World.cpp11
-rw-r--r--src/World/World.hpp2
-rw-r--r--src/main.cpp18
7 files changed, 82 insertions, 21 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();
+}
+
+}
diff --git a/src/Time.hpp b/src/Time.hpp
new file mode 100644
index 0000000..83c41c0
--- /dev/null
+++ b/src/Time.hpp
@@ -0,0 +1,31 @@
+#pragma once
+
+#include "Common/Sizes.hpp"
+
+namespace MC {
+
+class Time {
+public:
+    using Timestamp = U64;
+
+    Time() = default;
+
+    void start_frame();
+    void end_frame();
+
+    U64 total_frames() const;
+    Real delta() const;
+
+    static Timestamp now();
+
+private:
+    static constexpr Real delta_max = 1.0 / 10.0;
+    static constexpr Real delta_min = 1.0 / 1000.0;
+
+    U64 m_total_frames = 0;
+
+    Timestamp m_current_frame_start = 0;
+    Real m_delta = 0;
+};
+
+}
diff --git a/src/World/Clouds.cpp b/src/World/Clouds.cpp
index 36f2441..2e0f76c 100644
--- a/src/World/Clouds.cpp
+++ b/src/World/Clouds.cpp
@@ -1,4 +1,5 @@
 #include "Clouds.hpp"
+#include "../Time.hpp"
 #include "../Math/MVP.hpp"
 #include "../Math/Perlin.hpp"
 #include "../Math/AABB.hpp"
@@ -33,8 +34,8 @@ Clouds::Clouds(Real ascept, Real fov, Real near, Real far, Vector<3, F32> sky_co
     m_program.unbind();
 }
 
-void Clouds::update(U64 time) {
-    m_x_offset += time / 5000.0;
+void Clouds::update(const Time& time) {
+    m_x_offset += 5.0 * time.delta();
 }
 
 void Clouds::render(const GFX::Camera& camera) const {
diff --git a/src/World/Clouds.hpp b/src/World/Clouds.hpp
index b2d5a10..35e7155 100644
--- a/src/World/Clouds.hpp
+++ b/src/World/Clouds.hpp
@@ -1,5 +1,6 @@
 #pragma once
 
+#include "../Time.hpp"
 #include "../GFX/Binder.hpp"
 #include "../GFX/Shading/Program.hpp"
 #include "../GFX/Shading/Uniform.hpp"
@@ -11,7 +12,7 @@ class Clouds {
 public:
     Clouds(Real ascept, Real fov, Real near, Real far, Vector<3, F32> sky_color, Vector<3, F32> sun_direction);
 
-    void update(U64 time);
+    void update(const Time& time);
     void render(const GFX::Camera& camera) const;
 private:
     constexpr static U32 CloudMatrixSize = 128;
diff --git a/src/World/World.cpp b/src/World/World.cpp
index de80afe..8685e8a 100644
--- a/src/World/World.cpp
+++ b/src/World/World.cpp
@@ -1,5 +1,6 @@
 #include "World.hpp"
 #include "Generation/ChunkMeshing.hpp"
+#include "../Time.hpp"
 
 namespace MC::World {
 
@@ -71,9 +72,9 @@ void World::load_finished_chunks_from_queue() {
 
 void World::request_generation(ChunkIndex index, Real priority) {
     m_queue.add(index, priority, [=]() -> GenerationResult {
-        auto start = timestamp();
+        auto start = Time::now();
         auto chunk = m_generator.generate(index.x, index.y);
-        return {chunk, timestamp() - start};
+        return {chunk, Time::now() - start};
     });
 }
 
@@ -89,12 +90,6 @@ World::ChunkData& World::get(ChunkIndex index) {
     return entry->second;
 }
 
-U64 World::timestamp() {
-    auto time = std::chrono::system_clock::now().time_since_epoch();
-    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(time);
-    return ms.count();
-}
-
 void World::try_to_create_mesh_for_chunk(ChunkData& data) {
     auto index = data.index;
 
diff --git a/src/World/World.hpp b/src/World/World.hpp
index ef8b7af..8a11dbe 100644
--- a/src/World/World.hpp
+++ b/src/World/World.hpp
@@ -47,8 +47,6 @@ private:
 
     ChunkData& get(ChunkIndex index);
 
-    static U64 timestamp();
-
     U8 m_view_distance_radius = 10;
 
     struct GenerationResult {
diff --git a/src/main.cpp b/src/main.cpp
index 6e78dab..1c6aa27 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2,6 +2,7 @@
 #include <GL/glew.h>
 #include <GLFW/glfw3.h>
 
+#include "Time.hpp"
 #include "Common/Sizes.hpp"
 #include "GFX/Window.hpp"
 #include "GFX/Camera.hpp"
@@ -23,7 +24,7 @@
 
 void run();
 void render(const MC::GFX::BindableMesh&, const MC::GFX::Texture&);
-void process_input(MC::GFX::Window&, MC::GFX::Camera&);
+void process_input(MC::GFX::Window&, MC::GFX::Camera&, MC::Time&);
 void setup_gl();
 void fix_macos_render(const MC::GFX::Window&);
 
@@ -93,16 +94,17 @@ void run() {
     glFrontFace(GL_CCW);
     glCullFace(GL_BACK);
 
-    U64 time = 0;
+    MC::Time time;
 
     while (!window.should_close()) {
+        time.start_frame();
         window.start_frame();
 
 #ifdef __APPLE__
         fix_macos_render(window);
 #endif
 
-        process_input(window, camera);
+        process_input(window, camera, time);
         clouds.update(time);
 
         glClearColor(sky_color.x(), sky_color.y(), sky_color.z(), 1.0f); // #DBDBDB
@@ -129,7 +131,7 @@ void run() {
 
         clouds.render(camera);
 
-        time++;
+        time.end_frame();
     }
 }
 
@@ -141,7 +143,7 @@ void render(const MC::GFX::BindableMesh& mesh, const MC::GFX::Texture& texture)
     texture.unbind();
 }
 
-void process_input(MC::GFX::Window& window, MC::GFX::Camera& camera) {
+void process_input(MC::GFX::Window& window, MC::GFX::Camera& camera, MC::Time& time) {
     if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) {
         window.close();
     }
@@ -153,10 +155,10 @@ void process_input(MC::GFX::Window& window, MC::GFX::Camera& camera) {
     Real x = key(GLFW_KEY_D) - key(GLFW_KEY_A);
     Real y = key(GLFW_KEY_SPACE) - key(GLFW_KEY_LEFT_SHIFT);
     Real z = key(GLFW_KEY_S) - key(GLFW_KEY_W);
-    Real boost = key(GLFW_KEY_LEFT_CONTROL) * 2.0f;
+    Real boost = key(GLFW_KEY_LEFT_CONTROL) * 75.0f;
 
-    auto move_speed = 0.2f + boost;
-    auto rotation_speed = 0.1f;
+    auto move_speed = (20.0f + boost) * time.delta();
+    auto rotation_speed = 5.0f * time.delta();
 
     camera.move_relative({x * move_speed, 0.0f, z * move_speed});
     camera.move({0.0f, y * move_speed, 0.0f});