暴露一些SFML到lua与luabridge相对容易吗? [英] Is exposing some of SFML to lua with luabridge relatively easy?

查看:620
本文介绍了暴露一些SFML到lua与luabridge相对容易吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将我自己的类暴露给lua,例如:

I understand how to expose my own classes to lua, like this:

lua_State* L = luaL_newstate();
luaL_openlibs(L);
getGlobalNamespace(L)
    .beginClass<Foo>("Foo")
        .addConstructor<void(*)(void)>()
        .addProperty(/*Property Definition*/)
        .addFunction(/*Function Definition*/)
    .endClass()

但是,因为我试图尽可能多地将我的代码移植到lua脚本中,我想有lua类有SFML对象,例如 sf :: Text sf :: Texture 。我没有对Luabridge做过很多实验,我不知道我是否能够这样做:

But, since I am trying to move as much of my code into lua scripts as possible, I would like to have lua classes that have SFML objects, like sf::Text or sf::Texture. I haven't done very much experimentation with Luabridge, and I'm not sure if I'd be able to do something like this:

lua_State* L = luaL_newstate();
luaL_openlibs(L);
getGlobalNamespace(L)
    .beginClass<sf::Text>("Text")
        .addConstructor<void(*)(void)>()
        .addFunction("setCharacterSize", &sf::Text::setCharacterSize)
        .addFunction("getCharacterSize", &sf::Text::getCharacterSize)
        //ETC...
    .endClass()

如果这样做不起作用创建一个包装类,如下所示:

If doing this doesn't work (as I worry it may), would I need to create a wrapper class, like this:

class Text
{
    private:
        sf::Text textObj;

    public:
        void setCharacterSize(const int& size) {textObj.setCharacterSize(size);}
        int& getCharacterSize() {return textObj.getCharacterSize();}
}

//Then do the same as the second snippet, without sf::Text but with Text class



UPDATE:



尝试公开 sf :: Text 类,当尝试执行 sf :: Text :: setPosition 函数时出现错误:

UPDATE:

After attempting to expose the sf::Text class, I get an error when trying to do the sf::Text::setPosition function:

lua_State* L = luaL_newstate();
luaL_openlibs(L);
luabridge::getGlobalNamespace(L)
.beginClass<sf::Text>("Text")
    .addConstructor<void(*)(void)>()
    .addFunction("setCharacterSize", &sf::Text::setCharacterSize)
    .addFunction("getCharacterSize", &sf::Text::getCharacterSize)
    .addFunction("setColor", &sf::Text::setColor)
    .addFunction("getColor", &sf::Text::getColor)
    .addFunction("setFont", &sf::Text::setFont)
    .addFunction("getFont", &sf::Text::getFont)
    .addFunction("setPosition", &sf::Text::setPosition)
    .addFunction("getPosition", &sf::Text::getPosition)
    .addFunction("setScale", &sf::Text::setScale)
    .addFunction("getScale", &sf::Text::getScale)
    .addFunction("setString", &sf::Text::setString)
    .addFunction("getString", &sf::Text::getString)
.endClass()

错误讯息:

no matching function for call to ‘luabridge::Namespace::Class<sf::Text>::addFunction(const char [12], <unresolved overloaded function type>)’
note: candidate is:
note: template<class MemFn> luabridge::Namespace::Class<T>& luabridge::Namespace::Class<T>::addFunction(const char*, MemFn) [with MemFn = MemFn; T = sf::Text]


推荐答案

熟悉luabridge,但它听起来像你有一个问题,因为sf :: Text :: setPosition是重载的。

I'm not familiar with luabridge, but it sounds like you have a problem because sf::Text::setPosition is overloaded.

直接从SFML文档:
void setPosition(float x,float y)
设置对象的位置

Straight from the SFML documentation: void setPosition (float x, float y) set the position of the object

void setPosition(const Vector2f& position)
设置对象的位置

void setPosition (const Vector2f &position) set the position of the object

luabridge无法确定您的意思是用于setPosition。因为你不熟悉你的意见,我会澄清:在C ++中,函数签名是它的名称,它需要的参数(参数的数量和参数的类型)。函数不能具有相同的签名,这意味着它们可以具有相同的名称,但是C ++编译器可以通过它们的参数来分开它们。 sf :: Text有两个函数,名为setPosition,一个是vector2f,另一个是两个float。

There's no way luabridge can determine which you mean to use for setPosition. Since you didn't sound familiar with the idea in your comment, I'll clarify: In C++, a function signature is its name, and the arguments that it takes (both number of arguments and type of arguments). Functions can't have the same signature, which means they CAN have the same name, but the C++ compiler can tell them apart by their arguments. sf::Text has two functions named setPosition, one takes a vector2f, and the other takes two floats directly.

但是, http://oberon00.github.io/luabind/functions.html

在这里谈到重载函数,你可以同样提供签名吗?

Where it talks about overloaded functions can you similarly provide the signature?

所以你会有这样的:

 .addFunction("setPosition", (void(*)(float x, float y)) &sf::Text::setPosition) 

如果没有,你需要包装并给重载一个不同的名字。

If not, you'll need to wrap and give the overloads a different name.

编辑: https://github.com/vinniefalco/LuaBridgeDemo/blob/master/LuaBridge/README.md

LuaBridge不支持重载函数,也不支持是可能在未来。由于Lua是动态类型的,任何尝试解析从脚本传递的一组参数的系统在尝试选择适当匹配的C ++函数签名时将面临相当大的不确定性。

你需要包装,听起来像一个有趣的下午!你需要给函数不同的名称,或者只包括一个。

You'll need to wrap, sounds like a fun afternoon! You'll need to give the functions distinct names, or just include one.

此外,我怀疑当参数或返回值是其他SFML对象时会有一些诡计。我想他们需要先定义吗?请注意,像Vector2i和Vector2f这样的东西是sf :: Vector2和sf :: Vector2的typedef,以保存用户的一些输入。

Also, I suspect there will be some trickery when the arguments or return values are other SFML objects. I guess they'll need to be defined first? Be aware that stuff like Vector2i and Vector2f are typedefs of sf::Vector2 and sf::Vector2 to save the user some typing.

这篇关于暴露一些SFML到lua与luabridge相对容易吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