Cocos2d-x JSB Automatic Binding bindings

Source: Internet
Author: User

Javascript Binding (JSB) Automatic Binding tutorial. Cocos2d-x JSB Automatic Binding bindings-generator (hereinafter referred to as B-G) Use experience


If you want to understand the principle of depth, it is recommended to read the Cocos2D-x from C ++ to JS Advanced Path: http://blog.51cto.com/zt/560

What is B-G?
When using JSB, if the C ++ class or method you want to use is not bound to an existing JSB, you can use B-G. It can generate the corresponding C ++ binding file and corresponding JS interface. At that time, you only need to call the generated JS interface, it will be automatically converted to the original C ++ class, which is equivalent to directly calling the original C ++ method or class.

B-G generation process:
Prepare the necessary tools-> prepare the C ++ file you want to bind-> write the generated file. ini configuration file and. sh script program-> Run. sh script file-> Generate the binding file-> test the binding file until it can be used normally.

Necessary tools:
Cocos2d-x Engine: Http://cocos2d-x.org/projects/cocos2d-x/wiki/Download
Python 1, 2.7: Http://www.python.org/download/ (currently the bindings-generator in the engine only supports python2.7)
Py-ymal: Http://pyyaml.org/wiki/PyYAML (this is a third-party package for python, the download page has different system installation methods, can refer. Generator. py will call this package .)
Cheetah: Http://www.cheetahtemplate.org/(this is also a third-party python package, a template, generator. py will call this package, the installation method is the same as py-ymal .)
Libclang:Http://llvm.org/releases/3.1/clang+llvm-3.1-x86_64-apple-darwin11.tar.gz (generated when the need to use lib, note that the master branch is 2.2.2 this version of the engine with 3.1, develop branch is 3.0 version of the engine with 3.3)
Android NDK:Http://dl.google.com/android/ndk/android-ndk-r8c-darwin-x86.tar.bz2 (the r8c used when testing, now the 3.0beta version needs to use r9b)

. Ini file
Example: cocos2dx engine root directory/tools/bindings-generator/test. ini
Cocos2dx engine root directory/tools/tojs/cocos2dx_extension.ini
Cocos2dx engine root directory/tools/tojs/cocos2dx. ini


Usage of some parameters in. ini:
Name:It is simply a name.
Prefix:The last generated file will be prefixed with this name, such as prefix. cpp, prefix. hpp, prefix_api.js.
Classes:The name of the class to be converted must be all the classes in the imported header file. Here you can use regular expressions to add multiple classes. For details, refer to cocox2dx. ini.
Extra_arguments:System parameters required for some interfaces. For example, the clang package and the system parameters required for introducing the android ndk package, you can refer to the preceding three. ini statements.
Headers:The path of the header file to bind.
Target_namespace:Namespace. The class of the final JS file will start with this namespace. For example, if your class is sqlite and the namespace is cocos2dx, cocos2dx. sqlite is generated at the end.
Rename_functions:You can change the name of the method you want to bind to what you want. You can change multiple parameters and separate them with commas (,). For more information, see SqliteCpp: [sqlite3_execCpp = sqlite3_exec]. This is to rename the sqlite3 _ execCpp method in SqliteCpp to sqlite3_exec.
Rename_classes: Same as above. Rename the class.
Skip: Skip the methods and classes you do not need to bind, so they are not generated.

. Sh file: it is mainly used to call a program to read the. ini configuration file and write some parameters.
Example: cocos2dx engine root directory/tools/tojs/genbindings. sh
Cocos2dx engine root directory/tools/bindings-generator/test. sh

In the following sentence, you can call generator. py to call your configuration file, and generate the required binding file on the enemy you set. Set-x LD_LIBRARY_PATH =$ {CLANG_ROOT}/lib $ PYTHON_BIN $ {generator}/generator. py $ {TO_JS_ROOT}/sqlite. ini-s sqlite-o $ {TO_JS_ROOT}/sqliteBinding
After writing. ini and. sh, execute sudo your. sh path in cmd to find the file you generated in the configuration area.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.