diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-16 12:46:44 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-16 12:46:44 -0400 |
commit | dbb26902ed54ce308fdcec4697616e152f2894fd (patch) | |
tree | 13f6d840e6b300c10a1f6ff54051c12d9a4126e4 | |
parent | 331e858d2dd0155b54b4bf9edfbbb84d3d5a8145 (diff) |
success with font loading
-rw-r--r-- | Assets/FreePixel.ttf | bin | 0 -> 64880 bytes | |||
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | Scripts/init.lua | 2 | ||||
-rw-r--r-- | src/engine.cpp | 11 | ||||
-rw-r--r-- | src/script.cpp | 13 | ||||
-rw-r--r-- | src/script.hpp | 6 | ||||
-rw-r--r-- | src/text.cpp | 23 | ||||
-rw-r--r-- | src/text.hpp | 13 |
8 files changed, 43 insertions, 26 deletions
diff --git a/Assets/FreePixel.ttf b/Assets/FreePixel.ttf Binary files differnew file mode 100644 index 0000000..d22b2a2 --- /dev/null +++ b/Assets/FreePixel.ttf @@ -83,3 +83,4 @@ $(OUTDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT) @rm -f $(OUTDIR)/$*.$(DEPEXT).tmp .PHONY: all remake clean cleaner resources + diff --git a/Scripts/init.lua b/Scripts/init.lua index f177462..2eeee18 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -1,3 +1,5 @@ +game.loadFont("freepixel16", "Assets/FreePixel.ttf", 16) + bird = { Player = 0, EventListeners = { diff --git a/src/engine.cpp b/src/engine.cpp index 66739ff..37f6e68 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -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; diff --git a/src/script.cpp b/src/script.cpp index a6f0968..61aa136 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -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) diff --git a/src/script.hpp b/src/script.hpp index d4e2234..f86c8cd 100644 --- a/src/script.hpp +++ b/src/script.hpp @@ -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_ diff --git a/src/text.cpp b/src/text.cpp index 7b2eb02..85c0ceb 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -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; } diff --git a/src/text.hpp b/src/text.hpp index 08248b3..d7fb790 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -24,12 +24,23 @@ #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 |