在Lua

时间:2015-07-25 17:42:54

标签: c++ lua sfml luabridge

我已经使用LuaBridge向Lua公开了许多SFML类,并且我测试了大多数类并且它们工作得很好(sf::RectangleShapesf::Texture等等)但是,我遇到了问题sf::Fontsf::Text。首先,我的C ++代码公开sf::Fontsf::Text

#include <string>

#include <SFML/Graphics.hpp>

#include <lua.hpp>
#include <LuaBridge.h>

//Wrapper for sf::Text:
class TextWrap
{
    private:
        sf::Text member;

    public:
        void setCharacterSize(const int& newSize) {member.setCharacterSize(newSize);}
        int getCharacterSize() {return member.getCharacterSize();}

        void setColor(const sf::Color& newColor) {member.setColor(newColor);}
        sf::Color getColor() {return member.getColor();}

        void setFont(const sf::Font& newFont) {member.setFont(newFont);}
        const sf::Font* getFont() {return member.getFont();}

        void setPosition(const sf::Vector2f& newPosition) {member.setPosition(newPosition);}
        sf::Vector2f getPosition() {return member.getPosition();}

        void setRotation(const float& rotation) {member.setRotation(rotation);}
        float getRotation() {return member.getRotation();}

        void setScale(const sf::Vector2f& newScale) {member.setScale(newScale);}
        sf::Vector2f getScale() {return member.getScale();}

        void setString(const std::string& text) {member.setString(text);}
        std::string getString() {return member.getString();}

        void move(const sf::Vector2f& movePos) {member.move(movePos);}
        void rotate(const float& rotation) {member.rotate(rotation);}
        void scale(const sf::Vector2f& scaleAmount) {member.scale(scaleAmount);}

        sf::Text getMember() {return member;}
};

class WindowWrap
{
    private:
        sf::RenderWindow member;

    public:
        void createWindowed(const unsigned int width, const unsigned int height, const std::string& title) {member.create(sf::VideoMode(width, height), title);}
        void createFullscreen(const unsigned int width, const unsigned int height, const std::string& title) {member.create(sf::VideoMode(width, height), title, sf::Style::Fullscreen);}
        void createSplash(const unsigned int width, const unsigned int height) {member.create(sf::VideoMode(width, height), "Splash");}
        void close() {member.close();}

        void clear(const sf::Color& clearColour) {member.clear(clearColour);}
        void display() {member.display();}
        void drawText(TextWrap& text) {member.draw(text.getMember());}

        void setPosition(const sf::Vector2i& newPosition) {member.setPosition(newPosition);}
        sf::Vector2i getPosition() {return member.getPosition();}
        void setSize(const sf::Vector2u& newSize) {member.setSize(newSize);}
        sf::Vector2u getSize() {return member.getSize();}

        bool isOpen() {return member.isOpen();}

        sf::Vector2i mapCoordsToPixel(const sf::Vector2f& point) {return member.mapCoordsToPixel(point);}
        sf::Vector2f mapPixelToCoords(const sf::Vector2i& point) {return member.mapPixelToCoords(point);}

        bool setActive() {return member.setActive();}

        void setFramerateLimit(const unsigned int& limit) {member.setFramerateLimit(limit);}

        void setJoystickThreshold(const float& threshold) {member.setJoystickThreshold(threshold);}
        void setKeyRepeatEnabled(bool enabled) {member.setKeyRepeatEnabled(enabled);}
        void setVerticalSyncEnabled(bool enabled) {member.setVerticalSyncEnabled(enabled);}
        void setMouseCursorVisible(bool enabled) {member.setMouseCursorVisible(enabled);}

        void setTitle(const std::string& title) {member.setTitle(title);}

        void setVisible(const bool& enabled) {member.setVisible(enabled);}
};

