最近闲着,捣鼓一下Lua调用自定义C++类。重点参考了下面几篇文章,记录一下流程。
http://shahdza.blog.51cto.com/2410787/1569003
https://segmentfault.com/a/1190000000631630。
1.首先我在定义 了我自己的类 的头文件 myTest.h:内容如下
#include "cocos2d.h"
using namespace cocos2d;
class myTest : public Ref
{
public:
myTest() {};
~myTest() {};
bool init() { return true; };
CREATE_FUNC(myTest);
//Sprite* creuu();
int foo(int i);
};
2.编写了myTest.cpp的内容:
#include "myTest.h"
#include "cocos2d.h"
using namespace cocos2d;
int myTest::foo(int i)
{
//auto sprite = Sprite:create("dsafa");
return i + 100;
}
3.将 myTest.h和myTest.cpp两个文件copy到frameworks\runtime-src\Classes下面
4.配置自己的.ini文件,frameworks\cocos2d-x\tools\tolua下面一大堆.ini文件,随便copy一个.ini文件,然后重命名myTest.ini
内容如下,下面5处字体加大,且是斜体的地方,就是我们需要修改的地方,其他的不需要改变:
[myTest]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = myTest
# 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
#macro_judgement = #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/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/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include
clang_flags = -nostdinc -x c++ -std=c++11
cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/android
cocos_flags = -DANDROID
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/myTest.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 = myTest
# 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 = Controller::[getAllController getKeyStatus]
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
5.修改tolua下面的genbindings.py文件,在第129行加上:
'myTest.ini' : ('myTest', 'lua_myTest_auto'), \
6.就是执行genbindings.py文件,如果你的电脑还没有安装python的话,就安装一个python 2.7版本,当我执行 python genbindings.py会发现如下好几处错误:
1.ImportError :no module name yaml
2 .ImportError:no module name Cheetah.Tamplate
3.dos2unix 不是内部内部或者外部命令
错误的解决办法就是安装上面这些python工具文件,参考了:http://blog.sina.com.cn/s/blog_8af106960101d11f.html
首选 安装setuptools,然后安装pip,之后将C:\Python27\Scripts加入到系统环境变量中。
安装yaml,执行pip search pyyaml,再执行pip install PyYAML
安装Cheetah,执行pip search Cheetah,在执行pip install Cheetah
安装dos2unix,解压到一某个目录下面, 并设置PATH环境变量的值指向bin目录下。
http://waterlan.home.xs4all.nl/dos2unix/dos2unix-7.1-win32.zip
7.上面的错误解决了,再执行python genbindings.py文件,如果没啥错误的话,就会执行成功,最后打印出:Generating lua bindings succeeds
8.成功后,在frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto中会找到我们生成的C++的桥接文件,lua_myTest_auto.cpp和lua_myTest_auto.hpp。
9.使用 VS2013 打开frameworks\runtime-src\proj.win32下的工程。
(1)将自定义的类 myTest添加到项目工程的Classes下。
(2)将 lua_custom_api_auto.cpp、lua_custom_api_auto.hpp 添加到工程liblua的auto下。
(3)添加lualib工程的文件搜索路径。将$(EngineRoot)../runtime-src/Classes路径加进去。
(4)编辑 frameworks\runtime-src\Classes 下的入口类 AppDelegate.cpp,加入下面的
画圈的代码
(5)至此在lua代码中就可以调用myTest类了,流程就结束了。
local test = cc.myTest:create()
test:foo(100)