Cocos2dx-JS绑定C++大致流程

Cococs2dx-JS 版本3.12,环境:Mac
。这里有两种情况,为修改引擎底层方法和自定义新的C++类。

首先为了保证genbindings.py 正常运行 你需要下3样东西 已安装的请略过

  1. python 2.7.x:目前引擎里面的bindings-generator只支持python2.7.x版本
  2. py-ymal(http://pyyaml.org/wiki/PyYAML):这是python的一个第三方包,下载页有不同系统的安装方式,可以参考generator.py会调用到这个包。
  3. cheetah(http://www.cheetahtemplate.org/):这也是python的一个第三方包,一个模板,generator.py会调用到这个包.

安装目录执行 python setup.py install 进行安装.

修改引擎底层方法

这个比较简单,这里做的是给UIImageView添加了一个public方法。然后在js中用一个UIImageView对象去调用这个方法。
首先在UIImageView.cpp中添加如下方法:

void ImageView::getImageDes()
{
    CCLOG("this is getImageDes!!");
}

然后到工程/frameworks/cocos2d-x/tools/tojs目录下执行python脚本genbindings.py。(可以对这个脚本修改,然后只是重新绑定UIImageView相关的文件)。然后就可以在/frameworks/cocos2d-x/cocos/scripting/js-bindings/auto目录下的jsb_cocos2dx_ui_auto.cpp文件中看到相应的绑定内容。如下:

bool js_cocos2dx_ui_ImageView_getImageDes(JSContext *cx, uint32_t argc, jsval *vp)
{
    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
    js_proxy_t *proxy = jsb_get_js_proxy(obj);
    cocos2d::ui::ImageView* cobj = (cocos2d::ui::ImageView *)(proxy ? proxy->ptr : NULL);
    JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_ui_ImageView_getImageDes : Invalid Native Object");
    if (argc == 0) {
        cobj->getImageDes();
        args.rval().setUndefined();
        return true;
    }

    JS_ReportError(cx, "js_cocos2dx_ui_ImageView_getImageDes : wrong number of arguments: %d, was expecting %d", argc, 0);
    return false;
}

然后在js脚本中就可以调用getImageDes方法了,如下:

this._image.getImageDes();

控制台就可以看到打印。这种方式还是比较简单的绑定。

添加自定义的C++类

这里我创建了一个简单的C++类,CustomClass:
CustomClass.h

#ifndef CUSTOMCLASS
#define CUSTOMCLASS
#include "cocos2d.h"
namespace cocos2d {
  class CustomClass : public cocos2d::Ref
  {
    public:
      CustomClass();
      ~CustomClass();
      bool init();
      std::string helloMsg();
      CREATE_FUNC(CustomClass);
  };
} //namespace cocos2d
#endif // CUSTOMCLASS

CustomClass.cpp

#include "CustomClass.h"
USING_NS_CC;
CustomClass::CustomClass(){
}
CustomClass::~CustomClass(){
}
bool CustomClass::init(){
  return true;
}
std::string CustomClass::helloMsg(){
  return "this is CustomClass Msg!";
}

然后把这个类放在游戏工程目录/frameworks/cocos2d-x/cocos/my/下。


file.png

然后把my文件夹导入到cocos2d_libs工程中,


structure.png

并为cocos2d_libs工程的 User Header Searcher Paths,添加目录,指定到my目录下,$(SRCROOT)/../../../../cocos/my

add_header_search_path.png

同时,我也为游戏工程添加了这个目录(这个觉得是没有必要的)。
然后在tojs文件夹下添加cocos2dx_custom.ini文件。这个可以复制一份,然后修改相应的位置,主要是
headers = %(cocosdir)s/cocos/my/CustomClass.h 这个是头文件的路径,然后是
classes = CustomClass.* 这个是要包含进来的类
classes_need_extend = CustomClass 需要在js里面派生的类

然后修改 genbindings.py这个文件,151行的位置,添加刚才的cocos2dx_custom.init文件,如下:

cmd_args = {'cocos2dx.ini': ('cocos2d-x', 'jsb_cocos2dx_auto'),
            'cocos2dx_custom.ini': ('cocos2dx_custom', 'jsb_cocos2dx_custom_auto'),
            }

这是一份完整的cocos2dx_custom.ini:

[cocos2dx_custom]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = 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 namespace
target_namespace = ccext

android_headers = -I%(androidndkdir)s/platforms/android-19/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/%(clang_version)s/include 
clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__


cocos_headers = -I%(cocosdir)s -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/android -I%(cocosdir)s/extensions -I%(cocosdir)s/external -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/network -I%(cocosdir)s/cocos/ui/UIEditBox -I%(cocosdir)s/cocos/ui -I%(cocosdir)s/jsext -I%(cocosdir)s/jsext/system -I%(cocosdir)s/jsext/alipay  -I%(cocosdir)s/jsext/video -I%(cocosdir)s/jsext/webview -I%(cocosdir)s/jsext/umeng
#cocos_headers = -I%(cocosdir)s -I%(cocosdir)s/cocos -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/extra/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.*

#需要在js里面派生的类
#classes_need_extend = 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" lookup
classes_have_no_parents = 

# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip = Ref

# 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

剩下的就是运行 python genbindings.py,绑定成功之后可以在项目/frameworks/cocos2d-x/cocos/scripting/js-bindings/auto目录下看到对应的jsb_cocos2dx_custom_auto.hpp和jsb_cocos2dx_custom_auto.cpp,同时在这个目录下有一个api文件夹,jsb_cocos2dx_custom_api.js也相应的生成了。
然后把这两个文件导入到cocos2d_js_bindings工程的auto文件夹下。


jsb_binding_structure.png

然后在AppDelegate.cpp文件中注册,如下:

//jsbinding test
sc->addRegisterCallback(register_all_cocos2dx_custom);

然后我们就可以在js代码中使用我们的C++类了

var customClass = ccext.CustomClass.create();
var msg = customClass.helloMsg()
cc.log("customClass's msg is========>>>" + msg)

此时可以在控制台看到打印log。至此,js绑定C++已经完成 。

然后就是 Android 下面的处理。主要是把我们新添加的类,包含到Android.mk中。首先到项目/frameworks/cocos2d-x/cocos/scripting/js-bindings/proj.android/目录下,找到Android.mk ,然后在 LOCAL_C_INCLUDES下,包含我们的新添加的类的路径。在 LOCAL_SRC_FILES下包含,我们的cpp文件,如下: ../auto/jsb_cocos2dx_custom_auto.cpp \ 。同时,要保证我们的原始的C++类也包含到mk文件中,保证变异的时候能找到我们的原始C++文件。然后这样Android下面编译也没什么问题了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容