cocos2dx lua-binding<二>用cocos code ide

cocos2dx lua-binding<二>用cocos code ide,第1张

概述<1>frameworks/runtime-src/Classes下: 建立要绑定的.h和.cpp文件 <2>建立后,发现文件不在Classes中,就复制一份到Classes中 <3>frameworks/cocos2d-x/tools/tolua下: 1,cocos2dx_custom.ini [cocos2dx_custom]# the prefix to be added to the g

<1>frameworks/runtime-src/Classes下:

建立要绑定的.h和.cpp文件


<2>建立后,发现文件不在Classes中,就复制一份到Classes中


<3>frameworks/cocos2d-x/tools/tolua下:

1,cocos2dx_custom.ini

[cocos2dx_custom]# the prefix to be added to the generated functions. You might or might not use this in your own# templatesprefix = cocos2dx_custom# create a target namespace (in JavaScript,this would create some code like the equiv. to `ns = ns || {}`)# all classes will be embedded in that namespacetarget_namespace = ccandroID_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/includeandroID_flags = -D_SIZE_T_defineD_ clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/my -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/androIDcocos_flags = -DANDROIDcxxgenerator_headers = # extra arguments for clangextra_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 parseheaders = %(cocosdir)s/../runtime-src/Classes/CustomClass.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 = CustomClass.*# 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" lookupclasses_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 constructorabstract_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
2,genbindings.py
#!/usr/bin/python# This script is used to generate luabinding glue codes.# AndroID ndk version must be ndk-r9b.import sysimport os,os.pathimport shutilimport ConfigParserimport subprocessimport refrom contextlib import contextmanagerdef _check_ndk_root_env():    ''' Checking the environment NDK_ROOT,which will be used for building    '''    try:        NDK_ROOT = os.environ['NDK_ROOT']    except Exception:        print "NDK_ROOT not defined. Please define NDK_ROOT in your environment."        sys.exit(1)    return NDK_ROOTdef _check_python_bin_env():    ''' Checking the environment PYTHON_BIN,which will be used for building    '''    try:        PYTHON_BIN = os.environ['PYTHON_BIN']    except Exception:        print "PYTHON_BIN not defined,use current python."        PYTHON_BIN = sys.executable    return PYTHON_BINclass CmdError(Exception):    pass@contextmanagerdef _pushd(newDir):    prevIoUsDir = os.getcwd()    os.chdir(newDir)    yIEld    os.chdir(prevIoUsDir)def _run_cmd(command):    ret = subprocess.call(command,shell=True)    if ret != 0:        message = "Error running command"        raise CmdError(message)def main():    cur_platform= '??'    llvm_path = '??'    ndk_root = _check_ndk_root_env()    # del the " in the path    ndk_root = re.sub(r"\"","",ndk_root)    python_bin = _check_python_bin_env()    platform = sys.platform    if platform == 'win32':        cur_platform = 'windows'    elif platform == 'darwin':        cur_platform = platform    elif 'linux' in platform:        cur_platform = 'linux'    else:        print 'Your platform is not supported!'        sys.exit(1)    if platform == 'win32':        x86_llvm_path = os.path.abspath(os.path.join(ndk_root,'toolchains/llvm-3.3/prebuilt','%s' % cur_platform))    else:        x86_llvm_path = os.path.abspath(os.path.join(ndk_root,'%s-%s' % (cur_platform,'x86')))    x64_llvm_path = os.path.abspath(os.path.join(ndk_root,'x86_64')))    if os.path.isdir(x86_llvm_path):        llvm_path = x86_llvm_path    elif os.path.isdir(x64_llvm_path):        llvm_path = x64_llvm_path    else:        print 'llvm toolchain not found!'        print 'path: %s or path: %s are not valID! ' % (x86_llvm_path,x64_llvm_path)        sys.exit(1)    project_root = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..'))    cocos_root = os.path.abspath(os.path.join(project_root,''))    cxx_generator_root = os.path.abspath(os.path.join(project_root,'tools/bindings-generator'))    # save config to file    config = ConfigParser.ConfigParser()    config.set('DEFAulT','androIDndkdir',ndk_root)    config.set('DEFAulT','clangllvmdir',llvm_path)    config.set('DEFAulT','cocosdir',cocos_root)    config.set('DEFAulT','cxxgeneratordir',cxx_generator_root)    config.set('DEFAulT','extra_flags','')    # To fix parse error on windows,we must difine __WCHAR_MAX__ and undefine __MINGW32__ .    if platform == 'win32':        config.set('DEFAulT','-D__WCHAR_MAX__=0x7fffffff -U__MINGW32__')    conf_ini_file = os.path.abspath(os.path.join(os.path.dirname(__file__),'userconf.ini'))    print 'generating userconf.ini...'    with open(conf_ini_file,'w') as configfile:      config.write(configfile)    # set proper environment variables    if 'linux' in platform or platform == 'darwin':        os.putenv('LD_liBRARY_PATH','%s/libclang' % cxx_generator_root)    if platform == 'win32':        path_env = os.environ['PATH']        os.putenv('PATH',r'%s;%s\libclang;%s\tools\win32;' % (path_env,cxx_generator_root,cxx_generator_root))    try:        tolua_root = '%s/tools/tolua' % project_root        output_dir = '%s/cocos/scripting/lua-bindings/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_custom.ini' : ('cocos2dx_custom','lua_cocos2dx_custom'),\                    }        target = 'lua'        generator_py = '%s/generator.py' % cxx_generator_root        for key in cmd_args.keys():            args = cmd_args[key]            cfg = '%s/%s' % (tolua_root,key)            print 'Generating bindings for %s...' % (key[:-4])            command = '%s %s %s -s %s -t %s -o %s -n %s' % (python_bin,generator_py,cfg,args[0],target,output_dir,args[1])            _run_cmd(command)        if platform == 'win32':            with _pushd(output_dir):                _run_cmd('dos2unix *')        print '---------------------------------'        print 'Generating lua bindings succeeds.'        print '---------------------------------'    except Exception as e:        if e.__class__.__name__ == 'CmdError':            print '---------------------------------'            print 'Generating lua bindings fails.'            print '---------------------------------'            sys.exit(1)        else:            raise# -------------- main --------------if __name__ == '__main__':    main()

