Ver Fonte

Add an FPS Counter object.
TODO: Actually write text.

Sam Jaffe há 6 anos atrás
pai
commit
b701621c33

+ 4 - 0
engine/engine.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		CD1C83562298B55F00825C4E /* fps_counter.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C83542298B55F00825C4E /* fps_counter.cxx */; };
 		CD62D8462251A94C0023219A /* libgraphics.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8452251A94C0023219A /* libgraphics.dylib */; };
 		CD62D8482251A9500023219A /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8472251A9500023219A /* libmath.dylib */; };
 		CD62FCCE22904A8900376440 /* libengine.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDB1F8AE1D7A30CD00700C6B /* libengine.dylib */; };
@@ -66,6 +67,7 @@
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
+		CD1C83542298B55F00825C4E /* fps_counter.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = fps_counter.cxx; sourceTree = "<group>"; };
 		CD62D8452251A94C0023219A /* libgraphics.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libgraphics.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD62D8472251A9500023219A /* libmath.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libmath.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD62FCB622904A7B00376440 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
@@ -171,6 +173,7 @@
 				CD62FD33229364DB00376440 /* entity.cxx */,
 				CDB1F8D01D7A32A300700C6B /* events.cpp */,
 				CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */,
+				CD1C83542298B55F00825C4E /* fps_counter.cxx */,
 				CDB1F8CA1D7A319A00700C6B /* scene.cpp */,
 				CD62FD3E2293746900376440 /* serial.cxx */,
 			);
