浏览代码

Add text engine to the game_dispatch engine.

Sam Jaffe 6 年之前
父节点
当前提交
a71d1a6363

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

@@ -8,7 +8,6 @@
 
 /* Begin PBXBuildFile section */
 		CD1C83562298B55F00825C4E /* fps_counter.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C83542298B55F00825C4E /* fps_counter.cxx */; };
-		CD1C8418229A095600825C4E /* text_engine.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CD1C8416229A095600825C4E /* text_engine.hpp */; };
 		CD1C8419229A095600825C4E /* text_engine.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C8417229A095600825C4E /* text_engine.cxx */; };
 		CD62D8462251A94C0023219A /* libgraphics.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8452251A94C0023219A /* libgraphics.dylib */; };
 		CD62D8482251A9500023219A /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8472251A9500023219A /* libmath.dylib */; };
@@ -70,7 +69,6 @@
 
 /* Begin PBXFileReference section */
 		CD1C83542298B55F00825C4E /* fps_counter.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = fps_counter.cxx; sourceTree = "<group>"; };
-		CD1C8416229A095600825C4E /* text_engine.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = text_engine.hpp; sourceTree = "<group>"; };
 		CD1C8417229A095600825C4E /* text_engine.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = text_engine.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; };
@@ -178,7 +176,6 @@
 				CDB1F8D01D7A32A300700C6B /* events.cpp */,
 				CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */,
 				CD1C83542298B55F00825C4E /* fps_counter.cxx */,
-				CD1C8416229A095600825C4E /* text_engine.hpp */,
 				CD1C8417229A095600825C4E /* text_engine.cxx */,
 				CDB1F8CA1D7A319A00700C6B /* scene.cpp */,
 				CD62FD3E2293746900376440 /* serial.cxx */,
