]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
success with font loading
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 16 Sep 2019 16:46:44 +0000 (12:46 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 16 Sep 2019 16:46:44 +0000 (12:46 -0400)
Assets/FreePixel.ttf [new file with mode: 0644]
Makefile
Scripts/init.lua
src/engine.cpp
src/script.cpp
src/script.hpp
src/text.cpp
src/text.hpp

diff --git a/Assets/FreePixel.ttf b/Assets/FreePixel.ttf
new file mode 100644 (file)
index 0000000..d22b2a2
Binary files /dev/null and b/Assets/FreePixel.ttf differ
index e5861100cccda8e995cf009841b50e4a8857b9e6..dcbac2bea5f759dbf1b205c93e230104500bd3f8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -83,3 +83,4 @@ $(OUTDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT)
        @rm -f $(OUTDIR)/$*.$(DEPEXT).tmp
 
 .PHONY: all remake clean cleaner resources
+
index f177462021b5eb2a0464f61f693a8e0f8e1ce80a..2eeee183355db35bd3349e3bfe35d1727971da72 100644 (file)
@@ -1,3 +1,5 @@
+game.loadFont("freepixel16", "Assets/FreePixel.ttf", 16)
+
 bird = {
     Player = 0,
     EventListeners = {
index 66739ffef966c5ba4e6b0e076d197d74bbe82980..37f6e681ea8dab7f960b9ed4699e6f1aab6d3767 100644 (file)
@@ -27,6 +27,7 @@
 #include "script.hpp"
 #include "render.hpp"
 #include "physics.hpp"
+#include "text.hpp"
 
 #include "components/EventListener.hpp"
 #include "components/Script.hpp"
@@ -47,10 +48,18 @@ int Engine::init(void)
     systems.add<RenderSystem>();
     systems.add<ScriptSystem>(entities);
     systems.add<PhysicsSystem>();
+    systems.add<TextSystem>();
     systems.configure();
 
     // Load game script and entity data
-    systems.system<ScriptSystem>()->init();
+    auto* script = systems.system<ScriptSystem>().get();
+    script->addToGameNamespace("loadFont",
+    [this](std::string name, std::string file, int size) {
+        systems.system<TextSystem>().get()->loadFont(name, file, size);
+    });
+    script->init();
+    
+
     if (GameState::load("save.json", entities)) {
         std::cout << "Loaded from save.json. Delete the file if you don't want "
                      "it." << std::endl;
index a6f0968e51507b250ca792f4f351222fcaca66b0..61aa1360f341e18750e950736d25d2f769ed5f10 100644 (file)
@@ -31,6 +31,9 @@ void ScriptSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
 
     // Init after systems.configure() in engine.cpp
     //init();
+    lua.open_libraries(sol::lib::base, sol::lib::math, sol::lib::string);
+
+    scriptExport();
 }
 
 #include <components/Script.hpp>
@@ -54,9 +57,6 @@ void ScriptSystem::receive([[maybe_unused]] const EntitySpawnEvent &toSpawn)
 
 int ScriptSystem::init(void)
 {
-    lua.open_libraries(sol::lib::base, sol::lib::math, sol::lib::string);
-
-    scriptExport();
     doFile();
 
     return 0;
@@ -87,9 +87,6 @@ void ScriptSystem::doFile(void)
 
 void ScriptSystem::scriptExport(void)
 {
-    std::function<sol::table(sol::table)> func = 
-        [this](sol::table t){ return spawn(t);};
-
     lua.new_usertype<Position>("Position",
             sol::constructors<Position(double x, double y), Position()>(),
             "x", &Position::x,
@@ -124,8 +121,8 @@ void ScriptSystem::scriptExport(void)
             sol::constructors<Physics(void), Physics()>(),
             "standing", &Physics::standing);
 
-    auto gamespace = lua["game"].get_or_create<sol::table>();
-    gamespace.set_function("spawn", func);
+    game = lua["game"].get_or_create<sol::table>();
+    game.set_function("spawn", [this](sol::table t) { return spawn(t); });
 }
 
 sol::table ScriptSystem::spawn(sol::object param)
index d4e22347248651e1d6721104c8ffbef35a22b6ce..f86c8cd865f5cff95a22a20de3e0f060904c95df 100644 (file)
@@ -45,6 +45,7 @@ private:
      * interactions between C and Lua
      */
     sol::state lua;
+    sol::table game;
 
     entityx::EntityManager& manager;
 
@@ -96,6 +97,11 @@ public:
      * Contains all calls that export components/functions to lua.
      */
     void scriptExport(void);
+
+    template<typename F>
+    void addToGameNamespace(const std::string& name, F func) {
+        game.set_function(name, func);
+    }
 };
 
 #endif // SYSTEM_SCRIPT_HPP_
index 7b2eb023b8c4a8a73b636198acb600798234b454..85c0ceb2389b19216a414e9fad2571edf05ff7bc 100644 (file)
@@ -1,18 +1,6 @@
 #include "text.hpp"
 
-#include <SDL2/SDL_opengl.h>
-
-#include <tuple>
-
-struct FT_Info {
-    std::pair<float, float> wh;
-    std::pair<float, float> bl;
-    std::pair<float, float> ad;
-       GLuint tex;
-
-       FT_Info(void)
-               : tex(0) {}
-};
+#include <iostream>
 
 //FT_Library freetype;
 //std::map<std::string, FT_Face> fonts;
@@ -41,15 +29,15 @@ void TextSystem::loadFont(const std::string& name,
                           int size)
 {
 
-    if (fonts.find(name) == fonts.end()) {
+    if (fonts.find(file) == fonts.end()) {
         FT_Face face;
         if (FT_New_Face(freetype, file.c_str(), 0, &face)) {
             // TODO handle this error
         }
-        fonts.emplace(name, face);
+        fonts.emplace(file, face);
     }
 
-    auto& face = fonts[name];
+    auto& face = fonts[file];
     FT_Set_Pixel_Sizes(face, 0, size);
     fontData.try_emplace(name, 95);
 
@@ -77,5 +65,8 @@ void TextSystem::loadFont(const std::string& name,
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, g->bitmap.width, g->bitmap.rows,
             0, GL_RGBA, GL_UNSIGNED_BYTE, buf.data());
     }
+
+    std::cout << "Loaded font: " << file << " (size: " << size << ')'
+              << std::endl;
 }
 
index 08248b3607d60e4d492635bc6fd9e91fa39da7c6..d7fb79056355b7f3e64b6d6de584bd27698cb77f 100644 (file)
 #include <entityx/entityx.h>
 #include <ft2build.h>
 #include <freetype/freetype.h>
+#include <SDL2/SDL_opengl.h>
+
 
 #include <map>
 #include <string>
+#include <tuple>
 #include <vector>
 
-struct FT_Info;
+struct FT_Info {
+    std::pair<float, float> wh;
+    std::pair<float, float> bl;
+    std::pair<float, float> ad;
+       GLuint tex;
+
+       FT_Info(void)
+               : tex(0) {}
+};
 
 /**
  * @class PhysicsSystem