您的位置:首页 > 移动开发 > Cocos引擎

Mac下cocos3.15如何将c++绑定至javascript(自动生成)详解

2017-11-23 16:26 495 查看
How to Use bindings-generator
==================

On Windows:
------------

* Make sure that you have installed `android-ndk-r10c` or later.
* 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.11.win32-py2.7.exe and install it.
* Download pyCheetah from https://raw.github.com/dumganhar/cocos2d-x/download/downloads/Cheetah.zip, unzip it to "C:\Python27\Lib\site-packages"
* Set environment variables (`NDK_ROOT`) and `PYTHON_BIN`
* Go to "cocos2d-x/tools/tojs" folder, and run "genbindings.py". The generated codes will be under "cocos\scripting\auto-generated\js-bindings".

On MAC:
----------

* The OSX 10.10 has a built-in python2.7 and if your os don't have python2.7 then use [Homebrew](http://brew.sh/) to install the python and use pip install the python dependencies.
<pre>
brew install python
</pre>

* Install python dependices by pip.
<pre>
sudo easy_install pip
sudo pip install PyYAML
sudo pip install Cheetah
</pre>

* Download NDK 64bit r10c or later from [Android Ndk](https://developer.android.com/ndk/downloads/index.html)
* Run
<pre>
export NDK_ROOT=/path/to/android-ndk-r10c
./genbindings.py
</pre>

On Ubuntu Linux 12.04 64bit
------------

* Install python
<pre>
sudo apt-get install python2.7
</pre>
* Install python dependices by pip.
<pre>
sudo apt-get install python-pip
sudo pip install PyYAML
sudo pip install Cheetah
</pre>
* Download NDK 64bit r10c or later from [Android Ndk](https://developer.android.com/ndk/downloads/index.html)
* Go to "cocos2d-x/tools/tojs", Run
<pre>
export NDK_ROOT=/path/to/android-ndk-r10c
./genbindings.py
</pre>


 

2.生成主要流程:

准备好你要绑定的C++的文件 -> 写生成文件的.ini配置文件和.sh 脚本程序 -> 运行 .sh 脚本文件 -> 生成绑定文件 -> 添加到项目中使用

 

关于设置 NDK   PYTHON 路径相关

 

在 .bash_profile 文件中加入对应目录 使用命令   pico .bash_profile(如果提示文件权限不是自己)

 

使用命令  sudo chmod g+rw .bash_profile 改回来

 

export NDK_ROOT="/Users/****/android-ndk-r9b"

 

export PYTHON_BIN="/System/Library/Frameworks/Python.framework/Versions/2.7/bin$

 

为了修改即刻生效 执行 命令 source .bash_profile

  jsb绑定大概流程就是以上,以下详细介绍从项目创建到 使用bindings-generator生成所需绑定文件、以及使用(以下绑定流程均在mac下进行)

 

 

1.新建一个cocos项目

  终端 进入 cocos.py文件所在目录:

   cd /Users/******/Downloads/cocos2d-x-3.15.1/tools/cocos2d-console/bin

  执行 命令 python cocos.py new J
1b60a
sGameDemo -p com.coco2dx.jsdemo -l js -d ~/Desktop     (支持3中语言 js  cpp   lua 我这里选择js)

2.添加自己所需的C++代码

 添加一个CCSpriteWithHue.h文件
1 #ifndef __CCSpriteWithHue__
2 #define __CCSpriteWithHue__
3
4 #include "cocos2d.h"
5 class SpriteWithHue: public cocos2d::Sprite
6 {
7 public:
8     static SpriteWithHue* create(const std::string& filename);
9     static SpriteWithHue* create(const std::string& filename, const cocos2d::Rect& rect);
10
11     static SpriteWithHue* createWithTexture(cocos2d::Texture2D *texture);
12     static SpriteWithHue* createWithTexture(cocos2d::Texture2D *texture, const cocos2d::Rect& rect, bool rotated=false);
13
14     static SpriteWithHue* createWithSpriteFrame(cocos2d::SpriteFrame *spriteFrame);
15     static SpriteWithHue* createWithSpriteFrameName(const std::string& spriteFrameName);
16
17     float getHue();
18     void setHue(float hue);
19
20 protected:
21     float _hue;
22     GLfloat _mat[3][3];
23
24     bool initWithTexture(cocos2d::Texture2D *texture);
25     bool initWithTexture(cocos2d::Texture2D *texture, const cocos2d::Rect& rect);
26     virtual bool initWithTexture(cocos2d::Texture2D *texture, const cocos2d::Rect &rect, bool rotated);
27     virtual bool initWithSpriteFrame(cocos2d::SpriteFrame *spriteFrame);
28
29     void setupDefaultSettings();
30     void initShader();
31     const GLchar* shaderBody();
32     virtual void updateColor();
33     void updateColorMatrix();
34     void updateAlpha();
35     GLfloat getAlpha();
36
37     void hueUniformCallback(cocos2d::GLProgram *p, cocos2d::Uniform *u);
38 };
39
40 #endif


 

  添加对应cpp文件

1 #include "CCSpriteWithHue.h"
2
3 const GLchar* colorRotationShaderBody();
4 void xRotateMat(float mat[3][3], float rs, float rc);
5 void yRotateMat(float mat[3][3], float rs, float rc);
6 void zRotateMat(float mat[3][3], float rs, float rc);
7 void matrixMult(float a[3][3], float b[3][3], float c[3][3]);
8 void hueMatrix(GLfloat mat[3][3], float angle);
9 void premultiplyAlpha(GLfloat mat[3][3], float alpha);
10
11 SpriteWithHue* SpriteWithHue::create(const std::string& filename)
12 {
13     SpriteWithHue *sprite = new (std::nothrow) SpriteWithHue();
14     if (sprite && sprite->initWithFile(filename))
15     {
16         sprite->autorelease();
17         return sprite;
18     }
19     CC_SAFE_DELETE(sprite);
20     return nullptr;
21 }
22
23 SpriteWithHue* SpriteWithHue::create(const std::string& filename, const cocos2d::Rect& rect)
24 {
25     SpriteWithHue *sprite = new (std::nothrow) SpriteWithHue();
26     if (sprite && sprite->initWithFile(filename, rect))
27     {
28         sprite->autorelease();
29         return sprite;
30     }
31     CC_SAFE_DELETE(sprite);
32     return nullptr;
33 }
34
35 SpriteWithHue* SpriteWithHue::createWithTexture(cocos2d::Texture2D *texture)
36 {
37     SpriteWithHue *sprite = new (std::nothrow) SpriteWithHue();
38     if (sprite && sprite->initWithTexture(texture))
39     {
40         sprite->autorelease();
41         return sprite;
42     }
43     CC_SAFE_DELETE(sprite);
44     return nullptr;
45 }
46
47 SpriteWithHue* SpriteWithHue::createWithTexture(cocos2d::Texture2D *texture, const cocos2d::Rect& rect, bool rotated)
48 {
49     SpriteWithHue *sprite = new (std::nothrow) SpriteWithHue();
50     if (sprite && sprite->initWithTexture(texture, rect, rotated))
51     {
52         sprite->autorelease();
53         return sprite;
54     }
55     CC_SAFE_DELETE(sprite);
56     return nullptr;
57 }
58
59 SpriteWithHue* SpriteWithHue::createWithSpriteFrame(cocos2d::SpriteFrame *spriteFrame)
60 {
61     SpriteWithHue *sprite = new (std::nothrow) SpriteWithHue();
62     if (sprite && spriteFrame && sprite->initWithSpriteFrame(spriteFrame))
63     {
64         sprite->autorelease();
65         return sprite;
66     }
67     CC_SAFE_DELETE(sprite);
68     return nullptr;
69 }
70
71 SpriteWithHue* SpriteWithHue::createWithSpriteFrameName(const std::string& spriteFrameName)
72 {
73     cocos2d::SpriteFrame *frame = cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
74
75 #if COCOS2D_DEBUG > 0
76     char msg[256] = {0};
77     sprintf(msg, "Invalid spriteFrameName: %s", spriteFrameName.c_str());
78     CCASSERT(frame != nullptr, msg);
79 #endif
80
81     return createWithSpriteFrame(frame);
82 }
83
84 bool SpriteWithHue::initWithTexture(cocos2d::Texture2D *texture, const cocos2d::Rect &rect, bool rotated)
85 {
86     bool ret = Sprite::initWithTexture(texture, rect, rotated);
87     if(ret)
88     {
89         setupDefaultSettings();
90         initShader();
91     }
92     return ret;
93 }
94
95 bool SpriteWithHue::initWithTexture(cocos2d::Texture2D *texture)
96 {
97     CCASSERT(texture != nullptr, "Invalid texture for sprite");
98
99     cocos2d::Rect rect = cocos2d::Rect::ZERO;
100     rect.size = texture->getContentSize();
101
102     return initWithTexture(texture, rect);
103 }
104
105 bool SpriteWithHue::initWithTexture(cocos2d::Texture2D *texture, const cocos2d::Rect& rect)
106 {
107     return initWithTexture(texture, rect, false);
108 }
109
110 bool SpriteWithHue::initWithSpriteFrame(cocos2d::SpriteFrame *spriteFrame)
111 {
112     CCASSERT(spriteFrame != nullptr, "");
113
114     bool bRet = initWithTexture(spriteFrame->getTexture(), spriteFrame->getRect());
115     setSpriteFrame(spriteFrame);
116
117     return bRet;
118 }
119
120 void SpriteWithHue::setupDefaultSettings()
121 {
122     _hue = 0.0f;
123 }
124
125 void SpriteWithHue::initShader()
126 {
127     auto glprogram = cocos2d::GLProgramCache::getInstance()->getGLProgram("hue_program");
128     if(!glprogram)
129     {
130        glprogram = cocos2d::GLProgram::createWithByteArrays(cocos2d::ccPositionTextureColor_noMVP_vert, shaderBody());
131        cocos2d::GLProgramCache::getInstance()->addGLProgram(glprogram, "hue_program");
132     }
133     auto glprogramstate = cocos2d::GLProgramState::create(glprogram);
134     setGLProgramState(glprogramstate);
135     updateColor();
136 }
137
138 const GLchar* SpriteWithHue::shaderBody()
139 {
140     return colorRotationShaderBody();
141 }
142
143 void SpriteWithHue::updateColor()
144 {
145     Sprite::updateColor();
146     updateColorMatrix();
147     updateAlpha();
148 }
149
150 void SpriteWithHue::hueUniformCallback(cocos2d::GLProgram *p, cocos2d::Uniform *u)
151 {
152    glUniformMatrix3fv(u->location, 1, GL_FALSE, (GLfloat*)&_mat);
153 }
154
155 void SpriteWithHue::updateColorMatrix()
156 {
157     hueMatrix(_mat, _hue);
158     premultiplyAlpha(_mat, getAlpha());
159
160     getGLProgramState()->setUniformCallback("u_hue", CC_CALLBACK_2(SpriteWithHue::hueUniformCallback, this));
161 }
162
163 void SpriteWithHue::updateAlpha()
164 {
165     getGLProgramState()->setUniformFloat("u_alpha", getAlpha());
166 }
167
168 GLfloat SpriteWithHue::getAlpha()
169 {
170     return _displayedOpacity/255.0f;
171 }
172
173 float SpriteWithHue::getHue()
174 {
175     return _hue;
176 }
177
178 void SpriteWithHue::setHue(float hue)
179 {
180     //CCLOG("setHue->%f",hue);
181     _hue = hue;
182     updateColorMatrix();
183 }
184
185 //shader
186
187 const GLchar * colorRotationShaderBody()
188 {
189     return
190     "                                                               \n\
191     #ifdef GL_ES                                                    \n\
192     precision mediump float;                                        \n\
193     #endif                                                          \n\
194     \n\
195     varying vec2 v_texCoord;                                        \n\
196     uniform mat3 u_hue;                                             \n\
197     uniform float u_alpha;                                          \n\
198     \n\
199     void main()                                                     \n\
200     {                                                               \n\
201     vec4 pixColor = texture2D(CC_Texture0, v_texCoord);             \n\
202     vec3 rgbColor = u_hue * pixColor.rgb;                           \n\
203     gl_FragColor = vec4(rgbColor, pixColor.a * u_alpha);            \n\
204     }                                                               \n\
205     ";
206 }
207
208 void xRotateMat(float mat[3][3], float rs, float rc)
209 {
210     mat[0][0] = 1.0;
211     mat[0][1] = 0.0;
212     mat[0][2] = 0.0;
213
214     mat[1][0] = 0.0;
215     mat[1][1] = rc;
216     mat[1][2] = rs;
217
218     mat[2][0] = 0.0;
219     mat[2][1] = -rs;
220     mat[2][2] = rc;
221 }
222
223 void yRotateMat(float mat[3][3], float rs, float rc)
224 {
225     mat[0][0] = rc;
226     mat[0][1] = 0.0;
227     mat[0][2] = -rs;
228
229     mat[1][0] = 0.0;
230     mat[1][1] = 1.0;
231     mat[1][2] = 0.0;
232
233     mat[2][0] = rs;
234     mat[2][1] = 0.0;
235     mat[2][2] = rc;
236 }
237
238
239 void zRotateMat(float mat[3][3], float rs, float rc)
240 {
241     mat[0][0] = rc;
242     mat[0][1] = rs;
243     mat[0][2] = 0.0;
244
245     mat[1][0] = -rs;
246     mat[1][1] = rc;
247     mat[1][2] = 0.0;
248
249     mat[2][0] = 0.0;
250     mat[2][1] = 0.0;
251     mat[2][2] = 1.0;
252 }
253
254 void matrixMult(float a[3][3], float b[3][3], float c[3][3])
255 {
256     int x, y;
257     float temp[3][3];
258
259     for(y=0; y<3; y++) {
260         for(x=0; x<3; x++) {
261             temp[y][x] = b[y][0] * a[0][x] + b[y][1] * a[1][x] + b[y][2] * a[2][x];
262         }
263     }
264     for(y=0; y<3; y++) {
265         for(x=0; x<3; x++) {
266             c[y][x] = temp[y][x];
267         }
268     }
269 }
270
271 void hueMatrix(GLfloat mat[3][3], float angle)
272 {
273 #define SQRT_2      sqrt(2.0)
274 #define SQRT_3      sqrt(3.0)
275
276     float mag, rot[3][3];
277     float xrs, xrc;
278     float yrs, yrc;
279     float zrs, zrc;
280
281     // Rotate the grey vector into positive Z
282     mag = SQRT_2;
283     xrs = 1.0/mag;
284     xrc = 1.0/mag;
285     xRotateMat(mat, xrs, xrc);
286     mag = SQRT_3;
287     yrs = -1.0/mag;
288     yrc = SQRT_2/mag;
289     yRotateMat(rot, yrs, yrc);
290     matrixMult(rot, mat, mat);
291
292     // Rotate the hue
293     zrs = sin(angle);
294     zrc = cos(angle);
295     zRotateMat(rot, zrs, zrc);
296     matrixMult(rot, mat, mat);
297
298     // Rotate the grey vector back into place
299     yRotateMat(rot, -yrs, yrc);
300     matrixMult(rot,  mat, mat);
301     xRotateMat(rot, -xrs, xrc);
302     matrixMult(rot,  mat, mat);
303 }
304
305 void premultiplyAlpha(GLfloat mat[3][3], float alpha)
306 {
307     for (int i = 0; i < 3; ++i) {
308         for (int j = 0; j < 3; ++j) {
309             mat[i][j] *= alpha;
310         }
311     }
312 }
[SpriteWithHue]
name = SpriteWithHue
prefix = autoSpriteWithHuebindings
classes = SpriteWithHue

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_
cocos_flags = -DANDROID
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/cocos -I%(cocosdir)s/cocos/platform/android -I%(cocosdir)s/external

simple_test_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/gameJsb

extra_arguments = %(android_headers)s %(clang_headers)s %(android_flags)s %(clang_flags)s %(cocos_headers)s %(cocos_flags)s %(simple_test_headers)s %(extra_flags)s

headers = %(cocosdir)s/cocos/gameJsb/CCSpriteWithHue.h

target_namespace =
remove_prefix =
skip = 
base_objects =
abstract_classes =
classes_have_type_info = no
rename =
rename_functions =
rename_classes =
# 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 =

# 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 = yes
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: