]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
text rendering in-place; but something's missing
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 17 Sep 2019 19:36:26 +0000 (15:36 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 17 Sep 2019 19:36:26 +0000 (15:36 -0400)
Scripts/init.lua
src/engine.cpp
src/render.hpp
src/text.cpp
src/text.hpp

index 65513ec97f0aceb4f025318668e89c70c2c67835..da9ba14a42b62d534170a0cd8c35fcc0592cd525 100644 (file)
@@ -1,4 +1,4 @@
-game.loadFont("freepixel16", "Assets/FreePixel.ttf", 16)
+game.loadFont("default", "Assets/FreePixel.ttf", 16)
 
 bird = {
     Player = 0,
@@ -9,6 +9,7 @@ bird = {
         end,
         MoveLeftReleased = function(self)
             self.Velocity.x = self.Velocity.x + 100
+            game.puts("default", self.Position.x, self.Position.y, "Hey.")
         end,
         MoveRightPressed = function(self)
             self.Velocity.x = self.Velocity.x + 100
index 70b0b45404a0e73f1a25eb08801f9caaf8c7dcab..703b45e248eb5478f65da62141c75f994e0e7cd5 100644 (file)
@@ -58,6 +58,10 @@ int Engine::init(void)
     [this](std::string name, std::string file, int size) {
         systems.system<TextSystem>().get()->loadFont(name, file, size);
     });
+    script->addToGameNamespace("puts",
+    [this](std::string name, float x, float y, std::string text) {
+        systems.system<TextSystem>().get()->put(name, x, y, text);
+    });
     script->init();
     
 
@@ -138,6 +142,7 @@ void Engine::renderLoop(void)
 {
     entityx::TimeDelta dt = 0; /**< Elapsed milliseconds since each loop */
     while (shouldRun()) {
+        systems.update<TextSystem>(dt);
         systems.update<RenderSystem>(dt);
     }
 }
index d74aca4d602b1da9bd983e7e5746e70a5621e790..3f632f142b3926d707bf280d7db8c0145f9e151e 100644 (file)
@@ -67,6 +67,7 @@ private:
 
     // Map of VBOs and their render data
     std::map<GLuint, RenderData> renders;
+
 public:
     RenderSystem() :
         window(nullptr, SDL_DestroyWindow) {}
index 186907f61d2cd9f5ed8a782720e99942e2e6981d..1381eb2563ad5927bd338db84ea004a1aa52fb1d 100644 (file)
@@ -1,5 +1,7 @@
 #include "text.hpp"
 
+#include "events/render.hpp"
+
 #include <iostream>
 
 void TextSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
@@ -8,16 +10,30 @@ void TextSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
     if (FT_Init_FreeType(&freetype) != 0) {
         // TODO handle error
     }
+
+    shouldUpdateVBOs = false;
 }
     
 /**
  * Draws the text for all entities.
  */
 void TextSystem::update([[maybe_unused]] entityx::EntityManager& entites,
-                        [[maybe_unused]] entityx::EventManager& events,
+                        entityx::EventManager& events,
                         [[maybe_unused]] entityx::TimeDelta dt)
 {
-    // TODO render each Text component's text
+    if (shouldUpdateVBOs) {
+        shouldUpdateVBOs = false;
+        updateVBOs();
+
+        for (auto& data : fontData) {
+            auto& d = data.second;
+            if (d.text.size() == 0)
+                continue;
+
+            // TODO make normal
+            events.emit<NewRenderEvent>(d.vbo, d.tex, 0, d.buffer.size());
+        }
+    }
 }
 
 void TextSystem::loadFont(const std::string& name,
@@ -105,7 +121,8 @@ void TextSystem::put(const std::string& font,
     if (fontData.find(font) == fontData.end())
         return;
 
-    fontData[font].text.emplace_back(text, x, y);
+    fontData[font].text.emplace_back(text, x, y, -9.0f);
+    shouldUpdateVBOs = true;
 }
 
 void TextSystem::updateVBOs(void)
index 8efe3ae23b73c8fe3e9f303dd8d258557a08d564..c3479ca6e0d8cd994529efcef32779b28a7fe631 100644 (file)
@@ -91,6 +91,7 @@ private:
     FT_Library freetype;
     std::map<std::string, FT_Face> fonts;
     std::map<std::string, Font> fontData;
+    bool shouldUpdateVBOs;
 
     void updateVBOs(void);
 };