最近閑著塔猾,搗鼓一下Lua調(diào)用自定義C++類够委。重點參考了下面幾篇文章像云,記錄一下流程。
http://shahdza.blog.51cto.com/2410787/1569003
https://segmentfault.com/a/1190000000631630永丝。
1.首先我在定義 了我自己的類 的頭文件 myTest.h:內(nèi)容如下
#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的內(nèi)容:
#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
內(nèi)容如下,下面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.就是執(zhí)行g(shù)enbindings.py文件蛇耀,如果你的電腦還沒有安裝python的話,就安裝一個python 2.7版本坎弯,當(dāng)我執(zhí)行 python genbindings.py會發(fā)現(xiàn)如下好幾處錯誤:
? ? 1.ImportError :no module name yaml
? ? 2 .ImportError:no module name Cheetah.Tamplate
? ? 3.dos2unix 不是內(nèi)部內(nèi)部或者外部命令
錯誤的解決辦法就是安裝上面這些python工具文件纺涤,參考了:http://blog.sina.com.cn/s/blog_8af106960101d11f.html
首選? 安裝setuptools,然后安裝pip抠忘,之后將C:\Python27\Scripts加入到系統(tǒng)環(huán)境變量中撩炊。
安裝yaml,執(zhí)行pip search pyyaml,再執(zhí)行pip install PyYAML
安裝Cheetah,執(zhí)行pip search Cheetah,在執(zhí)行pip install Cheetah
安裝dos2unix崎脉,解壓到一某個目錄下面, 并設(shè)置PATH環(huán)境變量的值指向bin目錄下拧咳。
http://waterlan.home.xs4all.nl/dos2unix/dos2unix-7.1-win32.zip
7.上面的錯誤解決了,再執(zhí)行python genbindings.py文件囚灼,如果沒啥錯誤的話骆膝,就會執(zhí)行成功,最后打印出: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代碼中就可以調(diào)用myTest類了层坠,流程就結(jié)束了殖妇。
local test = cc.myTest:create()
test:foo(100)