Skip to content

Commit 0fc2680

Browse files
committed
Issue cocos2d#2416: Added CCLayerRenderCmd.js, CCParticleSystemRenderCmd.js, CCSpriteRenderCmd.js to engine.
1 parent 2ffb81d commit 0fc2680

14 files changed

+1610
-1445
lines changed

cocos2d/core/base-nodes/CCNode.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
185185
_componentContainer: null,
186186
_isTransitionFinished: false,
187187

188-
_rotationRadiansX: 0,
189-
_rotationRadiansY: 0,
190188
_className: "Node",
191189
_showNode: false,
192190
_name: "", ///<a string label, an user defined string to identify this node
@@ -531,8 +529,6 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
531529
*/
532530
setRotation: function (newRotation) {
533531
this._rotationX = this._rotationY = newRotation;
534-
this._rotationRadiansX = this._rotationX * 0.017453292519943295; //(Math.PI / 180);
535-
this._rotationRadiansY = this._rotationY * 0.017453292519943295; //(Math.PI / 180);
536532
this.setNodeDirty();
537533
},
538534

@@ -558,7 +554,6 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
558554
*/
559555
setRotationX: function (rotationX) {
560556
this._rotationX = rotationX;
561-
this._rotationRadiansX = this._rotationX * 0.017453292519943295; //(Math.PI / 180);
562557
this.setNodeDirty();
563558
},
564559

@@ -584,7 +579,6 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
584579
*/
585580
setRotationY: function (rotationY) {
586581
this._rotationY = rotationY;
587-
this._rotationRadiansY = this._rotationY * 0.017453292519943295; //(Math.PI / 180);
588582
this.setNodeDirty();
589583
},
590584

@@ -2298,11 +2292,12 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
22982292
}
22992293
if (_t._transformDirty) {
23002294
// Translate values
2301-
var x = _t._position.x;
2302-
var y = _t._position.y;
2295+
var x = _t._position.x, y = _t._position.y;
23032296
var apx = _t._anchorPointInPoints.x, napx = -apx;
23042297
var apy = _t._anchorPointInPoints.y, napy = -apy;
23052298
var scx = _t._scaleX, scy = _t._scaleY;
2299+
var rotationRadiansX = _t._rotationX * 0.017453292519943295; //0.017453292519943295 = (Math.PI / 180); for performance
2300+
var rotationRadiansY = _t._rotationY * 0.017453292519943295;
23062301

23072302
if (_t._ignoreAnchorPointForPosition) {
23082303
x += apx;
@@ -2314,10 +2309,10 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
23142309
// If we skew with the exact same value for both x and y then we're simply just rotating
23152310
var cx = 1, sx = 0, cy = 1, sy = 0;
23162311
if (_t._rotationX !== 0 || _t._rotationY !== 0) {
2317-
cx = Math.cos(-_t._rotationRadiansX);
2318-
sx = Math.sin(-_t._rotationRadiansX);
2319-
cy = Math.cos(-_t._rotationRadiansY);
2320-
sy = Math.sin(-_t._rotationRadiansY);
2312+
cx = Math.cos(-rotationRadiansX);
2313+
sx = Math.sin(-rotationRadiansX);
2314+
cy = Math.cos(-rotationRadiansY);
2315+
sy = Math.sin(-rotationRadiansY);
23212316
}
23222317
var needsSkewMatrix = ( _t._skewX || _t._skewY );
23232318

@@ -2589,9 +2584,17 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
25892584
},
25902585

25912586
_initRendererCmd: function(){
2587+
this._rendererCmd = cc.renderer.getRenderCmd(this);
25922588
},
25932589

2594-
_transformForRenderer: null
2590+
_transformForRenderer: null,
2591+
2592+
_createRenderCmd: function(){
2593+
if(cc._renderType === cc._RENDER_TYPE_CANVAS)
2594+
return new cc.Node.CanvasRenderCmd(this);
2595+
else
2596+
return new cc.Node.WebGLRenderCmd(this);
2597+
}
25952598
});
25962599

