Question

I am working on a 2D game engine at the moment and have hit a stumbling block whilst implementing the LUA scripting / Interpreter.

I'm using SWIG and have got the basics all working fine.

In the engine I use the GLM (OpenGL Mathematics Libary http://glm.g-truc.net/) for all Vector and Matrix related areas.

I really need to expose glm (at a basic level) to the via SWIG to LUA so I can call methods in LUA like:

actor:GetPosition()  <- Which returns a glm::vec2

GLM is quite a complex library (possibly an understatement lol) and I do not require the whole thing exposed, that would be ridiculous amounts of work I assume. I simply want to be able to access the xy components of the glm::vec2 class.

I'm sure this must be easy as SWIG doesn't require a full class definition and there must be a way to let SWIG just assume that the glm::vec2 class just has x,y params.

I'm not sure If using Proxy classes in SWIG is the way to do this ? or some other method ? I'm quite new to LUA integration and SWIG also.

One route I really don't want to go down is ditching GLM and writing my own Vector/Matrix library which is much more simple, no templates etc and I can then simply wrap with SWIG, but I feel this would be a waste of time and I would ultimately end up with a less powerful Math Library :(.

Thanks in advance and I can provide more info if necessary.

Was it helpful?

Solution

A solution in luabind would look like this:

#include <luabind/luabind.hpp>


extern "C" int init(lua_State* L)
{
    using namespace luabind;
    using namespace glm;

    open(L);

    module(L)
    [
        class_<dvec2>("dvec2")
            .def_readwrite("x", &dvec2::x)
            .def_readwrite("y", &dvec2::y)
    ];

    return 0;
}

OTHER TIPS

The big problem with GLM is that its vector types use things that have dubious C++ validity. Things that many compilers will allow, but are not allowed by the standard. SWIG uses its own parser to work, so it's going to have a hard time making heads or tails of GLM's definitions.

In a case like this, I would suggest using something like Luabind rather than SWIG. It gives a bit more fine-grained control in such circumstances, and it has the benefit of not using its own parser. Granted, you could effectively rewrite the prototypes for the important parts of GLM in your .swig file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top