加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-Lua 3.12 生成绑定 导出C++类给LUA调用( tolua genbindi

发布时间:2020-12-14 17:27:37 所属栏目:百科 来源:网络整理
导读:工具: Python 、pyyaml、pyCheetah 都用 32 位版本 (可避免很多错误) (ps:tolua目录下README.mdown里有下载链接) frameworkscocos2d-xtoolstoluaREADME.mdown 例子: MyClass.h [cpp] view plain copy #include"cocos2d.h" using namespace cocos2d;

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

(ps:tolua目录下README.mdown里有下载链接)

frameworkscocos2d-xtoolstoluaREADME.mdown


例子:

MyClass.h

[cpp] view plain copy
  1. #include"cocos2d.h"
  2. usingnamespacecocos2d;
  3. classMyClass:publicRef
  4. {public:
  5. MyClass(){};
  6. ~MyClass(){};
  7. boolinit(){returntrue;};
  8. CREATE_FUNC(MyClass);
  9. intfoo(inti);
  10. };


MyClass.cpp

#include"MyClass.h"

  • intMyClass::foo(inti)
  • {
  • returni+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


    生成frameworkscocos2d-xtoolstoluaMyGenbindings.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'),根据需要添加的
    }

    运行pythonmygenbindings.py 即生成在输出目录frameworkscocos2d-xtoolstoluaauto

    遇到问题参考:

    一、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:Python27Libsite-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 "cocosscriptingauto-generatedlua-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/distutils-sig@python.org/msg10512.html

    importsys

    from_winreg*

    # tweak as necessary

    version = sys.version[:3]

    installpath = sys.prefix

    regpath ="SOFTWAREPythonPythoncore%s"% (version)

    installkey ="InstallPath"

    pythonkey ="PythonPath"

    pythonpath ="%s;%sLib;%sDLLs"% (

    installpath,installpath,installpath

    )

    defRegisterPy():

    try:

    reg = OpenKey(HKEY_CURRENT_USER,regpath)

    exceptEnvironmentError as e:

    reg = CreateKey(HKEY_CURRENT_USER,regpath)

    SetValue(reg,installkey,REG_SZ,installpath)

    CloseKey(reg)

    :

    print"*** Unable to register!"

    return

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

    if(QueryValue(reg,installkey) == installpathand

    QueryValue(reg,pythonkey) == pythonpath):

    "=== Python""is already registered!"

    return

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

    __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 到下载文件夹的目录,

    输入命令:sudopythonsetup.py install即可

    如果是windows提示这个错误,那么也是下载Cheetan这个库,然后安装


    五、

    使用PYTHONPATH环境变量,在这个环境变量中输入相关的路径,不同的路径之间用逗号(英文的!)分开,如果PYTHONPATH 变量还不存在,可以创建它!


    这里的路径会自动加入到sys.path中,而且可以在不同的python版本中共享,应该是一样较为方便的方法。


    unzip it to "C:Python27Libsite-packages" 若运行出现错误,可以

    设置环境变量:PYTHONPATH

    D:AndroidWin764Python27libssite-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);

    //frameworkscocos2d-xtoolstoluaautolua_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脚本了。

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读