25972600
/**
@@ -2735,9 +2738,10 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
27352738

27362739
// rotation Cos and Sin
27372740
var Cos = 1, Sin = 0;
2741+
var rotationRadiansX = _t._rotationX * 0.017453292519943295; //0.017453292519943295 = (Math.PI / 180); for performance
27382742
if (_t._rotationX) {
2739-
Cos = Math.cos(_t._rotationRadiansX);
2740-
Sin = Math.sin(_t._rotationRadiansX);
2743+
Cos = Math.cos(rotationRadiansX);
2744+
Sin = Math.sin(rotationRadiansX);
27412745
}
27422746

27432747
// base abcd

cocos2d/core/base-nodes/CCNodeRenderCmd.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,26 @@
2323
****************************************************************************/
2424

2525
//The cc.Node's render command for Canvas
26-
cc.NodeCanvasRenderCmd = function(){
26+
cc.Node.CanvasRenderCmd = function(renderableObject){
27+
this._node = renderableObject;
2728
this._needDraw = false;
2829
this._anchorPointInPoints = new cc.Point(0,0);
2930
this._transform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
3031
this._transformWorld = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
31-
3232
};
3333

34-
cc.NodeCanvasRenderCmd.prototype = {
35-
constructor: cc.NodeCanvasRenderCmd
36-
34+
cc.Node.CanvasRenderCmd.prototype = {
35+
constructor: cc.Node.CanvasRenderCmd
3736
};
3837

39-
//register to renderer
40-
4138
//The cc.Node's render command for WebGL
42-
cc.NodeWebGLRenderCmd = function(){
39+
cc.Node.WebGLRenderCmd = function(renderableObject){
40+
this._node = renderableObject;
4341
this._needDraw = false;
42+
this._transform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
43+
this._transformWorld = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
4444
};
4545

46-
cc.NodeWebGLRenderCmd.prototype = {
47-
constructor: cc.NodeCanvasRenderCmd
48-
46+
cc.Node.WebGLRenderCmd.prototype = {
47+
constructor: cc.Node.WebGLRenderCmd
4948
};

cocos2d/core/layers/CCLayer.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ cc.LayerColor = cc.Layer.extend(/** @lends cc.LayerColor# */{
388388
this._updateColor();
389389
},
390390

391+
_createRenderCmd: function(){
392+
if (cc._renderType === cc._RENDER_TYPE_CANVAS)
393+
return new cc.LayerColor.CanvasRenderCmd(this);
394+
else
395+
return new cc.LayerColor.WebGLRenderCmd(this);
396+
},
397+
391398
draw: null
392399
});
393400

@@ -412,9 +419,6 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
412419
this._blendFunc = new cc.BlendFunc(cc.BLEND_SRC, cc.BLEND_DST);
413420
cc.LayerColor.prototype.init.call(this, color, width, height);
414421
};
415-
_p._initRendererCmd = function(){
416-
this._rendererCmd = new cc.RectRenderCmdCanvas(this);
417-
};
418422
_p._setWidth = function(width){
419423
cc.Node.prototype._setWidth.call(this, width);
420424
};
@@ -609,10 +613,6 @@ cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
609613
cc.LayerGradient.prototype.init.call(_t, start, end, v);
610614
},
611615

612-
_initRendererCmd: function(){
613-
this._rendererCmd = new cc.GradientRectRenderCmdCanvas(this);
614-
},
615-
616616
/**
617617
* Initialization of the layer, please do not call this function by yourself, you should pass the parameters to constructor to initialize a layer
618618
* @param {cc.Color} start starting color
@@ -772,6 +772,13 @@ cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
772772
this._updateColor();
773773
},
774774

775+
_createRenderCmd: function(){
776+
if (cc._renderType === cc._RENDER_TYPE_CANVAS)
777+
return new cc.LayerGradient.CanvasRenderCmd(this);
778+
else
779+
return new cc.LayerGradient.WebGLRenderCmd(this);
780+
},
781+
775782
_draw: null,
776783

777784
_updateColor: null
+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/****************************************************************************
2+
Copyright (c) 2013-2014 Chukong Technologies Inc.
3+
4+
http://www.cocos2d-x.org
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
****************************************************************************/
24+
25+
//Layer's canvas render command
26+
cc.Layer.CanvasRenderCmd = function(renderable){
27+
cc.Node.CanvasRenderCmd.call(this, renderable);
28+
};
29+
30+
cc.Layer.CanvasRenderCmd.prototype = Object.create(cc.Node.CanvasRenderCmd.prototype);
31+
32+
//Layer's WebGL render command
33+
cc.Layer.WebGLRenderCmd = function(renderable){
34+
cc.Node.WebGLRenderCmd.call(this, renderable);
35+
};
36+
37+
cc.Layer.WebGLRenderCmd.prototype = Object.create(cc.Node.WebGLRenderCmd.prototype);
38+
39+
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
40+
41+
//LayerColor's canvas render command
42+
cc.LayerColor.CanvasRenderCmd = function(renderable){
43+
cc.Layer.CanvasRenderCmd.call(this, renderable);
44+
this._needDraw = true;
45+
};
46+
cc.LayerColor.CanvasRenderCmd.prototype = Object.create(cc.Layer.CanvasRenderCmd.prototype);
47+
48+
cc.LayerColor.CanvasRenderCmd.prototype.rendering = function (ctx, scaleX, scaleY) {
49+
var context = ctx || cc._renderContext,
50+
node = this._node,
51+
t = node._transformWorld,
52+
curColor = node._displayedColor,
53+
opacity = node._displayedOpacity / 255,
54+
locWidth = node._contentSize.width,
55+
locHeight = node._contentSize.height;
56+
57+
if (opacity === 0)
58+
return;
59+
60+
var needTransform = (t.a !== 1 || t.b !== 0 || t.c !== 0 || t.d !== 1);
61+
var needRestore = (node._blendFuncStr !== "source-over") || needTransform;
62+
63+
if (needRestore) {
64+
context.save();
65+
context.globalCompositeOperation = node._blendFuncStr;
66+
}
67+
context.globalAlpha = opacity;
68+
context.fillStyle = "rgba(" + (0 | curColor.r) + "," + (0 | curColor.g) + ","
69+
+ (0 | curColor.b) + ", 1)";
70+
if (needTransform) {
71+
context.transform(t.a, t.c, t.b, t.d, t.tx * scaleX, -t.ty * scaleY);
72+
context.fillRect(0, 0, locWidth * scaleX, -locHeight * scaleY);
73+
} else {
74+
context.fillRect(t.tx * scaleX, -t.ty * scaleY, locWidth * scaleX, -locHeight * scaleY);
75+
}
76+
if (needRestore)
77+
context.restore();
78+
cc.g_NumberOfDraws++;
79+
};
80+
81+
//LayerColor's WebGL render command
82+
cc.LayerColor.WebGLRenderCmd = function(renderable){
83+
cc.Layer.WebGLRenderCmd.call(this, renderable);
84+
this._needDraw = true;
85+
};
86+
cc.LayerColor.WebGLRenderCmd.prototype = Object.create(cc.Layer.WebGLRenderCmd.prototype);
87+
88+
cc.LayerColor.WebGLRenderCmd.prototype.rendering = function (ctx) {
89+
var context = ctx || cc._renderContext;
90+
var node = this._node;
91+
92+
node._shaderProgram.use();
93+
node._shaderProgram._setUniformForMVPMatrixWithMat4(node._stackMatrix);
94+
cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR);
95+
96+
//
97+
// Attributes
98+
//
99+
context.bindBuffer(context.ARRAY_BUFFER, node._verticesFloat32Buffer);
100+
context.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, context.FLOAT, false, 0, 0);
101+
102+
context.bindBuffer(context.ARRAY_BUFFER, node._colorsUint8Buffer);
103+
context.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, 0, 0);
104+
105+
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
106+
context.drawArrays(context.TRIANGLE_STRIP, 0, 4);
107+
};
108+
109+
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
110+
//LayerGradient's Canvas render command
111+
cc.LayerGradient.CanvasRenderCmd = function(renderable){
112+
cc.LayerColor.CanvasRenderCmd.call(this, renderable);
113+
this._needDraw = true;
114+
this._startPoint = cc.p(0, 0);
115+
this._endPoint = cc.p(0, 0);
116+
this._startStopStr = null;
117+
this._endStopStr = null;
118+
};
119+
cc.LayerGradient.CanvasRenderCmd.prototype = Object.create(cc.LayerColor.CanvasRenderCmd.prototype);
120+
121+
cc.LayerGradient.CanvasRenderCmd.prototype.rendering = function (ctx, scaleX, scaleY) {
122+
var context = ctx || cc._renderContext,
123+
self = this,
124+
node = self._node,
125+
opacity = node._displayedOpacity / 255,
126+
t = node._transformWorld;
127+
128+
if (opacity === 0)
129+
return;
130+
131+
var needTransform = (t.a !== 1 || t.b !== 0 || t.c !== 0 || t.d !== 1);
132+
var needRestore = (node._blendFuncStr !== "source-over") || needTransform;
133+
if (needRestore) {
134+
context.save();
135+
context.globalCompositeOperation = node._blendFuncStr;
136+
}
137+
context.globalAlpha = opacity;
138+
var locWidth = node._contentSize.width, locHeight = node._contentSize.height;
139+
140+
var gradient = context.createLinearGradient(self._startPoint.x, self._startPoint.y, self._endPoint.x, self._endPoint.y);
141+
gradient.addColorStop(0, this._startStopStr);
142+
gradient.addColorStop(1, this._endStopStr);
143+
context.fillStyle = gradient;
144+
145+
if (needTransform) {
146+
context.transform(t.a, t.c, t.b, t.d, t.tx * scaleX, -t.ty * scaleY);
147+
context.fillRect(0, 0, locWidth * scaleX, -locHeight * scaleY);
148+
} else
149+
context.fillRect(t.tx * scaleX, -t.ty * scaleY, locWidth * scaleX, -locHeight * scaleY);
150+
151+
if (needRestore)
152+
context.restore();
153+
cc.g_NumberOfDraws++;
154+
};
155+
156+
//LayerColor's WebGL render command
157+
cc.LayerGradient.WebGLRenderCmd = function(renderable){
158+
cc.LayerColor.WebGLRenderCmd.call(this, renderable);
159+
this._needDraw = true;
160+
};
161+
cc.LayerGradient.WebGLRenderCmd.prototype = Object.create(cc.LayerColor.WebGLRenderCmd.prototype);
162+
163+
164+

cocos2d/core/layers/CCLayerWebGL.js

-6
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ cc._tmp.WebGLLayerColor = function () {
6464

6565
cc.LayerColor.prototype.init.call(_t, color, width, height);
6666
};
67-
_p._initRendererCmd = function(){
68-
this._rendererCmd = new cc.RectRenderCmdWebGL(this);
69-
};
7067
_p.setContentSize = function (size, height) {
7168
var locSquareVertices = this._squareVertices;
7269
if (height === undefined) {
@@ -143,9 +140,6 @@ cc._tmp.WebGLLayerColor = function () {
143140
cc._tmp.WebGLLayerGradient = function () {
144141
//cc.LayerGradient define start
145142
var _p = cc.LayerGradient.prototype;
146-
_p._initRendererCmd = function(){
147-
this._rendererCmd = new cc.RectRenderCmdWebGL(this);
148-
};
149143
_p.draw = cc.LayerColor.prototype.draw;
150144
_p._updateColor = function () {
151145
var _t = this;

0 commit comments

Comments
 (0)