cocos2d-x 之quick-ini

cocos2d-x 之quick-ini,第1张

概述最近要学习在quick上用lua编写游戏了,在学习之前,很想知道lua与c++之间的关系,于是找到了tolua++, tolua++是一种第三方的软件包,可以为Lua提供面向对象的特性,这样我们就可以使用Lua来编写使用C++语言库的脚本文件。在cocos中,bindings-generator脚本代替了toLua++。更快的将C++类注册进Lua环境。      首先在quick 3.5下新建一
最近要学习在quick上用lua编写游戏了,在学习之前,很想知道lua与c++之间的关系,于是找到了tolua++,tolua++是一种第三方的软件包,可以为Lua提供面向对象的特性,这样我们就可以使用Lua来编写使用C++语言库的脚本文件。在cocos中,bindings-generator脚本代替了tolua++。更快的将C++类注册进Lua环境。  

首先在quick 3.5下新建一个项目,cd进入工具-bin目录下,输入:cocos new NB -l lua -d ./Projects。这样就新建了项目NB。进入:D:\NB\frameworks\cocos2d-x\tools\tolua 目录你会看见很多ini文件,首先打开README.mdown。这里面有环境的配置,为了方便以后的记忆,我还是写出来了。我是在win7下建的环境。

第一步:确定电脑中有`androID-ndk-r9b`.

第二步: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.10.win32-py2.7.exe and install it.(这个很重要,不然会执行python时报错,什么yaml的错)

第五步: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`)

第七步:run "genbindings.py".

环境配好后,在D:\NB\frameworks\cocos2d-x\tools\tolua 目录下,看见genbindings.py,可以在里面改,也可以重新复制一份出来改。我是复制一份出来改的。

1、先说ini,任意打开一份ini,

[cocos2dx_spine]  (ini文件名)
<span >#prefix会被添加到生成的函数.你也可以选择不添加这个到你的模板</span>prefix = cocos2dx_spine # 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 = spandroID_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++11 -U __SSE__cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -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 parse (类所在路劲)headers = %(cocosdir)s/cocos/editor-support/spine/spine-cocos2dx.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 = SkeletonRenderer SkeletonAnimation (需要注册的类,有多个需要用空格空开)# 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.(在lua中不能访问的函数名,没有,=后面就不写)skip = SkeletonRenderer::[findBone findSlot getAttachment setAttachment update draw createWithData],*::[update draw drawSkeleton],SkeletonAnimation::[setAnimationStateData createWithData addAnimation getCurrent setAnimation onAnimationStateEvent onTrackEntryEvent getState createWithfile](下面全空的默认OK)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 = Skeleton SkeletonAnimation# 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、在D:\NB\frameworks\runtime-src\Classes目录下编写自己的类,也可以在其他目录下。

(这个是引用:http://segmentfault.com/a/1190000000631630)

MyClass.h

#pragma once#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;}

3、MyClass.ini

[MyClass] (改)prefix = MyClass (改)target_namespace = my (改)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/includeandroID_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/editor-support -I%(cocosdir)s/cocos/platform/androIDcocos_flags = -DANDROIDcxxgenerator_headers = 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 headers = %(cocosdir)s/../runtime-src/Classes/MyClass.h (改,这是自己类路劲,可以自己改)classes = MyClass (改)skip =  (改)rename_functions = rename_classes =remove_prefix = classes_have_no_parents = base_classes_to_skip = Ref ProcessBaseabstract_classes = script_control_cpp = no
4、复制一份genbindings.py为genbindings_MyClass。类容如下:

output_dir = '%s/cocos/scripting/lua-bindings/auto' % project_root (这个路径是生成lua_MyClass_auto 的路径,可以自己改
cmd_args = { 'MyClass.ini' : ('MyClass','lua_MyClass_auto')} 只生成MyClass的注册类。

5、在D:\NB\frameworks\cocos2d-x\tools\tolua目录下,执行python genbindings_myclass.py。没问题的话,你会在output_dir的路径下看见

lua_MyClass_auto.cpp 和 lua_MyClass_auto.hpp 两个文件。

在D:\NB\frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto\API目录下看见MyClass.lua
6、在D:\NB\frameworks\runtime-src\Classes\AppDelegate.cpp中加入:#include "lua_MyClass_auto.hpp"

LuaEngine* engine = LuaEngine::getInstance();后面加入:register_all_myclass(engine->getLuaStack()->getLuaState());

7、编译运行,这样,MyClass这个类就被导出到lua了

8、在main.lua中尝试:

local test = my.MyClass:create()print("lua bind: " .. test:foo(99))

(仅个人理解,如有不对,还请指正)

参考链接:http://www.cocoachina.com/bbs/read.PHP?tID-196416.HTML

http://segmentfault.com/a/1190000000631630

总结

以上是内存溢出为你收集整理的cocos2d-x 之quick-ini全部内容,希望文章能够帮你解决cocos2d-x 之quick-ini所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存