Cocos2d-Lua 3.12 生成綁定 導出C++類給LUA調用( tolua genbindings.py 的使用)

工具:Python、pyyaml、pyCheetah都用32位版本 (可避免很多錯誤)

(ps:tolua目錄下README.mdown裏有下載鏈接)

frameworks\cocos2d-x\tools\tolua\README.mdown


例子:

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);
};


MyClass.cpp

#include "MyClass.h"
int MyClass::foo(int i)
{
  return i + 100;
}

生成所需的配置文件MyClass.ini 重點提示 可從其他.ini文件複製修改

[MyClass]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates修改
prefix = MyClass

# all classes will be embedded in that namespace修改
target_namespace = my
#修改
#headers = %(cocosdir)s/../runtime-src/Classes/MyClass.h
headers = MyClass.h

#修改
classes = MyClass


生成frameworks\cocos2d-x\tools\tolua\MyGenbindings.py

        tolua_root = '%s/tools/tolua' % project_root
        output_dir = '%s/tools/tolua/auto' % project_root  輸出目錄,可自定義,根據需要

        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'), \
                    #'cocos2dx_experimental_video.ini' : ('cocos2dx_experimental_video', 'lua_cocos2dx_experimental_video_auto'), \
                    #'cocos2dx_experimental.ini' : ('cocos2dx_experimental', 'lua_cocos2dx_experimental_auto'), \
                    #'cocos2dx_controller.ini' : ('cocos2dx_controller', 'lua_cocos2dx_controller_auto'), \
                    #'cocos2dx_cocosbuilder.ini': ('cocos2dx_cocosbuilder', 'lua_cocos2dx_cocosbuilder_auto'), \
                    #'cocos2dx_cocosdenshion.ini': ('cocos2dx_cocosdenshion', 'lua_cocos2dx_cocosdenshion_auto'), \
                    #'cocos2dx_3d.ini': ('cocos2dx_3d', 'lua_cocos2dx_3d_auto'), \
                    #'cocos2dx_audioengine.ini': ('cocos2dx_audioengine', 'lua_cocos2dx_audioengine_auto'), \
                    #'cocos2dx_csloader.ini' : ('cocos2dx_csloader', 'lua_cocos2dx_csloader_auto'), \
                    #'cocos2dx_experimental_webview.ini' : ('cocos2dx_experimental_webview', 'lua_cocos2dx_experimental_webview_auto'), \
                    #'cocos2dx_physics3d.ini' : ('cocos2dx_physics3d', 'lua_cocos2dx_physics3d_auto'), \
                    #'cocos2dx_navmesh.ini' : ('cocos2dx_navmesh', 'lua_cocos2dx_navmesh_auto'), \

'MyClass.ini' : ('MyClass', 'lua_MyClass_auto'),   根據需要添加的
                    }

運行python mygenbindings.py 即生成在輸出目錄frameworks\cocos2d-x\tools\tolua\auto

遇到問題參考:

一、How to Use bindings-generator
==================
On Windows:
------------
* Make sure that you have installed `android-ndk-r10c` or later.
* Download python2.7.3 (32bit) from (http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi).
* Add the installed path of python (e.g. C:\Python27) to windows environment variable named 'PATH'.
* Download pyyaml from http://pyyaml.org/download/pyyaml/PyYAML-3.11.win32-py2.7.exe and install it.
* Download pyCheetah from https://raw.github.com/dumganhar/my_old_cocos2d-x_backup/download/downloads/Cheetah.zip, unzip it to "C:\Python27\Lib\site-packages"
* Set environment variables `NDK_ROOT` and `PYTHON_BIN`
* Go to "cocos2d-x/tools/tolua" folder, and run "genbindings.py". The generated codes will be under "cocos\scripting\auto-generated\lua-bindings".


二、python version 2.7 required,which was not found in the registry

方法:新建一個register.py文件,把一下代碼貼進去,保存

#

# script to register Python 2.0 or later for use with win32all

# and other extensions that require Python registry settings

#

# written by Joakim Loew for Secret Labs AB / PythonWare

#

# source:

# http://www.pythonware.com/products/works/articles/regpy20.htm

#

# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/[email protected]/msg10512.html

 import sys

 from _winregimport *

 # tweak as necessary

version = sys.version[:3]

installpath = sys.prefix

 

regpath ="SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)

installkey ="InstallPath"

pythonkey ="PythonPath"

pythonpath ="%s;%s\\Lib\\;%s\\DLLs\\" % (

    installpath, installpath, installpath

)

 def RegisterPy():

    try:

        reg = OpenKey(HKEY_CURRENT_USER, regpath)

    except EnvironmentError as e:

        try:

            reg = CreateKey(HKEY_CURRENT_USER, regpath)

            SetValue(reg, installkey, REG_SZ, installpath)

            SetValue(reg, pythonkey, REG_SZ, pythonpath)

            CloseKey(reg)

        except:

            print "*** Unable to register!"

            return

        print "--- Python", version, "is now registered!"

        return

    if (QueryValue(reg, installkey) == installpathand

        QueryValue(reg, pythonkey) == pythonpath):

        CloseKey(reg)

        print "=== Python", version, "is already registered!"

        return

    CloseKey(reg)

    print "*** Unable to register!"

    print "*** You probably have another Python installation!"

 if __name__ =="__main__":

    RegisterPy()

win7是 64的原因,在安裝python(32位)時,如果選擇只爲當前用戶,以上問題是不會出現的,如果選擇所有用戶,那就用上面的方法解決吧。

三、TranslationUnitLoadError: Error parsing translation unit. 的提示錯誤,基本都是.ini文件沒有配置正確,仔細檢查一下 .ini文件裏的 “headers = ”指向的路徑是否正確


四、ImportError: No module named Cheetah.Template

從錯誤提示就知道缺少cheetah庫,所以先從http://pythonhosted.org//Cheetah/這個地址下載庫,打開終端,cd 到下載文件夾的目錄,

 

輸入命令:sudo python setup.py install即可

如果是windows提示這個錯誤,那麼也是下載Cheetan這個庫,然後安裝



五、

使用PYTHONPATH環境變量,在這個環境變量中輸入相關的路徑,不同的路徑之間用逗號(英文的!)分開,如果PYTHONPATH 變量還不存在,可以創建它!


這裏的路徑會自動加入到sys.path中,而且可以在不同的python版本中共享,應該是一樣較爲方便的方法。


unzip it to "C:\Python27\Lib\site-packages" 若運行出現錯誤,可以

設置環境變量:PYTHONPATH

D:\AndroidWin764\Python27\libs\site-packages


如何使用生成的文件:

一、把生成的文件添加至項目中,編輯AppDelegate.cpp文件

修改applicationDidFinishLaunching方法,register_all_*加上是生成的的類名。必須放在engine->executeScriptFile前面。

    // register lua module
    auto engine = LuaEngine::getInstance();

    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* L = engine->getLuaStack()->getLuaState();
    lua_module_register(L);

//frameworks\cocos2d-x\tools\tolua\auto\lua_MyClass_auto.cpp

//register_all_MyClass(lua_State* tolua_S)

register_all_MyClass(engine->getLuaStack()->getLuaState());


    register_all_packages();

    if (engine->executeScriptFile("src/main.lua"))
    {
        return false;
    }

然後,可以編輯LUA腳本了。




























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章