cocos2d lua binding自定义 实践

How to Use bindings-generator
==================

On Windows:
------------

* Make sure that you have installed `android-ndk-r16` or later.
* Download python2.7.3 (32bit) from (http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi).
* Add the installed path of python (e.g. C:\Python27) to windows environment variable named 'PATH'.
* Download pyyaml from http://pyyaml.org/download/pyyaml/PyYAML-3.11.win32-py2.7.exe and install it.
* Download [Cheetah-2.4.4.tar.gz](https://pypi.python.org/packages/cd/b0/c2d700252fc251e91c08639ff41a8a5203b627f4e0a2ae18a6b662ab32ea/Cheetah-2.4.4.tar.gz#md5=853917116e731afbc8c8a43c37e6ddba), extract and install it by `python setup.py`.
* Set environment variables `NDK_ROOT` and `PYTHON_BIN`
* Go to "cocos2d-x/tools/tolua" folder, and run "genbindings.py". The generated codes will be under "cocos\scripting\auto-generated\lua-bindings".

tolua文件夹中有说明文档,我用的版本是3.17.2比较新的版本,按照说明把环境配置好之后,执行genbindings.py脚本试一试能不能成功,结果出现了很多次报错,报错信息这里就不展示,解决方法大多数也是百度的。等你折腾到第一次执行成功了之后,那么就可以添加自己的自定义class了。

DownManager类

我添加的一个类,然后呢,在tolua中添加一个ini配置文件,可以找一个复制一份出来,然后改一个名字,我的名字是 cocos2dx_legend.ini

prefix  headers  cocos_headers  classes

这几个参数需要配置好,给看一下我的配置文件吧

[cocos2dx_legend]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = cocos2dx_legend

# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace = cc

android_headers = 

android_flags = -target armv7-none-linux-androideabi -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -DANDROID -D__ANDROID_API__=14 -gcc-toolchain %(gcc_toolchain_dir)s --sysroot=%(androidndkdir)s/platforms/android-14/arch-arm  -idirafter %(androidndkdir)s/sources/android/support/include -idirafter %(androidndkdir)s/sysroot/usr/include -idirafter %(androidndkdir)s/sysroot/usr/include/arm-linux-androideabi -idirafter %(clangllvmdir)s/lib64/clang/5.0/include -I%(androidndkdir)s/sources/cxx-stl/llvm-libc++/include

clang_headers = 
clang_flags = -nostdinc -x c++ -std=c++11 -fsigned-char -U__SSE__

cocos_headers = -I%(cocosdir)s -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/platform/android -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/external -I%(cocosdir)s/external/lua/tolua -I%(cocosdir)s/external/lua/luajit/include -I%(cocosdir)s/cocos/scripting/lua-bindings/auto -I%(cocosdir)s/cocos/scripting/lua-bindings/manual

cocos_flags = -DANDROID -U__MINGW32__

cxxgenerator_headers = 

# extra arguments for clang
extra_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 parse
headers = %(cocosdir)s/../runtime-src/Classes/DownManager.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 = DownManager

# 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" lookup
classes_have_no_parents = 

# 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 constructor
abstract_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 lua binding自定义 实践_第1张图片

'cocos2dx_legend.ini' : ('cocos2dx_legend', 'lua_cocos2dx_legend_auto'), \

 

再次执行脚本genbindings.py等到成功就说明配置成功了,然后我们需要把生成的文件在c++中引用进来,先看生成的文件在哪里

cocos2d lua binding自定义 实践_第2张图片

 cocos2d lua binding自定义 实践_第3张图片

 在lua_module_register中把头文件添加进去,然后我们看一下生成的文件中有一个这个函数

register_all_cocos2dx_legend

TOLUA_API int register_all_cocos2dx_legend(lua_State* tolua_S)
{
	tolua_open(tolua_S);
	
	tolua_module(tolua_S,"cc",0);
	tolua_beginmodule(tolua_S,"cc");

	lua_register_cocos2dx_legend_DownManager(tolua_S);

	tolua_endmodule(tolua_S);
	return 1;
}

这个函数也是很重要的,把class类注册进lua环境中,我们需要在lua_module_register.cpp中lua_module_register调用函数,

cocos2d lua binding自定义 实践_第4张图片

 

int lua_module_register(lua_State* L)
{
    // Don't change the module register order unless you know what your are doing
    register_cocosdenshion_module(L);
    register_network_module(L);
    register_cocosbuilder_module(L);
    register_cocostudio_module(L);
    register_ui_module(L);
    register_extension_module(L);
    register_spine_module(L);
    register_cocos3d_module(L);
    register_audioengine_module(L);
	register_all_cocos2dx_legend(L);
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
    register_physics3d_module(L);
#endif
#if CC_USE_NAVMESH
    register_navmesh_module(L);
#endif
    return 1;
}

至此编译之后,lua环境中就已经可以调用我们的类DownManager 我们可以试一试看,

cc.DownManager:getInstance():pLog() 

 

void DownManager::pLog()
{
	log("DownManager - pLog !");
}

运行之后如果我们可以看到打印就说明成功了

cocos2d lua binding自定义 实践_第5张图片

 好,这样就算成功了,如果我们有很多个类需要注册进lua环境中的话,我们可用一个头文件包换所有的类,然后主要包这一个头文件就可以导出所有包含的类了。

到这里就结束了,如有错误之处还请指出。

你可能感兴趣的:(cocos2d,cocos2d,lua,游戏引擎)