c++とlua

13262 ワード

luaは多くのc apiが持つluaとcは非常に便利なインタラクションを提供しているが、実際に運用されているインタラクションはc++java objcなどの言語を使用してluaとインタラクションして機能を実現する必要がある.
cocosでc++クラスをluaに登録する主にクラスオブジェクトインスタンス化関数をluaおよびインスタンスオブジェクトに登録する関数をluaに登録し、クラスオブジェクトをusertypeに登録し、関数をクラスmoduleに登録することに関心を持っている.
DEMO CODE
//  c      tolua++
#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif

TOLUA_API int register_test_binding(lua_State* tolua_S);

//     c++     public     lua
class DrawNode3D: public Node
{
public:

    static DrawNode3D* create();

    void drawLine(const Vec3 &from, const Vec3 &to, const Color4F &color);

    void drawCube(Vec3* vertices, const Color4F &color);

    const BlendFunc& getBlendFunc() const;

    void setBlendFunc(const BlendFunc &blendFunc);

    void onDraw(const Mat4 &transform, uint32_t flags);
};



int lua_cocos2dx_DrawNode3D_getBlendFunc(lua_State* L)
{
    int argc = 0;
    cocos2d::DrawNode3D* cobj = nullptr;
    bool ok  = true;

#if COCOS2D_DEBUG >= 1
    tolua_Error tolua_err;
#endif


#if COCOS2D_DEBUG >= 1
//    drawNode  usertype     
    if (!tolua_isusertype(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror;
#endif
    //     userType     c++ drawNode    
    cobj = (cocos2d::DrawNode3D*)tolua_tousertype(L,1,0);

#if COCOS2D_DEBUG >= 1
    if (!cobj)
    {
        tolua_error(L,"invalid 'cobj' in function 'lua_cocos2dx_DrawNode3D_getBlendFunc'", nullptr);
        return 0;
    }
#endif
    //              
    argc = lua_gettop(L)-1;
    if (argc == 0)
    {
        if(!ok)
            return 0;
        //   c++  
        const cocos2d::BlendFunc& ret = cobj->getBlendFunc();
        //     lua
        blendfunc_to_luaval(L, ret);
        return 1;
    }
    CCLOG("%s has wrong number of arguments: %d, was expecting %d 
"
, "cc.DrawNode3D:getBlendFunc",argc, 0); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_getBlendFunc'.",&tolua_err); #endif return 0; } int lua_cocos2dx_DrawNode3D_create(lua_State* L) { int argc = 0; bool ok = true; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; #endif #if COCOS2D_DEBUG >= 1 // if (!tolua_isusertable(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror; #endif argc = lua_gettop(L) - 1; // if (argc == 0) { if(!ok) return 0; // c++ cocos2d::DrawNode3D* ret = cocos2d::DrawNode3D::create(); // lua object_to_luaval<:drawnode3d>(L, "cc.DrawNode3D",(cocos2d::DrawNode3D*)ret); return 1; } CCLOG("%s has wrong number of arguments: %d, was expecting %d
"
, "cc.DrawNode3D:create",argc, 0); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_create'.",&tolua_err); #endif return 0; } int lua_cocos2dx_DrawNode3D_setBlendFunc(lua_State* L) { int argc = 0; cocos2d::DrawNode3D* cobj = nullptr; bool ok = true; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; #endif #if COCOS2D_DEBUG >= 1 if (!tolua_isusertype(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror; #endif // lua c++ cobj = (cocos2d::DrawNode3D*)tolua_tousertype(L,1,0); #if COCOS2D_DEBUG >= 1 if (!cobj) { // lua tolua_error(L,"invalid 'cobj' in function 'lua_cocos2dx_DrawNode3D_setBlendFunc'", nullptr); return 0; } #endif // argc = lua_gettop(L)-1; if (argc == 1) { cocos2d::BlendFunc arg0; ok &= luaval_to_blendfunc(L, 2, &arg0, "cc.Sprite3D:setBlendFunc"); if(!ok) { tolua_error(L,"invalid arguments in function 'lua_cocos2dx_DrawNode3D_setBlendFunc'", nullptr); return 0; } // cobj->setBlendFunc(arg0); return 0; } CCLOG("%s has wrong number of arguments: %d, was expecting %d
"
, "cc.DrawNode3D:setBlendFunc",argc, 1); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_setBlendFunc'.",&tolua_err); #endif return 0; } int lua_register_cocos2dx_DrawNode3D(lua_State* L) { // cc.DrawNode3D lua userType tolua_usertype(L,"cc.DrawNode3D"); // cc lua tolua_cclass(L,"DrawNode3D","cc.DrawNode3D","cc.Node",nullptr); // drawNode module tolua_beginmodule(L,"DrawNode3D"); // drawNode tolua_function(L,"getBlendFunc",lua_cocos2dx_DrawNode3D_getBlendFunc); tolua_function(L,"setBlendFunc",lua_cocos2dx_DrawNode3D_setBlendFunc01); tolua_function(L,"drawLine",lua_cocos2dx_DrawNode3D_drawLine); tolua_function(L,"clear",lua_cocos2dx_DrawNode3D_clear); tolua_function(L,"drawCube",lua_cocos2dx_DrawNode3D_drawCube); tolua_function(L,"create", lua_cocos2dx_DrawNode3D_create); // drawNode module tolua_endmodule(L); std::string typeName = typeid(cocos2d::DrawNode3D).name(); g_luaType[typeName] = "cc.DrawNode3D"; g_typeCast["DrawNode3D"] = "cc.DrawNode3D"; return 1; } int register_test_binding(lua_State* L) { // lua tolua_open(L); // cc module tolua_module(L, "cc", 0); // module tolua_beginmodule(L, "cc"); // drawNode lua_register_cocos2dx_DrawNode3D(L); // module tolua_endmodule(L); return 0; }