cocos-js 自动绑定C++(2)代码部分

来源:互联网 发布:找一份网络兼职 编辑:程序博客网 时间:2024/06/06 19:17
代码部分一共有3中文件
     1 .c++文件,就是自己需要的c++文件。 .h 和 .cpp
  1. 自动生成的c++文件 。可供js 调用的 .hpp 和 .cpp文件 (连接js 和cpp)
  2. 自动生成的 .js 文件 ,供自己代码中的js 直接调用


  • 写c++文件,将需要调用的C++文件(.h 和 .cpp)copy到 CocosCreator\resources\cocos2d-x\tools\tojs 目录下(其实位置可以任意,只需要在后面的配置文件中正确指向就可以)
            
            测试代码: 就是在控制台输出一句话

                .h 文件
                    //   jsbTest.h
                    #ifndef __JSB_TEST_H__
                    #define __JSB_TEST_H__
                    #include "cocos2d.h"
                    class jsbTest
                    {
                        public:
                           static void testlog();
                    };

                    #endif //__JSB_TEST_H


                .cpp 文件
                     //   jsbTest.cpp
                    #include "jsbTest.h"
                    USING_NS_CC;
                    void jsbTest::testlog()
                    {
                        CCLOG("jsbinding succeed!");
                    } 
                    
  • 写配置文件.ini   (其实就是复制修改,找一个原有的.ini文件 修改就好   )   配置文件路径: CocosCreator\resources\cocos2d-x\tools\tojs
            名字可以任意取 ,在需要执行的python文件中指定就行   这里设置 jsbTest (和c++文件名一致 方便管理)
                    [jsbTest]
                    # the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = jsbTest 
# 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 =
# the native namespace in which this module locates, this parameter is used for avoid conflict of the same class name in different modules, as "cocos2d::Label" <-> "cocos2d::ui::Label".
cpp_namespace =
android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/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 -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.9/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/%(clang_include)s
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 -I%(cocosdir)s/external/sources
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/tools/tojs/jsbTest.h               #这里就是需要调用的c++文件的头文件(就是在这里指定路径)
replace_headers =
cpp_headers =
# 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 = jsbTest
classes_need_extend =
# 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 =


1.修改prefix 名  为c++文件名
2.这顶headers 的文件路径(就是需要调用的c++文件的路径)
3.classes  设置 为c++文件名
其他部分置空   (可以自己设置特殊 的要求)

网上找到解释。。。。。。

.ini中部分参数的用法:
name: 单纯只是名称。
prefix: 最后生成的文件都会以这个命名前缀,如 prefix.cpp, prefix.hpp, prefix_api.js 
classes: 你的所需转换的类的名称,必须是所导入的头文件中所有的类,这里可以使用正则表达式来加入多个类,参考cocox2dx.ini。
extra_arguments: 一些接口所需的系统参数。如clang包,android ndk包的引入所需的系统参数,写法可以参照以上三个.ini。 
headers: 你所需要绑定的头文件路径。 
target_namespace:命名空间。最后生成的JS文件的类,会以这个命名空间开头。例如你的类为sqlite,命名空间为cocos2dx,那么最后生成的就是cocos2dx.sqlite。 
rename_functions:可以将你要绑定的方法的名称更改成你所要的。可以更改多个,用逗号隔开,写法参照 SqliteCpp::[sqlite3_execCpp=sqlite3_exec],这个就是将SqliteCpp中的sqlite3_ execCpp方法重命名为sqlite3_exec方法。
rename_classes :同上,重命名类。
skip :跳过你所不需要绑定的方法和类,于是就不生成。


  • 在genbindings.py 中指定要要执行的配置文件
                打开python 文件    creator 安装目录先: CocosCreator\resources\cocos2d-x\tools\tojs下面的genbindings.py文件
                    将新建的配置文件添加到生成目录里面,可以注释掉原有的配置文件,这样原来的自动绑定文件就不会重新生成,只操作指定的配置文件
                
                  
            
  • 最后在creator 安装路径 CocosCreator\resources\cocos2d-x\tools\tojs 下,命令行执行 python genbindings.py 

            不出意外就回生成两种文件(这次不会出意外了,除非ini 配置文件写错。。。。。)
                    在 creator安装路径CocosCreator\resources\cocos2d-x\cocos\scripting\js-bindings\auto 生成jsb_jsbTest_auto.h 和jsb_jsbTest_auto.m 文件
                    在 creator安装路径CocosCreator\resources\cocos2d-x\cocos\scripting\js-bindings\auto\api 生成jsb_jsbTest_auto_api.js文件

  • 在creator 里构建Android项目(必须是调试模式
            配置环境 添加android sdk ndk ant等内容 (百度)



           到这里生成的自动绑定的文件已经成功,下面就是如何将文件正确导入到项目中去。。。。。。。。。。。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  • 将生成的文件导入到项目中
            (1)复制文件      将c++文件复制到class文件夹下
                     creator 安装路径:    CocosCreator\resources\cocos2d-x\tools\tojs 下的 c++文件 (.h 和 .m两个文件)                                               ======》 项目build路径:build\jsb-default\frameworks\runtime-src\Classes
                     creator 安装路径:    CocosCreator\resources\cocos2d-x\cocos\scripting\js-bindings\auto(两个自动生成的.hpp 和 .cpp文件)           =======》 项目build 路径 build\jsb-default\frameworks\cocos2d-x\cocos\scripting\js-bindings\auto
                     creator 安装路径:    CocosCreator\resources\cocos2d-x\cocos\scripting\js-bindings\auto\api (自动生成的.js文件)                          =======》 项目build 路径 build\jsb-default\frameworks\cocos2d-x\cocos\scripting\js-bindings\auto\api


             (2)用vs 2015操作
                   将生成的文件导入到项目中
                         ①导入.cpp 和 .h文件(需要调用的c++文件)
                                点击项目的Classes 文件夹 --->添加 --->现有项    选择 .cpp 和 .h文件
                         ②导入生成的 .cpp 和 .hpp文件  (auto 路径下自动生成的) 
                                  点击libjscocos2d/auto文件夹   添加 ---> 现有项  选择 .cpp 和 .hpp文件

            (3)解决找不到头文件的问题
                        双击jsb_jsbTest_auto.cpp文件 会出现 找不到jsbTest.h的问题
                        添加查找路径: 右击libjscocos2d -->选择属性 -->选择c/c++  常规  ---> 附加包含目录 /添加.\runtime-src\Classes目录


             (4)注册
                        在appdelegate中 找到 js_module_register(); ---> 右击 转到定义 跳转到cpp文件中
                        包含#include "jsb_jsbTest_auto.hpp" 头文件
            在js_module_register()函数里注册回调函数:  sc->addRegisterCallback(register_all_jsbTest);

       用vs本地运行 不出错就没问题了;

  • 调用c++
      进入目录:\build\jsb-default\src

                打开目录下的:project.dev.js(creator项目里的所有js脚本在构建以后通通会合并到这个文件里),找到想要调用c++的地方,测试就直接在onLoad里调用
            jsbTest.testlog();

            

            保存 ---> vs 本地运行,应该就会有结果了 。。。。。


 --------------------------------------------------------------结束-----------------------------------------------
    只能在creator构建以后才能绑定,每次修改完c++文件后应该都要重新生成一遍 (这个测过), 以后 只要不修改++ 文件  ,creator重新构建场景对此应该是没有影响(还没有测)
参考文档:http://forum.cocos.com/t/js-c-jsb-jsbinding/37909