@@ -345,6 +348,7 @@
 			files = (
 				CD62FD35229364DB00376440 /* entity.cxx in Sources */,
 				CDB1F8D21D7A32A300700C6B /* events.cpp in Sources */,
+				CD1C83562298B55F00825C4E /* fps_counter.cxx in Sources */,
 				CDB1F8CC1D7A319A00700C6B /* scene.cpp in Sources */,
 				CDB1F8C81D7A312B00700C6B /* game_dispatch.cpp in Sources */,
 				CD62FD402293746900376440 /* serial.cxx in Sources */,

+ 34 - 0
engine/include/game/engine/fps_counter.hpp

@@ -0,0 +1,34 @@
+//
+//  fps_counter.hpp
+//  engine
+//
+//  Created by Sam Jaffe on 5/24/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <vector>
+
+#include "game/util/time.hpp"
+
+namespace graphics {
+  class object;
+}
+
+namespace engine {
+  class fps_counter {
+  public:
+    fps_counter(env::clock::duration const & fpscap, std::size_t precision = 3);
+    ~fps_counter();
+
+    void set_frame_step(env::clock::duration const & since);
+
+    std::vector<graphics::object> const & glyphs() const { return glyphs_; }
+
+  private:
+    std::size_t digits_;
+    std::size_t magnitude_;
+    std::vector<graphics::object> glyphs_;
+  };
+}

+ 3 - 1
engine/include/game/engine/game_dispatch.hpp

@@ -16,6 +16,7 @@
 
 #include "engine_fwd.hpp"
 #include "events.hpp"
+#include "game/engine/fps_counter.hpp"
 #include "game/util/time.hpp"
 
 namespace graphics {
@@ -38,13 +39,14 @@ namespace engine {
     void render();
 
   private:
-    float next_frame();
+    env::clock::tick next_frame();
     tick get_tick();
 
   private:
     std::shared_ptr<graphics::renderer> renderer;
     math::vec2 screen_size;
     env::clock::duration minimum_frame_duration;
+    fps_counter counter;
 
     using scene_t = std::shared_ptr<scene>;
     std::unordered_map<scene_id_t, scene_t> scenes;

+ 62 - 0
engine/src/fps_counter.cxx

@@ -0,0 +1,62 @@
+//
+//  fps_counter.cxx
+//  engine
+//
+//  Created by Sam Jaffe on 5/24/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "game/engine/fps_counter.hpp"
+
+#include <json/value.h>
+
+#include "game/engine/serial.hpp"
+#include "game/graphics/object.hpp"
+#include <iostream>
+
+using namespace engine;
+
+static graphics::object create() {
+  Json::Value value;
+  value["material"]["texture"]["file"] = "data/images/rock.png";
+  value["material"]["texture"]["uniform"] = "u_normalMap";
+  return engine::to_object(value);
+}
+
+static std::size_t digits(env::clock::duration const & fpscap) {
+  auto fps = env::clock::duration::period::den / fpscap.count();
+  std::size_t digits = 1;
+  while (fps /= 10)
+    ++digits;
+  return digits;
+}
+
+fps_counter::fps_counter(env::clock::duration const & fpscap,
+                         std::size_t precision)
+    : digits_(precision), magnitude_(std::pow(10, digits_)),
+      glyphs_(digits(fpscap) + digits_ + 1, create()) {
+  auto offset = make_vector(0.f, 44.f);
+  auto pixels = make_vector(12.f, 16.f);
+  auto bounds = make_vector(512.f, 64.f);
+  auto glyph_size = pixels / bounds;
+  for (std::size_t i = 0; i < glyphs_.size(); ++i) {
+    glyphs_[i].location.origin = make_vector((i + 1) * 10.f, 10.f);
+    glyphs_[i].location.size = make_vector(10.f, 10.f);
+    glyphs_[i].points = glyphs_[i].location;
+    glyphs_[i].frame.size = glyph_size;
+  }
+  glyphs_[digits_].frame.origin = (offset + 10 * pixels) / bounds;
+}
+
+fps_counter::~fps_counter() {}
+
+void fps_counter::set_frame_step(env::clock::duration const & since) {
+  auto offset = make_vector(0.f, 48.f);
+  auto pixels = make_vector(12.f, 16.f);
+  auto bounds = make_vector(512.f, 64.f);
+  auto fps = magnitude_ * env::clock::duration::period::den / since.count();
+  for (std::size_t i = 0; i < glyphs_.size(); ++i, fps /= 10) {
+    if (i == digits_) continue;
+    glyphs_[i].frame.origin = (offset + (fps % 10) * pixels) / bounds;
+  }
+}

+ 14 - 6
engine/src/game_dispatch.cpp

@@ -11,6 +11,7 @@
 
 #include "game/engine/events.hpp"
 #include "game/engine/scene.hpp"
+#include "game/graphics/object.hpp"
 #include "game/graphics/renderer.hpp"
 #include "game/util/env.hpp"
 
@@ -24,8 +25,8 @@ namespace engine {
 
   game_dispatch::game_dispatch(std::shared_ptr<graphics::renderer> renderer)
       : renderer(renderer), screen_size(env::screen_size()),
-        minimum_frame_duration(env::fps::v60), scenes(),
-        current_timestamp(env::clock::now()), curr_scene() {}
+        minimum_frame_duration(env::fps::v60), counter(minimum_frame_duration),
+        scenes(), current_timestamp(env::clock::now()), curr_scene() {}
 
   game_dispatch::current_scene_info::current_scene_info() {}
 
@@ -51,7 +52,7 @@ namespace engine {
         {local_scene_position, mask(event::MOUSE_MASK, press)});
   }
 
-  float game_dispatch::next_frame() {
+  env::clock::tick game_dispatch::next_frame() {
     env::clock::tick t(current_timestamp);
 
     while (t.since < minimum_frame_duration) {
@@ -60,13 +61,20 @@ namespace engine {
     }
 
     current_timestamp = t.now;
-    return std::chrono::duration<float>(t.since).count();
+    return t;
   }
 
-  void game_dispatch::update() { curr_scene.ptr->update(next_frame()); }
+  void game_dispatch::update() {
+    env::clock::tick t = next_frame();
+    counter.set_frame_step(t.since);
+    //    curr_scene.ptr->update(std::chrono::duration<float>(t.since).count());
+  }
 
   void game_dispatch::render() {
     graphics::batch_renderer batch(renderer.get());
-    curr_scene.ptr->render(batch);
+    for (auto & obj : counter.glyphs()) {
+      batch.draw(obj);
+    }
+    //    curr_scene.ptr->render(batch);
   }
 }