cocos2d-x 之quick-ini

前端之家收集整理的这篇文章主要介绍了cocos2d-x 之quick-ini前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
最近要学习在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 style="color: rgb(85,85,85); font-family: 'Microsoft Yahei',sans-serif; font-size: 15px; line-height: 26px; text-indent: 30px;">#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 = sp

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/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/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" 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 = 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/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/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android
cocos_flags = -DANDROID
cxxgenerator_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 ProcessBase
abstract_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

原文链接:https://www.f2er.com/cocos2dx/341458.html

猜你在找的Cocos2d-x相关文章