int main()
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    luabridge::getGlobalNamespace(L)
    .beginClass<sf::Vector2f>("Vector2f")
        .addConstructor<void(*)(float, float)>()
        .addData("x", &sf::Vector2f::x)
        .addData("y", &sf::Vector2f::y)
    .endClass()
    .beginClass<sf::Vector2i>("Vector2i")
        .addConstructor<void(*)(int, int)>()
        .addData("x", &sf::Vector2i::x)
        .addData("y", &sf::Vector2i::y)
    .endClass()
    .beginClass<sf::Vector2u>("Vector2u")
        .addConstructor<void(*)(unsigned int, unsigned int)>()
        .addData("x", &sf::Vector2u::x)
        .addData("y", &sf::Vector2u::y)
    .endClass()

    .beginClass<sf::IntRect>("IntRect")
        .addConstructor<void(*)(int, int, int, int)>()
        .addData("top", &sf::IntRect::top)
        .addData("left", &sf::IntRect::left)
        .addData("height", &sf::IntRect::height)
        .addData("width", &sf::IntRect::width)
    .endClass()
    .beginClass<sf::FloatRect>("FloatRect")
        .addConstructor<void(*)(float, float, float, float)>()
        .addData("top", &sf::FloatRect::top)
        .addData("left", &sf::FloatRect::left)
        .addData("height", &sf::FloatRect::height)
        .addData("width", &sf::FloatRect::width)
    .endClass()

    .beginClass<sf::Color>("Color")
        .addConstructor<void(*)(unsigned int, unsigned int, unsigned int, unsigned int)>()
        .addData("r", &sf::Color::r)
        .addData("g", &sf::Color::g)
        .addData("b", &sf::Color::b)
        .addData("a", &sf::Color::a)
    .endClass()

    .beginClass<sf::Font>("Font")
        .addConstructor<void(*)(void)>()
        .addFunction("loadFromFile", &sf::Font::loadFromFile)
    .endClass()
    .beginClass<TextWrap>("Text")
        .addConstructor<void(*)(void)>()
        .addFunction("setCharacterSize", &TextWrap::setCharacterSize)
        .addFunction("getCharacterSize", &TextWrap::getCharacterSize)
        .addFunction("setColor", &TextWrap::setColor)
        .addFunction("getColor", &TextWrap::getColor)
        .addFunction("setFont", &TextWrap::setFont)
        .addFunction("getFont", &TextWrap::getFont)
        .addFunction("setPosition", &TextWrap::setPosition)
        .addFunction("getPosition", &TextWrap::getPosition)
        .addFunction("setRotation", &TextWrap::setRotation)
        .addFunction("getRotation", &TextWrap::getRotation)
        .addFunction("setScale", &TextWrap::setScale)
        .addFunction("getScale", &TextWrap::getScale)
        .addFunction("setString", &TextWrap::setString)
        .addFunction("getString", &TextWrap::getString)
        .addFunction("move", &TextWrap::move)
        .addFunction("rotate", &TextWrap::rotate)
        .addFunction("scale", &TextWrap::scale)
    .endClass()

    .beginClass<WindowWrap>("Window")
        .addConstructor<void(*)(void)>()
        .addFunction("createWindowed", &WindowWrap::createWindowed)
        .addFunction("createFullscreen", &WindowWrap::createFullscreen)
        .addFunction("createSplash", &WindowWrap::createSplash)
        .addFunction("close", &WindowWrap::close)
        .addFunction("clear", &WindowWrap::clear)
        .addFunction("display", &WindowWrap::display)
        .addFunction("drawText", &WindowWrap::drawText)
        .addFunction("setPosition", &WindowWrap::setPosition)
        .addFunction("getPosition", &WindowWrap::getPosition)
        .addFunction("setSize", &WindowWrap::setSize)
        .addFunction("getSize", &WindowWrap::getSize)
        .addFunction("isOpen", &WindowWrap::isOpen)
        .addFunction("mapCoordsToPixel", &WindowWrap::mapCoordsToPixel)
        .addFunction("mapPixelToCoords", &WindowWrap::mapPixelToCoords)
        .addFunction("setActive", &WindowWrap::setActive)
        .addFunction("setFramerateLimit", &WindowWrap::setFramerateLimit)
        .addFunction("setKeyRepeatEnabled", &WindowWrap::setKeyRepeatEnabled)
        .addFunction("setVerticalSyncEnabled", &WindowWrap::setVerticalSyncEnabled)
        .addFunction("setMouseCursorVisible", &WindowWrap::setMouseCursorVisible)
        .addFunction("setTitle", &WindowWrap::setTitle)
        .addFunction("setVisible", &WindowWrap::setVisible)
    .endClass();

    WindowWrap mainWindow; //This is defined here so C++ knows if the main application is open
    mainWindow.createWindowed(800, 600, "Lua Test");
    luabridge::push(L, &mainWindow);
    lua_setglobal(L, "mainWindow");

    luaL_dofile(L, "./script.lua");
}

如果我创建使用TextFont的lua脚本,我会收到分段错误,有时会。我的意思是,当我绘制Text对象时,我只会得到错误,如果我设置了字体。

脚本:

text = Text();
text:setCharacterSize(16);
text:setPosition(Vector2f(100, 100));
text:setColor(Color(255,255,255,255));
text:setString("Hello World");
font = Font();
font:loadFromFile("./font.ttf");
text:setFont(font); --If I remove this I don't get a Segmentation Fault
mainWindow:drawText(text); --But the Segmentation Fault happens here

更新

在使用其他SFML类进行测试后,对于sf::Texturesf::Sprite / sf::RectangleShape / sf::CircleShape等,这似乎不是问题。这告诉我它是sf::Font问题,而不是被删除的引用

Related Question about LuaBridge and SFML

1 个答案:

答案 0 :(得分:0)

我找到解决问题的方法(感谢答案的SFML Forums)是在TextWrap更改此行代码:

void setFont(const sf::Font& newFont) {member.setFont(newFont);}

对此:

void setFont(const sf::Font* newFont) {member.setFont(*newFont);}

根据论坛,这是一个问题的原因是因为Lua基于C而不是C ++,所以不知道指针。因此,通过使参数成为指针,然后取消引用指针,我有效地解决了问题