Skip to content

Commit 926c9e4

Browse files
author
pandamicro
committed
Improve construction performance in general
1 parent 7f9e68a commit 926c9e4

17 files changed

+105
-84
lines changed

cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//-------------------------- ClippingNode's canvas render cmd --------------------------------
2626
(function () {
2727
cc.ClippingNode.CanvasRenderCmd = function (renderable) {
28-
cc.Node.CanvasRenderCmd.call(this, renderable);
28+
this._rootCtor(renderable);
2929
this._needDraw = false;
3030

3131
this._godhelpme = false;

cocos2d/clipping-nodes/CCClippingNodeWebGLRenderCmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// ------------------------------- ClippingNode's WebGL render cmd ------------------------------
2626
(function () {
2727
cc.ClippingNode.WebGLRenderCmd = function (renderable) {
28-
cc.Node.WebGLRenderCmd.call(this, renderable);
28+
this._rootCtor(renderable);
2929
this._needDraw = false;
3030

3131
this._beforeVisitCmd = new cc.CustomRenderCmd(this, this._onBeforeVisit);

cocos2d/core/base-nodes/CCAtlasNodeCanvasRenderCmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
(function () {
2929
cc.AtlasNode.CanvasRenderCmd = function (renderableObject) {
30-
cc.Node.CanvasRenderCmd.call(this, renderableObject);
30+
this._rootCtor(renderableObject);
3131
this._needDraw = false;
3232
this._colorUnmodified = cc.color.WHITE;
3333
this._textureToRender = null;

cocos2d/core/base-nodes/CCAtlasNodeWebGLRenderCmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
(function () {
2929
cc.AtlasNode.WebGLRenderCmd = function (renderableObject) {
30-
cc.Node.WebGLRenderCmd.call(this, renderableObject);
30+
this._rootCtor(renderableObject);
3131
this._needDraw = true;
3232
this._textureAtlas = null;
3333
this._colorUnmodified = cc.color.WHITE;

cocos2d/core/base-nodes/CCNode.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,10 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
163163

164164
//since 2.0 api
165165
_reorderChildDirty: false,
166-
_shaderProgram: null,
167166
arrivalOrder: 0,
168167

169168
_actionManager: null,
170169
_scheduler: null,
171-
_eventDispatcher: null,
172170

173171
_additionalTransformDirty: false,
174172
_additionalTransform: null,
@@ -190,32 +188,23 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
190188
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
191189
* @function
192190
*/
193-
ctor: function(){
194-
this._initNode();
195-
this._initRendererCmd();
196-
},
197-
198-
_initNode: function () {
191+
ctor: function () {
199192
var _t = this;
200193
_t._anchorPoint = cc.p(0, 0);
201194
_t._contentSize = cc.size(0, 0);
202195
_t._position = cc.p(0, 0);
203-
_t._normalizedPosition = cc.p(0,0);
196+
_t._normalizedPosition = cc.p(0, 0);
204197
_t._children = [];
205198

206199
var director = cc.director;
207-
_t._actionManager = director.getActionManager();
208-
_t._scheduler = director.getScheduler();
209200

210201
_t._additionalTransform = cc.affineTransformMakeIdentity();
211202
if (cc.ComponentContainer) {
212203
_t._componentContainer = new cc.ComponentContainer(_t);
213204
}
214-
215-
this._realOpacity = 255;
216205
this._realColor = cc.color(255, 255, 255, 255);
217-
this._cascadeColorEnabled = false;
218-
this._cascadeOpacityEnabled = false;
206+
207+
this._renderCmd = this._createRenderCmd();
219208
},
220209

221210
/**
@@ -224,7 +213,6 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
224213
* @returns {boolean} Whether the initialization was successful.
225214
*/
226215
init: function () {
227-
//this._initNode(); //this has been called in ctor.
228216
return true;
229217
},
230218

@@ -649,12 +637,12 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
649637
setPosition: function (newPosOrxValue, yValue) {
650638
var locPosition = this._position;
651639
if (yValue === undefined) {
652-
if(locPosition.x === newPosOrxValue.x && locPosition.y === newPosOrxValue.y)
640+
if (locPosition.x === newPosOrxValue.x && locPosition.y === newPosOrxValue.y)
653641
return;
654642
locPosition.x = newPosOrxValue.x;
655643
locPosition.y = newPosOrxValue.y;
656644
} else {
657-
if(locPosition.x === newPosOrxValue && locPosition.y === yValue)
645+
if (locPosition.x === newPosOrxValue && locPosition.y === yValue)
658646
return;
659647
locPosition.x = newPosOrxValue;
660648
locPosition.y = yValue;
@@ -1114,9 +1102,7 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
11141102
* @return {cc.ActionManager} A CCActionManager object.
11151103
*/
11161104
getActionManager: function () {
1117-
if (!this._actionManager)
1118-
this._actionManager = cc.director.getActionManager();
1119-
return this._actionManager;
1105+
return this._actionManager || cc.director.getActionManager();
11201106
},
11211107

11221108
/**
@@ -1140,9 +1126,7 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
11401126
* @return {cc.Scheduler} A CCScheduler object.
11411127
*/
11421128
getScheduler: function () {
1143-
if (!this._scheduler)
1144-
this._scheduler = cc.director.getScheduler();
1145-
return this._scheduler;
1129+
return this._scheduler || cc.director.getScheduler();
11461130
},
11471131

11481132
/**
@@ -2425,10 +2409,6 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
24252409
return false;
24262410
},
24272411

2428-
_initRendererCmd: function(){
2429-
this._renderCmd = cc.renderer.getRenderCmd(this);
2430-
},
2431-
24322412
_createRenderCmd: function () {
24332413
if (cc._renderType === cc.game.RENDER_TYPE_CANVAS)
24342414
return new cc.Node.CanvasRenderCmd(this);

cocos2d/core/base-nodes/CCNodeCanvasRenderCmd.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,26 @@ function transformChildTree(root) {
8383

8484
//-------------------------Base -------------------------
8585
cc.Node.RenderCmd = function (renderable) {
86-
this._dirtyFlag = 1; //need update the transform at first.
87-
this._savedDirtyFlag = true;
88-
8986
this._node = renderable;
90-
this._needDraw = false;
91-
this._anchorPointInPoints = new cc.Point(0, 0);
92-
93-
this._transform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
94-
this._worldTransform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
95-
this._inverse = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
96-
97-
this._displayedOpacity = 255;
87+
this._anchorPointInPoints = {x: 0, y: 0};
9888
this._displayedColor = cc.color(255, 255, 255, 255);
99-
this._cascadeColorEnabledDirty = false;
100-
this._cascadeOpacityEnabledDirty = false;
101-
102-
this._curLevel = -1;
10389
};
10490

10591
cc.Node.RenderCmd.prototype = {
10692
constructor: cc.Node.RenderCmd,
10793

94+
_needDraw: false,
95+
_dirtyFlag: 1,
96+
_curLevel: -1,
97+
98+
_displayedOpacity: 255,
99+
_cascadeColorEnabledDirty: false,
100+
_cascadeOpacityEnabledDirty: false,
101+
102+
_transform: null,
103+
_worldTransform: null,
104+
_inverse: null,
105+
108106
needDraw: function () {
109107
return this._needDraw;
110108
},
@@ -133,8 +131,12 @@ cc.Node.RenderCmd.prototype = {
133131
},
134132

135133
getParentToNodeTransform: function () {
136-
if (this._dirtyFlag & cc.Node._dirtyFlags.transformDirty)
137-
this._inverse = cc.affineTransformInvert(this.getNodeToParentTransform());
134+
if (!this._inverse) {
135+
this._inverse = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
136+
}
137+
if (this._dirtyFlag & cc.Node._dirtyFlags.transformDirty) {
138+
cc.affineTransformInvertOut(this.getNodeToParentTransform(), this._inverse);
139+
}
138140
return this._inverse;
139141
},
140142

@@ -161,6 +163,11 @@ cc.Node.RenderCmd.prototype = {
161163
},
162164

163165
transform: function (parentCmd, recursive) {
166+
if (!this._transform) {
167+
this._transform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
168+
this._worldTransform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
169+
}
170+
164171
var node = this._node,
165172
pt = parentCmd ? parentCmd._worldTransform : null,
166173
t = this._transform,
@@ -302,7 +309,7 @@ cc.Node.RenderCmd.prototype = {
302309
},
303310

304311
getNodeToParentTransform: function () {
305-
if (this._dirtyFlag & cc.Node._dirtyFlags.transformDirty) {
312+
if (!this._transform || this._dirtyFlag & cc.Node._dirtyFlags.transformDirty) {
306313
this.transform();
307314
}
308315
return this._transform;

cocos2d/core/layers/CCLayerCanvasRenderCmd.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
};
8181

8282
proto.transform = function (parentCmd, recursive) {
83+
if (!this._worldTransform) {
84+
this._worldTransform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
85+
}
8386
var wt = this._worldTransform;
8487
var a = wt.a, b = wt.b, c = wt.c, d = wt.d, tx = wt.tx, ty = wt.ty;
8588
this.originTransform(parentCmd, recursive);

cocos2d/core/textures/CCTexture2D.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ cc.PVRHaveAlphaPremultiplied_ = false;
9898

9999
cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {
100100

101-
if(cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
101+
if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
102102

103103
var proto = {
104104
_contentSize: null,

cocos2d/particle/CCParticleSystem.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,9 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
490490
* sourcePosition of the emitter setter
491491
* @param sourcePosition
492492
*/
493-
setSourcePosition:function (sourcePosition) {
494-
this._sourcePosition = sourcePosition;
493+
setSourcePosition: function (sourcePosition) {
494+
this._sourcePosition.x = sourcePosition.x;
495+
this._sourcePosition.y = sourcePosition.y;
495496
},
496497

497498
/**
@@ -507,7 +508,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
507508
* @param {cc.Point} posVar
508509
*/
509510
setPosVar: function (posVar) {
510-
this._posVar = posVar;
511+
this._posVar.x = posVar.x;
512+
this._posVar.y = posVar.y;
511513
},
512514

513515
/**
@@ -956,7 +958,10 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
956958
* @param {cc.Color} startColor
957959
*/
958960
setStartColor: function (startColor) {
959-
this._startColor = cc.color(startColor);
961+
this._startColor.r = startColor.r;
962+
this._startColor.g = startColor.g;
963+
this._startColor.b = startColor.b;
964+
this._startColor.a = startColor.a;
960965
},
961966

962967
/**
@@ -972,7 +977,10 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
972977
* @param {cc.Color} startColorVar
973978
*/
974979
setStartColorVar: function (startColorVar) {
975-
this._startColorVar = cc.color(startColorVar);
980+
this._startColorVar.r = startColorVar.r;
981+
this._startColorVar.g = startColorVar.g;
982+
this._startColorVar.b = startColorVar.b;
983+
this._startColorVar.a = startColorVar.a;
976984
},
977985

978986
/**
@@ -988,7 +996,10 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
988996
* @param {cc.Color} endColor
989997
*/
990998
setEndColor: function (endColor) {
991-
this._endColor = cc.color(endColor);
999+
this._endColor.r = endColor.r;
1000+
this._endColor.g = endColor.g;
1001+
this._endColor.b = endColor.b;
1002+
this._endColor.a = endColor.a;
9921003
},
9931004

9941005
/**
@@ -1004,7 +1015,10 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
10041015
* @param {cc.Color} endColorVar
10051016
*/
10061017
setEndColorVar: function (endColorVar) {
1007-
this._endColorVar = cc.color(endColorVar);
1018+
this._endColorVar.r = endColorVar.r;
1019+
this._endColorVar.g = endColorVar.g;
1020+
this._endColorVar.b = endColorVar.b;
1021+
this._endColorVar.a = endColorVar.a;
10081022
},
10091023

10101024
/**
@@ -1391,13 +1405,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
13911405
locModeA.tangentialAccelVar = (pszTmp) ? parseFloat(pszTmp) : 0;
13921406

13931407
// rotation is dir
1394-
var locRotationIsDir = locValueForKey("rotationIsDir", dictionary);
1395-
if (locRotationIsDir !== null) {
1396-
locRotationIsDir = locRotationIsDir.toString().toLowerCase();
1397-
locModeA.rotationIsDir = (locRotationIsDir === "true" || locRotationIsDir === "1");
1398-
}
1399-
else
1400-
locModeA.rotationIsDir = false;
1408+
var locRotationIsDir = locValueForKey("rotationIsDir", dictionary).toLowerCase();
1409+
locModeA.rotationIsDir = (locRotationIsDir != null && (locRotationIsDir === "true" || locRotationIsDir === "1"));
14011410
} else if (this.emitterMode === cc.ParticleSystem.MODE_RADIUS) {
14021411
// or Mode B: radius movement
14031412
var locModeB = this.modeB;

cocos2d/particle/CCParticleSystemCanvasRenderCmd.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
this._shapeType = cc.ParticleSystem.BALL_SHAPE;
3535

3636
this._pointRect = cc.rect(0, 0, 0, 0);
37-
this._tintCache = document.createElement("canvas");
37+
this._tintCache = null;
3838
};
3939
var proto = cc.ParticleSystem.CanvasRenderCmd.prototype = Object.create(cc.Node.CanvasRenderCmd.prototype);
4040
proto.constructor = cc.ParticleSystem.CanvasRenderCmd;
@@ -144,6 +144,9 @@
144144
};
145145

146146
proto._changeTextureColor = function (texture, color, rect) {
147+
if (!this._tintCache) {
148+
this._tintCache = document.createElement("canvas");
149+
}
147150
var tintCache = this._tintCache;
148151
var textureContentSize = texture.getContentSize();
149152
tintCache.width = textureContentSize.width;

0 commit comments

Comments
 (0)