From ad5a63db312d0029109e6ca0051feaa516419ad2 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 1 Oct 2019 08:37:05 -0400 Subject: cleaned up lua function bindings --- src/engine.cpp | 10 ++++------ src/script.cpp | 11 +++-------- src/script.hpp | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index b1d9a56..81e0272 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -59,13 +59,11 @@ int Engine::init(void) // Load game script and entity data auto* script = systems.system().get(); script->addToGameNamespace("loadFont", - [this](std::string name, std::string file, int size) { - systems.system().get()->loadFont(name, file, size); - }); + bindInstance(&TextSystem::loadFont, + systems.system().get())); script->addToGameNamespace("puts", - [this](std::string name, float x, float y, std::string text) { - systems.system().get()->put(name, x, y, text); - }); + bindInstance(&TextSystem::put, + systems.system().get())); script->init(); diff --git a/src/script.cpp b/src/script.cpp index 6cda627..0f940d3 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -87,12 +87,6 @@ void ScriptSystem::doFile(void) void ScriptSystem::scriptExport(void) { - std::function entitySpawn = - [this](sol::table t){ return spawn(t);}; - - std::function worldRegister = - [this](sol::object t){ return worldSystem.addWorld(t); }; - lua.new_usertype("Position", sol::constructors(), "x", &Position::x, @@ -137,8 +131,9 @@ void ScriptSystem::scriptExport(void) "getSize", &World::getSize); game = lua["game"].get_or_create(); - game.set_function("spawn", entitySpawn); - game.set_function("worldRegister", worldRegister); + game.set_function("spawn", bindInstance(&ScriptSystem::spawn, this)); + game.set_function("worldRegister", bindInstance(&WorldSystem::addWorld, + &worldSystem)); } sol::table ScriptSystem::spawn(sol::object param) diff --git a/src/script.hpp b/src/script.hpp index 0ac9e63..24cc142 100644 --- a/src/script.hpp +++ b/src/script.hpp @@ -26,6 +26,21 @@ #include "world.hpp" +/** + * Utility for pairing class instances to their member function calls. + * This is useful for adding functions to the Lua game namespace. + * + * @param func The member function to call + * @param instance The instance to bind to + * @return A function that calls the member function using the given instance + */ +template +auto bindInstance(R (C::* func)(Args...), C *instance) +{ + return [instance, func](Args... args) { (instance->*func)(args...); }; +} + + struct EntitySpawnEvent { sol::object ref; -- cgit v1.2.3 From 5b1c22529a946a782a8376de2b34c28348d078d1 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 3 Oct 2019 14:54:55 -0400 Subject: fixes; font mem leak fix --- Scripts/world.lua | 2 +- src/render.cpp | 3 +-- src/text.cpp | 22 ++++++++++++++-------- src/text.hpp | 2 ++ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Scripts/world.lua b/Scripts/world.lua index 3b56d9a..bb6c61e 100644 --- a/Scripts/world.lua +++ b/Scripts/world.lua @@ -103,7 +103,7 @@ world = { end end end - self:setData(1000, 1345, 5, "grass"); -- Test error checking + --self:setData(1000, 1345, 5, "grass"); -- Test error checking print("Done with world gen"); end } diff --git a/src/render.cpp b/src/render.cpp index cc7ecb1..0f0b138 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -339,9 +339,8 @@ int RenderSystem::init(void) context = SDL_GL_CreateContext(window.get()); - GLenum err; glewExperimental = GL_TRUE; - if((err=glewInit()) != GLEW_OK){ + if (auto err = glewInit(); err != GLEW_OK){ std::cerr << "GLEW was not able to initialize! Error: " << glewGetErrorString(err) << std::endl; return -1; diff --git a/src/text.cpp b/src/text.cpp index 6917a2c..fb82875 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -4,6 +4,14 @@ #include +TextSystem::~TextSystem(void) +{ + for (auto [name, face] : fonts) + FT_Done_Face(face); + + FT_Done_FreeType(freetype); +} + void TextSystem::configure([[maybe_unused]] entityx::EntityManager& entities, [[maybe_unused]] entityx::EventManager& events) { @@ -25,12 +33,11 @@ void TextSystem::update([[maybe_unused]] entityx::EntityManager& entites, shouldUpdateVBOs = false; updateVBOs(); - for (auto& data : fontData) { - auto& d = data.second; - if (d.text.size() == 0) - continue; - - events.emit(d.vbo, d.tex, 0, d.buffer.size()); + for (auto& [name, font] : fontData) { + if (font.text.size() != 0) { + events.emit(font.vbo, font.tex, 0, + font.buffer.size()); + } } } } @@ -145,8 +152,7 @@ void TextSystem::put(const std::string& font, void TextSystem::updateVBOs(void) { - for (auto& data : fontData) { - auto& d = data.second; + for (auto& [name, d] : fontData) { d.buffer.clear(); for (auto& text : d.text) { float tx = text.x; diff --git a/src/text.hpp b/src/text.hpp index 7cf90b2..1ef2afa 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -77,6 +77,8 @@ struct Font { class TextSystem : public entityx::System { public: + ~TextSystem(void); + /** * Prepares the system for running. */ -- cgit v1.2.3