<4>cd 到 frameworks/cocos2d-x/tools/tolua下面,运行./genbindings.py生成绑定文件


<5>xcode下:

1,

cocos2d_lua_bindings.xcodeproj下的auto文件夹里面添加生成的.hpp和.cpp文件

2,

Classes下添加导出的.hpp 和 .cpp文件及其对应的.h 和 .cpp文件

3,

TARGETS Mac下:

header Search Paths中添加搜索路径:

<span >$(SRCROOT)/../../../../../runtime-src/Classes</span>


<6>cocos code IDe 下:

local function main()    collectgarbage("collect")    -- avoID memory leak    collectgarbage("setpause",100)    collectgarbage("setstepmul",5000)    cc.fileUtils:getInstance():addSearchPath("src")    cc.fileUtils:getInstance():addSearchPath("res")    cc.Director:getInstance():getopenGLVIEw():setDesignResolutionSize(480,320,0)    local var = cc.CustomClass:create():helloMsg()    print(var)    --create scene    local scene = require("GameScene")    local gameScene = scene.create()    gameScene:playBgMusic()    if cc.Director:getInstance():getRunningScene() then        cc.Director:getInstance():replaceScene(gameScene)    else        cc.Director:getInstance():runWithScene(gameScene)    endend

搞定!!! 总结

以上是内存溢出为你收集整理的cocos2dx lua-binding<二>用cocos code ide全部内容,希望文章能够帮你解决cocos2dx lua-binding<二>用cocos code ide所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1061639.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存