Ver código fonte

Dynamically generate font configurations from a config file instead of having it hard-coded.

Sam Jaffe 6 anos atrás
pai
commit
897ea2c365

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

@@ -15,6 +15,7 @@
 
 namespace engine {
   math::vec2 to_vec2(Json::Value const & json);
+  math::vec2i to_vec2i(Json::Value const & json);
   math::vec2 to_vec2(Json::Value const & json, math::vec2 const & backup);
   graphics::object to_object(Json::Value const & json,
                              graphics::manager const & mgr);

+ 4 - 0
engine/src/serial.cxx

@@ -21,6 +21,10 @@ namespace engine {
     return make_vector(json[0].asFloat(), json[1].asFloat());
   }
 
+  math::vec2i to_vec2i(Json::Value const & json) {
+    return make_vector(json[0].asInt(), json[1].asInt());
+  }
+
   math::vec2 to_vec2(Json::Value const & json, math::vec2 const & backup) {
     return json.isNull() ? backup : to_vec2(json);
   }

+ 24 - 15
engine/src/text_engine.cxx

@@ -8,10 +8,14 @@
 
 #include "game/engine/text_engine.hpp"
 
+#include <fstream>
+#include <iostream>
+#include <json/reader.h>
 #include <json/value.h>
 
 #include "game/engine/serial.hpp"
 #include "game/graphics/manager.hpp"
+#include "game/util/env.hpp"
 
 using namespace engine;
 
@@ -22,26 +26,31 @@ Json::Value font_material(std::string const & font) {
   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)}};
+Json::Value get_font_config(std::string const & font) {
+  Json::Value value;
+  std::string path = env::resource_file("data/images/fonts/" + font + ".json");
+  std::ifstream(path) >> value;
+  return value;
 }
 
 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_)) {
-  // TODO (sjaffe): Also needs to be programmatic
-  auto glyph_size = make_vector(0.125f, 0.125f);
-  // The font I chose is a monospace font where height = 2 * width
-  prototype_.frame.size = glyph_size / make_vector(2, 1);
-  for (auto & pair : glyphs_) {
-    pair.second *= glyph_size;
+      glyphs_() {
+  Json::Value value = get_font_config(font);
+  math::vec2i const cells = to_vec2i(value["cells"]);
+  math::vec2 const cell_size = make_vector(1.f, 1.f) / cells;
+  math::vec2 glyph_size = to_vec2(value["glyph_size"]);
+  glyph_size /= std::max(glyph_size[0], glyph_size[1]);
+  prototype_.frame.size = cell_size * glyph_size;
+
+  for (int i = 0; i < cells[0]; ++i) {
+    for (int j = 0; j < cells[1]; ++j) {
+      std::cout << value["glyphs"][i][j].asCString() << "[" << j << ", " << i
+                << "->" << (cells[1] - i - 1) << "]\n";
+      glyphs_.emplace(*value["glyphs"][i][j].asCString(),
+                      (make_vector(j, cells[1] - i - 1)) * cell_size);
+    }
   }
 }