Browse Source

Add a text engine that mirrors current code in FPS Counter class but is disconnected from the rest of the system currently.

Sam Jaffe 6 years ago
parent
commit
6fcf770fad
3 changed files with 80 additions and 0 deletions
  1. 8 0
      engine/engine.xcodeproj/project.pbxproj
  2. 38 0
      engine/src/text_engine.cxx
  3. 34 0
      engine/src/text_engine.hpp

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

@@ -8,6 +8,8 @@
 
 /* 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 */; };
 		CD62FCCE22904A8900376440 /* libengine.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDB1F8AE1D7A30CD00700C6B /* libengine.dylib */; };
@@ -68,6 +70,8 @@
 
 /* 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; };
 		CD62FCB622904A7B00376440 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
@@ -174,6 +178,8 @@
 				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 */,
 			);
@@ -187,6 +193,7 @@
 			isa = PBXHeadersBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD1C8418229A095600825C4E /* text_engine.hpp in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -352,6 +359,7 @@
 				CDB1F8CC1D7A319A00700C6B /* scene.cpp in Sources */,
 				CDB1F8C81D7A312B00700C6B /* game_dispatch.cpp in Sources */,
 				CD62FD402293746900376440 /* serial.cxx in Sources */,
+				CD1C8419229A095600825C4E /* text_engine.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 38 - 0
engine/src/text_engine.cxx

@@ -0,0 +1,38 @@
+//
+//  text_engine.cxx
+//  engine
+//
+//  Created by Sam Jaffe on 5/25/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "text_engine.hpp"
+
+#include <json/value.h>
+
+#include "game/engine/serial.hpp"
+#include "game/graphics/manager.hpp"
+
+using namespace engine;
+
+Json::Value font_material(std::string const & font) {
+  Json::Value value;
+  value["material"]["texture"]["file"] = "data/images/fonts/" + font + ".bmp";
+  value["material"]["texture"]["uniform"] = "u_normalMap";
+  return value;
+}
+
+std::unordered_map<char, math::vec2> glyph_locations(std::string const & font) {
+  // TODO (sjaffe): Fill out glyphs programmatically (resource file)...
+  return {{'.', 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)}};
+}
+
+text_engine::text_engine(std::string const & font,
+                         std::shared_ptr<graphics::manager const> manager)
+    : font_(font), prototype_(to_object(font_material(font_), *manager)),
+      glyphs_(glyph_locations(font_)) {}

+ 34 - 0
engine/src/text_engine.hpp

@@ -0,0 +1,34 @@
+//
+//  text_engine.hpp
+//  engine
+//
+//  Created by Sam Jaffe on 5/25/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <memory>
+#include <string>
+#include <unordered_map>
+
+#include "game/graphics/object.hpp"
+#include "game/math/math_fwd.hpp"
+#include "vector/vector.hpp"
+
+namespace engine {
+  class text_engine {
+  public:
+    text_engine(std::string const & font,
+                std::shared_ptr<graphics::manager const> manager);
+
+    void create_text_cells(std::vector<graphics::object> &,
+                           std::string const &);
+
+  private:
+    //    std::shared_ptr<graphics::manager const> manager_;
+    std::string font_;
+    graphics::object prototype_;
+    std::unordered_map<char, math::vec2> glyphs_;
+  };
+}