@@ -193,7 +190,6 @@
 			isa = PBXHeadersBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				CD1C8418229A095600825C4E /* text_engine.hpp in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 1 - 0
engine/include/game/engine/engine_fwd.hpp

@@ -23,6 +23,7 @@ namespace engine {
   class fps_counter;
   class game_dispatch;
   class scene;
+  class text_engine;
 
   namespace event {
     struct key_event;

+ 6 - 1
engine/include/game/engine/fps_counter.hpp

@@ -14,10 +14,12 @@
 #include "game/util/time.hpp"
 
 namespace engine {
+  class text_engine;
   class fps_counter {
   public:
     fps_counter(env::clock::duration const & fpscap,
-                graphics::manager const & mgr, std::size_t precision = 3);
+                std::shared_ptr<text_engine> text_engine,
+                std::size_t precision = 3);
     ~fps_counter();
 
     void set_frame_step(env::clock::duration const & since);
@@ -25,9 +27,12 @@ namespace engine {
     std::vector<graphics::object> const & glyphs() const { return glyphs_; }
 
   private:
+    std::string fps(env::clock::duration const & since) const;
+
     int counter_{0}, change_after_{10};
     std::size_t digits_;
     std::size_t magnitude_;
+    std::shared_ptr<text_engine> text_engine_;
     std::vector<graphics::object> glyphs_;
   };
 }

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

@@ -42,6 +42,7 @@ namespace engine {
     std::shared_ptr<graphics::renderer> renderer;
     math::vec2 screen_size;
     env::clock::duration minimum_frame_duration;
+    std::shared_ptr<text_engine> text_engine_;
     std::unique_ptr<fps_counter> counter;
 
     using scene_t = std::shared_ptr<scene>;

engine/src/text_engine.hpp → engine/include/game/engine/text_engine.hpp


+ 13 - 49
engine/src/fps_counter.cxx

@@ -12,65 +12,29 @@
 #include <unordered_map>
 
 #include "game/engine/serial.hpp"
+#include "game/engine/text_engine.hpp"
 #include "game/graphics/object.hpp"
 
 using namespace engine;
 
-static graphics::object create(graphics::manager const & mgr) {
-  Json::Value value;
-  value["material"]["texture"]["file"] = "data/images/font.bmp";
-  value["material"]["texture"]["uniform"] = "u_normalMap";
-  return engine::to_object(value, mgr);
-}
-
-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;
-}
-
-std::unordered_map<char, math::vec2> const indices{
-    {'.', make_vector(6.f, 6.f)}, {'0', make_vector(0.f, 5.f)},
-    {'1', make_vector(1.f, 5.f)}, {'2', make_vector(2.f, 5.f)},
-    {'3', make_vector(3.f, 5.f)}, {'4', make_vector(4.f, 5.f)},
-    {'5', make_vector(5.f, 5.f)}, {'6', make_vector(6.f, 5.f)},
-    {'7', make_vector(7.f, 5.f)}, {'8', make_vector(0.f, 4.f)},
-    {'9', make_vector(1.f, 4.f)}};
-
-math::vec2 index(char value) {
-  auto it = indices.find(value);
-  return it != indices.end() ? it->second : make_vector(0.f, 0.f);
-}
-
-math::vec2 number_index(int num) { return index(char(num + '0')); }
-
 fps_counter::fps_counter(env::clock::duration const & fpscap,
-                         graphics::manager const & mgr, std::size_t precision)
+                         std::shared_ptr<text_engine> text_engine,
+                         std::size_t precision)
     : digits_(precision), magnitude_(std::pow(10, digits_)),
-      glyphs_(digits(fpscap) + digits_ + 1, create(mgr)) {
-  auto pixels = make_vector(0.125f, 0.125f);
-  for (std::size_t i = 0; i < glyphs_.size(); ++i) {
-    glyphs_[i].location.origin =
-        make_vector((glyphs_.size() - i) * 10.f - 5.f, 680.f);
-    glyphs_[i].location.size = make_vector(20.f, 20.f);
-    glyphs_[i].points = glyphs_[i].location;
-    glyphs_[i].frame.size = pixels;
-  }
-  glyphs_[digits_].frame.origin = index('.') * pixels;
-}
+      text_engine_(text_engine), glyphs_() {}
 
 fps_counter::~fps_counter() {}
 
+std::string fps_counter::fps(env::clock::duration const & since) const {
+  auto fps = magnitude_ * env::clock::duration::period::den / since.count();
+  std::string fps_str = std::to_string(fps);
+  fps_str.insert(fps_str.size() - digits_ + 1, ".");
+  return fps_str;
+}
+
 void fps_counter::set_frame_step(env::clock::duration const & since) {
   if (++counter_ != change_after_) { return; }
   counter_ = 0;
-  auto pixels = make_vector(0.125f, 0.125f);
-  auto fps = magnitude_ * env::clock::duration::period::den / since.count();
-  for (std::size_t i = 0; i < glyphs_.size(); ++i) {
-    if (i == digits_) continue;
-    glyphs_[i].frame.origin = number_index(fps % 10) * pixels;
-    fps /= 10;
-  }
+  text_engine_->create_text_cells(
+      glyphs_, {make_vector(5.f, 680.f), make_vector(20.f, 20.f), fps(since)});
 }

+ 3 - 1
engine/src/game_dispatch.cpp

@@ -12,6 +12,7 @@
 #include "game/engine/events.hpp"
 #include "game/engine/fps_counter.hpp"
 #include "game/engine/scene.hpp"
+#include "game/engine/text_engine.hpp"
 #include "game/graphics/object.hpp"
 #include "game/graphics/renderer.hpp"
 #include "game/util/env.hpp"
@@ -27,7 +28,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),
-        counter(new fps_counter(minimum_frame_duration, *renderer->manager())),
+        text_engine_(new text_engine("font", renderer->manager())),
+        counter(new fps_counter(minimum_frame_duration, text_engine_)),
         scenes(), current_timestamp(env::clock::now()), curr_scene() {}
 
   game_dispatch::~game_dispatch() {}

+ 1 - 1
engine/src/text_engine.cxx

@@ -6,7 +6,7 @@
 //  Copyright © 2019 Sam Jaffe. All rights reserved.
 //
 
-#include "text_engine.hpp"
+#include "game/engine/text_engine.hpp"
 
 #include <json/value.h>