Cocos2d-x 3.6(Lua):LuaをCocosのカスタムC ++にバインドする方法



Cocos2d X 3 6 How Bind Lua Custom C Cocos



cocos 3.XluaのハイバージョンでC ++クラスをバインドして定義する方法についての記事を見ました。記事は非常によく書かれています。残念ながら、彼が書いたようにバインディングは失敗しました。なぜなら、その中の避けられないピットのいくつかは著者によって言及されていなかったからです。時間の経過とともに状況が変化した可能性がありますが、バインディングは失敗したに違いありません。ここでは、元の記事を引用して、表示される穴を埋めます。
元の住所

著者の環境Quick-Cocos2dx-Community3.6



Cocos2d-x2.xとCocos2d-x3.xの違い(tolua ++)

2.xバージョンでは、Cocos2d-xはtoLua ++ファイルと.pkgファイルを使用してLua環境に登録します。ただし、Cocos2d-x 3.x以降では、bindings-generatorを使用します。スクリプトはtoLua ++に置き換わります。



bindings-generatorスクリプトの動作メカニズムは次のとおりです。

1. .pkgファイルと.hファイルを書き込む必要はありません。iniファイルを定義し、Lua環境にモジュール名を登録するだけです。

2. toLua ++ツールの生成方法を理解し、Pythonスクリプトを変更して、C ++クラスを動的に分析し、ブリッジされた.hおよび.cppコードを自動的に生成し、tolua ++コマンドを呼び出さないようにしました。



3. tolua ++コマンドは呼び出されなくなりましたが、最下層は引き続きtolua_functionなどのtoLua ++ライブラリ関数を使用します。 bindings-generatorスクリプトによって生成されるコードは、toLua ++ツールによって生成されるコードとほぼ同じです。

bindings-generatorスクリプトは、toLua ++ブリッジングコードを生成するイニシアチブを習得しました。これにより、多数の.pkgファイルと.hファイルが保存されるだけでなく、Cocos2dを実現するためのカスタムコードの挿入が改善されます。 x環境(メモリ回復など)のため、Cocos2d-xは3.x以降toLua ++と.pkgを放棄し、独自に記述されたbindings-generatorスクリプトに切り替えました。これは非常に高く評価されている賢いアプローチです。

次に、bindings-generatorスクリプトの使用方法:

1.独自のC ++クラスを作成し、Cocos2d-xのメモリ回復メカニズムを使用するために、Cocos2d-xのルールに従ってcocos2d :: Refクラスを継承します。

2. .iniファイルを作成して、bindings-generatorがこの構成ファイルに基づいてC ++クラスを公開する方法を認識できるようにします。

3. bindings-generatorスクリプトを変更して、.iniファイルを読み取れるようにします。

4. bindings-generatorスクリプトを実行して、ブリッジC ++クラスメソッドを生成します。

5. VS2012を使用して、カスタムC ++クラスと生成されたブリッジファイルをプロジェクトに追加します。そうしないと、コンパイルされません。

6. AppDelegate.cppを変更し、bridgeメソッドを実行すると、カスタムC ++クラスがLua環境に登録されます。

必要なライブラリとツールキットをインストールし、関連する環境変数を構成します。Quick-Cocos2dx-Community tools tolua README.mdownの指示に従ってください。

* `android-ndk-r9b`がインストールされていることを確認してください。でなければなりませんandroid-ndk-r9b
* python2.7.3(32ビット)を(http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi)からダウンロードします。
*インストールされているPythonのパス(C: Python27など)を「PATH」という名前のWindows環境変数に追加します。
* http://pyyaml.org/download/pyyaml/PyYAML-3.10.win32-py2.7.exeからpyyamlをダウンロードしてインストールします。
* pyCheetahをhttps://raw.github.com/dumganhar/my_old_cocos2d-x_backup/download/downloads/Cheetah.zipからダウンロードし、「C: Python27 Lib site-packages」に解凍します。
*環境変数を設定します( `NDK_ROOT`)

1つ目はカスタムC ++クラスです。プロジェクトのframeworks / runtime-src / Classes /ディレクトリにファイルを保存することに慣れています。

フレームワーク/ランタイム-src / Classes / MyClass.h

