http://my.oschina.net/skyHacker2/blog/298397
目录[-]
Cocos2d-x 3.1 Lua Binding 添加需要绑定的C++类 在xcode中,把my文件夹拖到cocos2d_libs.xcodeproj中 HNLuaTest.h HNLuaTest.cpp 编写ini文件 编写genbindings.py 运行genbindings_custom.py mac和ios的集成 Android集成 注册到Lua cocos code ide build custom runtime 测试 绑定时候使用自己的命名空间 Cocos2d-x 3.1 Lua Binding参考:http://www.cocos2d-x.org/docs/manual/code-ide/binding-custom-class-to-lua/en
添加需要绑定的C++类在xcode中,把my文件夹拖到cocos2d_libs.xcodeproj中
HNLuaTest.h
//// HNLuaTest.h// cocos2d_libs//// Created by Eleven Chen on 14-8-5.////#ifndef __cocos2d_libs__HNLuaTest__#define __cocos2d_libs__HNLuaTest__#include "cocos2d.h"#include <string>class Test : public cocos2d::Ref{public: static std::string helloMsg(); static Test* create(); bool init(); static cocos2d::Vec2 left();};#endif /* defined(__cocos2d_libs__HNLuaTest__) */HNLuaTest.cpp
//// HNLuaTest.cpp// cocos2d_libs//// Created by Eleven Chen on 14-8-5.////#include "HNLuaTest.h"using namespace cocos2d;std::string Test::helloMsg(){ return "Hello from HNLuaTest::helloMsg()";}Test* Test::create(){ return new test();}bool Test::init(){ return true;}Vec2 Test::left(){ return Vec2(0,0);}编写ini文件
进入到tolua目录:$(PROJECT_ROOT)/frameworks/cocos2d-x/tools/tolua
在之前先看看README文件,并安装好环境。
README文件中提到必须要用androID ndk r9b来编译,因为编译的时候用到了C++ std 4.7的头文件,而r9b包含了4.7,我用的是r9d,只有4.6和4.8。我把*.ini文件里面的4.7改成4.8来编译。
新建一个cocos2dx_custom.ini文件,然后从其他文件里面copy一份代码过来修改,代码里面每一个设置都有详细的注释。
clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include # the prefix to be added to the generated functions. You might or might not use this in your own# templatesprefix = cocos2dx_custom# create a target namespace (in JavaScript,this would create some code like the equiv. to `ns = ns || {}`)# all classes will be embedded in that namespacetarget_namespace = cc# the native namespace in which this module locates,this parameter is used for avoID conflict of the same class name in different modules,as "cocos2d::Label" <-> "cocos2d::ui::Label".cpp_namespace = androID_headers = -I%(androIDndkdir)s/platforms/androID-14/arch-arm/usr/include -I%(androIDndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androIDndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/includeandroID_flags = -D_SIZE_T_defineD_ clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/androIDcocos_flags = -DANDROIDcxxgenerator_headers = # extra arguments for clangextra_arguments = %(androID_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(androID_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s # what headers to parseheaders = %(cocosdir)s/cocos/my/HNLuaTest.h# what classes to produce code for. You can use regular Expressions here. When testing the regular# Expression,it will be enclosed in "^$",like this: "^Menu*$".classes = Test.*# what should we skip? in the format Classname::[function function]# Classname is a regular Expression,but will be used like this: "^Classname$" functions are also# regular Expressions,they will not be surrounded by "^$". If you want to skip a whole class,just# add a single "*" as functions. See bellow for several examples. A special class name is "*",which# will apply to all class names. This is a convenIEnce wildcard to be able to skip similar named# functions from all classes.skip = rename_functions = rename_classes =# for all class names,should we remove something when registering in the target VM?remove_prefix = # classes for which there will be no "parent" lookupclasses_have_no_parents = Test# base classes which will be skipped when their sub-classes found them.base_classes_to_skip =# classes that create no constructor# Set is special and we will use a hand-written constructorabstract_classes = # Determining whether to use script object(Js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.script_control_cpp = no编写genbindings.py
为了方便,每次绑定时不需要生成cocos2d-x的绑定代码,新建一个genbindings_custom.py
文件,然后复制genbindings.py
的代码过来,把下面的地方:
cmd_args = {'cocos2dx.ini' : ('cocos2d-x','lua_cocos2dx_auto'),\ 'cocos2dx_extension.ini' : ('cocos2dx_extension','lua_cocos2dx_extension_auto'),\ 'cocos2dx_ui.ini' : ('cocos2dx_ui','lua_cocos2dx_ui_auto'),\ 'cocos2dx_studio.ini' : ('cocos2dx_studio','lua_cocos2dx_studio_auto'),\ 'cocos2dx_spine.ini' : ('cocos2dx_spine','lua_cocos2dx_spine_auto'),\ 'cocos2dx_physics.ini' : ('cocos2dx_physics','lua_cocos2dx_physics_auto'),\ 'custom.ini': ('custom','lua_custom_auto'),\ 'cocos2dx_custom.ini' : ('cocos2dx_custom','lua_cocos2dx_custom_auto'),\ }
改为
cmd_args = {'cocos2dx_custom.ini' : ('cocos2dx_custom',\ }运行genbindings_custom.py
./genbindings_custom.py
生成的代码在cocos/scripting/lua-bindings/auto
里面
在cocos2d_lua_bindings.xcodeproj 的Build Settings中设置头文件搜索,添加
my
AndroID集成 在
my
文件夹中添加AndroID.mk文件,如下内容 LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODulE := cocos_custom_staticLOCAL_MODulE_filename := libmyLOCAL_SRC_fileS := \HNLuaTest.cppLOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) $(LOCAL_PATH)./LOCAL_C_INCLUDES := $(LOCAL_PATH) $(LOCAL_PATH)./LOCAL_CFLAGS += -Wno-psabiLOCAL_EXPORT_CFLAGS += -Wno-psabiLOCAL_WHolE_STATIC_liBRARIES := cocos2dx_staticinclude $(BUILD_STATIC_liBRARY)$(call import-module,.)打开lua-bindings文件夹的AndroID.mk文件,添加以下内容
LOCAL_SRC_fileS := manual/ccluaBrIDge.cpp \ #省略 auto/lua_cocos2dx_custom_auto.cpp \ ../../../external/lua/tolua/tolua_event.c \ #省略注册到Lua
修改AppDelegate.cpp
//register custom function //LuaStack* stack = engine->getLuaStack(); //register_custom_function(stack->getLuaState()); LuaStack *stack = engine->getLuaStack(); auto L = stack->getLuaState(); if (L) { lua_getglobal(L,"_G"); register_all_cocos2dx_custom(L); lua_settop(L,0); }#if (COCOS2D_DEBUG>0) if (startRuntime()) return true;#endif engine->executeScriptfile(ConfigParser::getInstance()->getEntryfile().c_str()); return true;
这里不能按照原本代码注释的方法那样做,那样会出错。
而且都是tolua_module
这个函数里面的lua_rawget(L,-2);
这一行出错。
原因是:
auto engine = LuaEngine::getInstance(); _defaultEngine->init(); executeScriptfile(“Deprecated.lua”); _stack->clean(); lua_settop(_state,0);LuaEngine初始化的时候会运行3个lua文件,把stack给清空了。
在tolua_module
函数中,lua_pushstring(L,name);
后,栈只有一个元素,所以 只需要把
_G
放到栈中,再运行register_all_cocos2dx_custom(L);
,如果模块不在_G
中,就添加到_G
中,就可以全局访问了。
测试
-- test custom local msg = cc.Test:helloMsg() print(msg)
输出
打开:cocos2d-x/tools/bindings-generator/targets/lua/conversions.yaml文件
在ns_map添加自己的命名空间:
ns_map: "cocos2d::extension::": "cc." "cocos2d::ui::": "ccui." "cocos2d::": "cc." "spine::": "sp." "cocostudio::": "ccs." "cocosbuilder::": "cc." "CocosDenshion::": "cc." # 我自己的命名空间 "hunuo::": "hn."总结
以上是内存溢出为你收集整理的Cocos2d-x 3.1 Lua Binding全部内容,希望文章能够帮你解决Cocos2d-x 3.1 Lua Binding所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)