Lua調(diào)用自定義C++類 ,cocos2dx 3.2

最近閑著,搗鼓一下Lua調(diào)用自定義C++類。重點(diǎn)參考了下面幾篇文章,記錄一下流程。

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.cpplua_myTest_auto.hpp



9.使用 VS2013 打開frameworks\runtime-src\proj.win32下的工程。

(1)將自定義的類 myTest添加到項(xiàng)目工程的Classes下。


(2)將 lua_custom_api_auto.cpp、lua_custom_api_auto.hpp 添加到工程liblua的auto下。


(3)添加lualib工程的文件搜索路徑。將$(EngineRoot)../runtime-src/Classes路徑加進(jìn)去。

(4)編輯 frameworks\runtime-src\Classes 下的入口類 AppDelegate.cpp,加入下面的

畫圈的代碼


(5)至此在lua代碼中就可以調(diào)用myTest類了,流程就結(jié)束了。

local test = cc.myTest:create()

test:foo(100)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 說明本次redis集群安裝在rhel6.8 64位機(jī)器上,redis版本為3.2.8,redis的gem文件版本為...
    讀或?qū)?/span>閱讀 15,616評論 3 9
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • 【本文由“木易林”發(fā)布,2017年02月07日】 目前項(xiàng)目,使用了Lua腳本,至于使用Lua的好處不再贅述了。于是...
    木易林1閱讀 608評論 0 0
  • 序 近期工作忙碌,為了趕SegmentFault for Android 4.0版,到了發(fā)瘋的程度。我來匯報(bào)一個進(jìn)...
    Gemini閱讀 1,171評論 0 4
  • 緩慢生長 今天,和往常一樣,又似乎不一樣。剛刷了QQ群,還是一如既往的“窺屏”,看著一條條往上跳的消息,自己卻插不...
    萬能的小明閱讀 472評論 1 1

友情鏈接更多精彩內(nèi)容