#include 'cocos2d.h' using namespace cocos2d class MyClass : public Ref { public: MyClass() {} ~MyClass() {} bool init() { return true } CREATE_FUNC(MyClass) int foo(int i) }フレームワーク/ランタイム-src / Classes / MyClass.cpp
#include 'MyClass.h' int MyClass::foo(int i) { return i + 100 }次に、.iniファイルを書き込みます。 Quick-Cocos2dx-Community tools toluaディレクトリにgenbindings.pyスクリプトと多くの.iniファイルがあります。これらは、bindings-generatorの実際の実行環境です。 cocos2dx_spine.iniのように、コンテンツの少ない.iniファイルを見つけてコピーし、名前をMyClass.iniに変更するだけです。次のように記述しました。
[myclass] # the prefix to be added to the generated functions. You might or might not use this in your own # templates prefix = MineClass # 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 = my 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 -U __SSE__ 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 = E:/cocosProj/myluaGame/frameworks/runtime-src/Classes/MyClass.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 = MyClass # 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

変更する部分は次のとおりです。

[myclass] #File identification prefix = myclass target_namespace = headers = E:/cocosProj/myluaGame/frameworks/runtime-src/Classes/MyClass.h #Header file path classes = MyClass skip = abstract_classes =最初の[myclass]を変更しなかったので、長い間間違えました Quick-Cocos2dx-Community tools toluaの下にあるgenbindings.pyを変更します。このファイルによってエクスポートされるcocosクラスが多すぎて、時間がかかります。 1つをコピーして、独自のクラスのみをインポートすることをお勧めします。genbindings_myclass.pyという名前を付けます
生成ディレクトリをプロジェクトに設定し、genbindings_myclass.pyを開きます
1 output_dir = '%s/cocos/scripting/lua-bindings/auto' % project_root
への変更
1 output_dir = '%s/tests/lua-empty-test/project/Classes/auto' % project_root

コマンドパラメータを変更し、
1 2 3 4 5 6 7 cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), 'cocos2dx_extension.ini' : ('cocos2dx_extension', 'lua_cocos2dx_extension_auto'), 'cocos2dx_ui.ini' : ('cocos2dx_ui', 'lua_cocos2dx_ui_auto'), 'cocos2dx_studio.ini' : ('cocos2dx_studio', 'lua_cocos2dx_studio_auto'), 'cocos2dx_spine.ini' : ('cocos2dx_spine', 'lua_cocos2dx_spine_auto'), 'cocos2dx_physics.ini' : ('cocos2dx_physics', 'lua_cocos2dx_physics_auto'), }
への変更
1 cmd_args = {'myclass.ini' : ('myclass', 'lua_myclass_auto') }

Pythonの場合は注意してくださいgenbindings_myclass.py、エラーを報告する必要があります! libclangが見つけられなかったエラーを報告してください。以前のバージョンの非互換性が原因である可能性があります。最新のbindings-generatorアドレスhttps://github.com/guojian822/bindings-generatorをダウンロードする必要があります
ダウンロード後、ファイル名はbindings-generator-developに変更されますbindings-generator、Quick-Cocos2dx-Community toolsを下に置きますバインディングを置き換えます-ジェネレーター、バックアップするのが最善です
その後、実行することができますPythongenbindings_myclass.pyを使用すると、次の図に示すように、バインドされたブリッジクラスlua_MyClass_auto.cppとlua_MyClass_auto.hppを生成し、それらをプロジェクトディレクトリにコピーして、追加できます。

コンパイルして実行

Classes / lua_module_register.hファイルを開き、ヘッダーファイルを追加します

1 #include 'tolua++/lua_MyClass_auto.hpp'

static int lua_module_register(lua_State * L)に登録関数を追加します

1 register_all_MineClass(L)

vs2012コンパイルエラーの場合、genbindings.pyスクリプトで生成されたファイルパスの置き換えなどの特別な処理があると、ソースファイルと生成されたファイルがプロジェクトに追加されないと推定されます。vs2012環境->プロパティに注意してください。 -> c / c ++->追加のインクルードディレクトリ、パスを追加します。

Luaコード:

1 2 3 4 5 6 function myadd(x, y) -- customize local test = my.MyClass:create() print('lua bind: ' .. test:foo(99)) return x + y end

コンパイルして実行します。