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

最近閑著塔猾,搗鼓一下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.cpplua_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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市破花,隨后出現(xiàn)的幾起案子谦趣,更是在濱河造成了極大的恐慌,老刑警劉巖座每,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件前鹅,死亡現(xiàn)場離奇詭異,居然都是意外死亡峭梳,警方通過查閱死者的電腦和手機舰绘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來葱椭,“玉大人捂寿,你說我怎么就攤上這事》踉耍” “怎么了秦陋?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長治笨。 經(jīng)常有香客問我驳概,道長,這世上最難降的妖魔是什么旷赖? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任顺又,我火速辦了婚禮,結(jié)果婚禮上等孵,老公的妹妹穿的比我還像新娘稚照。我一直安慰自己,他們只是感情好流济,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布锐锣。 她就那樣靜靜地躺著腌闯,像睡著了一般绳瘟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上姿骏,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天糖声,我揣著相機與錄音,去河邊找鬼。 笑死蘸泻,一個胖子當(dāng)著我的面吹牛琉苇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播悦施,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼并扇,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了抡诞?” 一聲冷哼從身側(cè)響起穷蛹,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎昼汗,沒想到半個月后肴熏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡顷窒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年蛙吏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鞋吉。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡鸦做,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谓着,到底是詐尸還是另有隱情馁龟,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布漆魔,位于F島的核電站坷檩,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏改抡。R本人自食惡果不足惜矢炼,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望阿纤。 院中可真熱鬧句灌,春花似錦、人聲如沸欠拾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽藐窄。三九已至资昧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間荆忍,已是汗流浹背格带。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工撤缴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叽唱。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓屈呕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親棺亭。 傳聞我的和親對象是個殘疾皇子虎眨,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容

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