瀏覽代碼

Add code to fill out text cells with data.

Sam Jaffe 6 年之前
父節點
當前提交
6c60af450a
共有 2 個文件被更改,包括 28 次插入3 次删除
  1. 19 1
      engine/src/text_engine.cxx
  2. 9 2
      engine/src/text_engine.hpp

+ 19 - 1
engine/src/text_engine.cxx

@@ -35,4 +35,22 @@ std::unordered_map<char, math::vec2> glyph_locations(std::string const & font) {
 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_)) {}
+      glyphs_(glyph_locations(font_)) {
+  // TODO (sjaffe): Also needs to be programmatic
+  prototype_.frame.size = make_vector(0.125f, 0.125f);
+}
+
+void text_engine::create_text_cells(std::vector<graphics::object> & out,
+                                    cell const & in) {
+  std::size_t const old_size = out.size();
+  std::size_t const new_size = in.content.size();
+  if (old_size != new_size) { out.resize(new_size, prototype_); }
+  for (std::size_t i = old_size; i < new_size; ++i) {
+    auto & obj = out[i];
+    obj.location = {in.origin + make_vector(in.size[0] * i, 0.f), in.size};
+    obj.points = obj.location;
+  }
+  for (std::size_t i = 0; i < new_size; ++i) {
+    out[i].frame.origin = glyphs_.find(in.content[i])->second;
+  }
+}

+ 9 - 2
engine/src/text_engine.hpp

@@ -18,12 +18,19 @@
 
 namespace engine {
   class text_engine {
+  public:
+    struct cell {
+      math::vec2 origin;
+      math::vec2 size;
+      std::string content;
+    };
+
   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 &);
+    void create_text_cells(std::vector<graphics::object> & out,
+                           cell const & in);
 
   private:
     //    std::shared_ptr<graphics::manager const> manager_;