cocos2dx lua自定义lua-binding

一、环境

系统 ios10.12.6

编译、编辑环境xcode9.1

cocos2dx版本3.13.1

二、绑定lua-binding前的准备

接下来的做法,只需要懂得cocos2dx的api就可以,不需要对lua有很深的了解;但是如果可以的话,应该把lua学习得比较透彻,至少了解lua栈的机制,这样对自己将要修改的东西才能更加明白透彻。

三、绑定自己的lua-binding

lua-binding里有很多的api,对这些api,如果可以了解到一些当然也是好事,但是我们不需要了解也可以自定义自己的lua-binding。

cocos2x里面已经有很多集成好的例子,我们可以学着来做一个自己的。

我们要仿照的例子是lua_cocos2dx_cocosdension_auto。

1.写一个自己的Node类

可以继承自Node或者Sprite类,都可以。我就以我之前写过的LiveVideo项目为例。这个类就是我们要从c++映射到lua的类,映射之后,还要在lua新建对象并显示出来。

LiveVideo类就不写出来了。

2.仿照cocosdension的代码

看lua-binding下面有一个auto文件夹。


文件夹的真实目录是cocos/scripting/lua-binding/auto。该文件夹下有个lua_cocos2dx_cocosdension_auto.cpp和.hpp文件。

这两个文件就是我们要仿照的例子。

看lua_cocos2dx_cocosdension的代码的时候,借鉴最后的一个类的代码就好了,比如SimpleAudioEngin类。

在这个auto文件夹中新建两个文件,名字为lua_cocos2dx_liveVideo_auto.cpp和lua_cocos2dx_liveVideo_auto.h。


我们写的lua_cocos2dx_liveVideo_auto.cpp和.h都要仿照lua_cocos2dx_cocosdension_auto文件。

这里,只贴出lua_cocos2dx_liveVideo_auto.cpp和.h文件的代码,lua_cocos2dx_cocosdension_auto文件的代码要靠自己领悟了。

lua_cocos2dx_liveVideo.cpp:

#include "scripting/lua-bindings/auto/lua_cocos2dx_liveVideo_auto.h"
#include "scripting/lua-bindings/manual/tolua_fix.h"
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
#include "liveVideo/LiveVideo.h"

int lua_cocos2dx_LiveVideo_create(lua_State* tolua_S)
{
    CCLOG("test!!!!!!");
    int argc = 0;
    bool ok  = true;
    
#if COCOS2D_DEBUG >= 1
    tolua_Error tolua_err;
#endif
    
#if COCOS2D_DEBUG >= 1
    if (!tolua_isusertable(tolua_S,1,"cc.LiveVideo",0,&tolua_err)) goto tolua_lerror;
#endif
    
    argc = lua_gettop(tolua_S) - 1;
    
    if (argc == 0)
    {
        if(!ok)
        {
            tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_LiveVideo_create'", nullptr);
            return 0;
        }
        CCLOG("v1111");
        LiveVideo* ret = LiveVideo::create();
        object_to_luaval<LiveVideo>(tolua_S, "cc.LiveVideo",(LiveVideo*)ret);
        return 1;
    }
    luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n ", "cc.LiveVideo:create",argc, 0);
    return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
    tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_LiveVideo_create'.",&tolua_err);
#endif
    return 0;
}

int lua_register_cocos2dx_LiveVideo(lua_State* tolua_S)
{
    tolua_usertype(tolua_S,"cc.LiveVideo");
    tolua_cclass(tolua_S,"LiveVideo","cc.LiveVideo","cc.Node",nullptr);
    tolua_beginmodule(tolua_S,"LiveVideo");
    tolua_function(tolua_S,"create", lua_cocos2dx_LiveVideo_create);
    tolua_endmodule(tolua_S);
    std::string typeName = typeid(LiveVideo).name();
    g_luaType[typeName] = "cc.LiveVideo";
    g_typeCast["LiveVideo"] = "cc.LiveVideo";
    return 1;
}
TOLUA_API int register_all_cocos2dx_liveVideo(lua_State* tolua_S)
{
    tolua_open(tolua_S);
    
    tolua_module(tolua_S,"cc",0);
    tolua_beginmodule(tolua_S,"cc");
    lua_register_cocos2dx_LiveVideo(tolua_S);
    
    tolua_endmodule(tolua_S);
    return 1;
}
lua_cocos2dx_liveVideo.h:

#include "base/ccConfig.h"
#ifndef lua_cocos2dx_liveVideo_auto_h
#define lua_cocos2dx_liveVideo_auto_h

#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif

int register_all_cocos2dx_liveVideo(lua_State* tolua_S);



#endif /* lua_cocos2dx_liveVideo_auto_h */

接下来,如图所示:

新建liveVideo文件夹

新建lua_cocos2dx_liveVideo_muanal.cpp

新建lua_cocos2dx_liveVideo_manual.h

同样,lua_cocos2dx_cocosdension_manual文件也要学习,因为lua_cocos2dx_liveVideo_manual的代码都是要从这里借鉴的。


lua_cocos2dx_liveVideo_manual.cpp:

/****************************************************************************
 Copyright (c) 2013-2014 Chukong Technologies Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/
#include "scripting/lua-bindings/manual/liveVideo/lua_cocos2dx_liveVideo_manual.h"
#include "scripting/lua-bindings/auto/lua_cocos2dx_liveVideo_auto.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"

int  register_liveVideo_module(lua_State* L)
{
    lua_getglobal(L, "_G");
    if (lua_istable(L,-1))//stack:...,_G,
    {
        register_all_cocos2dx_liveVideo(L);
    }
    lua_pop(L, 1);
    return 1;
}

lua_cocos2dx_liveVideo_manual.h:

/****************************************************************************
 Copyright (c) 2013-2014 Chukong Technologies Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/
#ifndef lua_cocos2dx_liveVideo_manual
#define lua_cocos2dx_liveVideo_manual

#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif

/**
 * @addtogroup lua
 * @{
 */

/**
 * Call this function can import the lua bindings for the cocosdenshion module.
 * After registering, we could call the related cocosdenshion code conveniently in the lua.eg,.cc.SimpleAudioEngine:getInstance():stopAllEffects().
 * In current mechanism, most bindings function of SimpleAudioEngine are wrapped in the Lua script file named AudioEngine.lua by more friendly modes.
 * If you don't want to use the cocosdenshion module in the lua, you only don't call this registering function.
 * If you don't register the cocosdenshion module, the package size would become smaller .
 * The current mechanism,this registering function is called in the lua_module_register.h
 */
TOLUA_API int  register_liveVideo_module(lua_State* L);

// end group
/// @}

#endif // #ifndef COCOS_SCRIPTING_LUA_BINDINGS_MANUAL_COCOSDENSHION_LUA_COCOS2DX_COCOSDENSHION_MANUAL_H__


最后:
在lua_module_register里添加这样的代码


最最后:

自己写lua代码测试了:

function LoginScene:initLiveVideoView()
	local button = ccui.Button:create("whiteBg.png");
	button:setPosition(cc.p(self._width - 40, 40));
	local function showLiveVideo()
		self:removeAllChildren();
		self:addChild(cc.LiveVideo:create());
	end
	button:addClickEventListener(showLiveVideo);
	button:ignoreContentAdaptWithSize(false);
	button:setContentSize(cc.size(40, 40))
	self:addChild(button);
end


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值