quick-3.5 绑定自定义C++类到Lua并使用cocos code ide 调式
2015-09-14 17:39
661 查看
在这里主要记录怎么绑定自定义C++类到Lua
参考文章在这里【绑定自定义类至Lua】,在我操作过程中略有不同,现在把步骤记录下来。
1.打开引擎目录下\tools\tolua\README.mdown,安装相应软件
2.项目目录\frameworks\cocos2d-x\tools\tolua,复制cocos2dx_cocosbuilder->custom.ini,genbindings->custom-genbindings.py
3.修改复制后的文件
custion.ini
主要修改:
第1行:[custom_socket]
第4行:prefix = custom_socket
第8行:target_namespace =
第26行:headers = %(cocosdir)s/../runtime-src/Classes/net/SocketModule.h
第30行:classes = ClientSocket.* CMD_Command.* ConnectHandler.* RecvHandler.*
其余根据具体来设置,主要说一下第26行是创建一个头文件,包含导出类的相关头文件,30行是导出的类定义
custom-genbindings.py
主要修改:
第134行: output_dir = '%s/../runtime-src/Classes/lua-bindings/auto' % project_root
第136行: cmd_args = {'custom.ini' : ('custom_socket', 'lua_custom_socket_auto'),\
}
修改好后,在命令行执行:custom-genbindings.py
如果没有错误的话会生成lua_custom_socket_auto.hpp和lua_custom_socket_auto.cpp
接下来注册自定义模块:
1.添加到工程
2.引入到AppDelegate
3.注册模块
使用cocos code IDE 1.2.0断点调试,由于使用调试的模拟器是另外一个程序quick-3.5/tools/simulator,所以需要将脚本生成的文件在simulator工程再添加一遍:
生成新的模拟器,然后在lua中测试,就可以调用自定义的C++类了
QQ群:239759131 cocos 技术交流 欢迎您
参考文章在这里【绑定自定义类至Lua】,在我操作过程中略有不同,现在把步骤记录下来。
1.打开引擎目录下\tools\tolua\README.mdown,安装相应软件
2.项目目录\frameworks\cocos2d-x\tools\tolua,复制cocos2dx_cocosbuilder->custom.ini,genbindings->custom-genbindings.py
3.修改复制后的文件
custion.ini
[custom_socket] # the prefix to be added to the generated functions. You might or might not use this in your own # templates prefix = custom_socket # 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 = 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 -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -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/net/SocketModule.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 = ClientSocket.* CMD_Command.* ConnectHandler.* RecvHandler.* # 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
主要修改:
第1行:[custom_socket]
第4行:prefix = custom_socket
第8行:target_namespace =
第26行:headers = %(cocosdir)s/../runtime-src/Classes/net/SocketModule.h
第30行:classes = ClientSocket.* CMD_Command.* ConnectHandler.* RecvHandler.*
其余根据具体来设置,主要说一下第26行是创建一个头文件,包含导出类的相关头文件,30行是导出的类定义
custom-genbindings.py
#!/usr/bin/python # This script is used to generate luabinding glue codes. # Android ndk version must be ndk-r9b. import sys import os, os.path import shutil import ConfigParser import subprocess import re from contextlib import contextmanager def _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_ROOT def _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_BIN class CmdError(Exception): pass @contextmanager def _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)) if not os.path.exists(x86_llvm_path): x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.4/prebuilt', '%s' % cur_platform)) else: x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.3/prebuilt', '%s-%s' % (cur_platform, 'x86'))) if not os.path.exists(x86_llvm_path): x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.4/prebuilt', '%s-%s' % (cur_platform, 'x86'))) x64_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.3/prebuilt', '%s-%s' % (cur_platform, 'x86_64'))) if not os.path.exists(x64_llvm_path): x64_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.4/prebuilt', '%s-%s' % (cur_platform, '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', 'extra_flags', '-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/../runtime-src/Classes/lua-bindings/auto' % project_root cmd_args = {'custom.ini' : ('custom_socket', 'lua_custom_socket_auto'),\ } 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) 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()
主要修改:
第134行: output_dir = '%s/../runtime-src/Classes/lua-bindings/auto' % project_root
第136行: cmd_args = {'custom.ini' : ('custom_socket', 'lua_custom_socket_auto'),\
}
修改好后,在命令行执行:custom-genbindings.py
如果没有错误的话会生成lua_custom_socket_auto.hpp和lua_custom_socket_auto.cpp
接下来注册自定义模块:
1.添加到工程
2.引入到AppDelegate
3.注册模块
使用cocos code IDE 1.2.0断点调试,由于使用调试的模拟器是另外一个程序quick-3.5/tools/simulator,所以需要将脚本生成的文件在simulator工程再添加一遍:
生成新的模拟器,然后在lua中测试,就可以调用自定义的C++类了
--test self.socket_ = ClientSocket:new() self.socket_:retain() print("create success")
QQ群:239759131 cocos 技术交流 欢迎您
相关文章推荐
- Linux 自检和 SystemTap
- Python 七步捉虫法
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 路由器的配置与调试
- 对于技术人员的出现了运行时间错误,是否要进行调试的解决方法
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解