Pārlūkot izejas kodu

Refactor time to the correct namespace. Add helpers.

Sam Jaffe 6 gadi atpakaļ
vecāks
revīzija
469e086c88

+ 2 - 2
engine/include/game/engine/game_dispatch.hpp

@@ -39,13 +39,13 @@ namespace engine {
 
   private:
     math::vec2 screen_size;
-    duration minimum_frame_duration;
+    env::clock::duration minimum_frame_duration;
 
     using scene_t = std::shared_ptr<scene>;
     std::unordered_map<scene_id_t, scene_t> scenes;
 
     bool running = true;
-    timestamp current_timestamp;
+    env::clock::timestamp current_timestamp;
     struct current_scene_info {
       current_scene_info();
       current_scene_info(scene *);

+ 4 - 10
engine/src/game_dispatch.cpp

@@ -22,9 +22,8 @@ namespace engine {
   }
 
   game_dispatch::game_dispatch()
-      : screen_size(env::screen_size()),
-        minimum_frame_duration(engine::fps::v60), scenes(),
-        current_timestamp(clock::now()), curr_scene() {}
+      : screen_size(env::screen_size()), minimum_frame_duration(env::fps::v60),
+        scenes(), current_timestamp(env::clock::now()), curr_scene() {}
 
   game_dispatch::current_scene_info::current_scene_info() {}
 
@@ -50,17 +49,12 @@ namespace engine {
         {local_scene_position, mask(event::MOUSE_MASK, press)});
   }
 
-  tick game_dispatch::get_tick() {
-    timestamp now = clock::now();
-    return {now, now - current_timestamp};
-  }
-
   float game_dispatch::next_frame() {
-    tick t = get_tick();
+    env::clock::tick t(current_timestamp);
 
     while (t.since < minimum_frame_duration) {
       std::this_thread::sleep_for(minimum_frame_duration - t.since);
-      t = get_tick();
+      t = env::clock::tick(current_timestamp);
     }
 
     current_timestamp = t.now;

+ 2 - 1
graphics/src/opengl_helper.cxx

@@ -22,6 +22,7 @@
 #include "game/graphics/shader.hpp"
 #include "game/util/env.hpp"
 #include "game/util/files.hpp"
+#include "game/util/time.hpp"
 
 namespace {
   typedef void (*GLGetIntFor)(GLuint, GLenum, GLint *);
@@ -320,7 +321,7 @@ namespace graphics { namespace shaders {
     // 103. Set the uniform values, including the texture unit numbers for
     // texture (sampler) uniforms
     // Env::GetCurrentTimeSeconds()
-    glUniform1f(timeUniformLocation, (float)time(NULL));
+    glUniform1f(timeUniformLocation, env::clock::current_time<float>());
     glUniform1f(scale, 2.f);
     glUniform1i(debugWave, 1); // TODO: m_waveEffectOn in ShaderProgram??
     // for GL_TEXTURE0, texture unit 0

+ 1 - 4
graphics/src/opengl_renderer.cxx

@@ -64,10 +64,7 @@ void opengl_renderer::clear() {
   glClearColor(0.f, 0.f, 0.0f, 1.f);
   glEnable(GL_DEPTH_TEST);
 
-  // TODO (sjaffe): Refactor this out...
-  current_time =
-      std::chrono::duration<double>(engine::clock::now().time_since_epoch())
-          .count();
+  current_time = env::clock::current_time<double>();
 
   // TODO (sjaffe): I labeled this as needed before, but I don't know why
   //      I think it has to do with the resolution being 2x the screen size

+ 22 - 14
util/include/game/util/time.hpp

@@ -9,21 +9,29 @@
 
 #include <chrono>
 
-namespace engine {
-  using clock = std::chrono::steady_clock;
-  using timestamp = std::chrono::time_point<clock>;
-  using duration = clock::duration;
+namespace env {
+  namespace clock {
+    using clock_t = std::chrono::steady_clock;
+    using timestamp = std::chrono::time_point<clock_t>;
+    using duration = clock_t::duration;
 
-  struct fps {
-    static duration const v24;
-    static duration const v30;
-    static duration const v60;
-    static duration const v120;
-    static duration const v144;
-  };
+    struct tick {
+      tick(timestamp prev) : now(clock_t::now()), since(now - prev) {}
+      timestamp now;
+      duration since;
+    };
 
-  struct tick {
-    timestamp now;
-    duration since;
+    inline timestamp now() { return clock_t::now(); }
+    template <typename T> T current_time() {
+      return std::chrono::duration<T>(now().time_since_epoch()).count();
+    }
+  }
+
+  struct fps {
+    static clock::duration const v24;
+    static clock::duration const v30;
+    static clock::duration const v60;
+    static clock::duration const v120;
+    static clock::duration const v144;
   };
 }

+ 6 - 7
util/src/time.cpp

@@ -7,13 +7,12 @@
 
 #include "game/util/time.hpp"
 
-namespace engine {
-  using std::chrono::duration_cast;
+namespace env {
   using std::chrono::nanoseconds;
 
-  duration const fps::v24{nanoseconds{41666666}};
-  duration const fps::v30{nanoseconds{33333333}};
-  duration const fps::v60{nanoseconds{16666666}};
-  duration const fps::v120{nanoseconds{8333333}};
-  duration const fps::v144{nanoseconds{6944444}};
+  clock::duration const fps::v24{nanoseconds{41666666}};
+  clock::duration const fps::v30{nanoseconds{33333333}};
+  clock::duration const fps::v60{nanoseconds{16666666}};
+  clock::duration const fps::v120{nanoseconds{8333333}};
+  clock::duration const fps::v144{nanoseconds{6944444}};
 }