Skip to content

Commit 8ed792c

Browse files
committed
Issue cocos2d#2416: refactor cc.Sprite's renderer
1 parent 131483a commit 8ed792c

File tree

4 files changed

+214
-317
lines changed

4 files changed

+214
-317
lines changed

cocos2d/core/sprites/CCSprite.js

+60-174
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
120120
_textureLoaded:false,
121121
_className:"Sprite",
122122

123-
//Only for texture update judgment
124-
_oldDisplayColor: cc.color.WHITE, //TODO move to Canvas render
125-
126123
_originalTexture: null, //TODO move to Canvas render cmd
127-
_drawSize_Canvas: null,
128124

129125
ctor: function (fileName, rect, rotated) {
130126
var self = this;
@@ -522,7 +518,12 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
522518
* @function
523519
* @param {Boolean} modify
524520
*/
525-
setOpacityModifyRGB:null,
521+
setOpacityModifyRGB: function (modify) {
522+
if (this._opacityModifyRGB !== modify) {
523+
this._opacityModifyRGB = modify;
524+
this._renderCmd._updateColor();
525+
}
526+
},
526527

527528
/**
528529
* Returns whether opacity modify color or not.
@@ -532,12 +533,6 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
532533
return this._opacityModifyRGB;
533534
},
534535

535-
/**
536-
* Update the display opacity.
537-
* @function
538-
*/
539-
updateDisplayedOpacity: null,
540-
541536
// Animation
542537

543538
/**
@@ -653,7 +648,36 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
653648
* @function
654649
* @return {Boolean}
655650
*/
656-
init:null,
651+
init: function () {
652+
var _t = this;
653+
if (arguments.length > 0)
654+
return _t.initWithFile(arguments[0], arguments[1]);
655+
656+
cc.Node.prototype.init.call(_t);
657+
_t.dirty = _t._recursiveDirty = false;
658+
659+
_t._blendFunc.src = cc.BLEND_SRC;
660+
_t._blendFunc.dst = cc.BLEND_DST;
661+
662+
_t.texture = null;
663+
_t._textureLoaded = true;
664+
_t._flippedX = _t._flippedY = false;
665+
666+
// default transform anchor: center
667+
_t.anchorX = 0.5;
668+
_t.anchorY = 0.5;
669+
670+
// zwoptex default values
671+
_t._offsetPosition.x = 0;
672+
_t._offsetPosition.y = 0;
673+
_t._hasChildren = false;
674+
675+
this._renderCmd.init();
676+
// updated in "useSelfRender"
677+
// Atlas: TexCoords
678+
_t.setTextureRect(cc.rect(0, 0, 0, 0), false, cc.size(0, 0));
679+
return true;
680+
},
657681

658682
/**
659683
* <p>
@@ -696,8 +720,6 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
696720
*/
697721
initWithTexture: null,
698722

699-
_textureLoadedCallback: null,
700-
701723
/**
702724
* Updates the texture rect of the CCSprite in points.
703725
* @function
@@ -724,20 +746,6 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
724746
*/
725747
addChild: null,
726748

727-
/**
728-
* Sets opacity of the sprite
729-
* @function
730-
* @param {Number} opacity
731-
*/
732-
setOpacity:null,
733-
734-
/**
735-
* Sets color of the sprite
736-
* @function
737-
* @param {cc.Color} color3
738-
*/
739-
setColor: null,
740-
741749
/**
742750
* Updates the display color
743751
* @function
@@ -768,7 +776,9 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
768776
* @param {cc.SpriteFrame} frame
769777
* @return {Boolean}
770778
*/
771-
isFrameDisplayed: null,
779+
isFrameDisplayed: function(frame){
780+
return this._renderCmd.isFrameDisplayed(frame);
781+
},
772782

773783
/**
774784
* Returns the current displayed frame.
@@ -800,25 +810,27 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
800810
* @function
801811
* @param {cc.Texture2D|String} texture
802812
*/
803-
setTexture: null,
804-
805-
// Texture protocol
806-
_updateBlendFunc:function () {
807-
if(this._batchNode){
808-
cc.log(cc._LogInfos.Sprite__updateBlendFunc);
813+
setTexture: function (texture) {
814+
var _t = this;
815+
if(texture && (cc.isString(texture))){
816+
texture = cc.textureCache.addImage(texture);
817+
_t.setTexture(texture);
818+
//TODO
819+
var size = texture.getContentSize();
820+
_t.setTextureRect(cc.rect(0,0, size.width, size.height));
821+
//If image isn't loaded. Listen for the load event.
822+
if(!texture._isLoaded){
823+
texture.addEventListener("load", function(){
824+
var size = texture.getContentSize();
825+
_t.setTextureRect(cc.rect(0,0, size.width, size.height));
826+
}, this);
827+
}
809828
return;
810829
}
830+
// CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteSheet
831+
cc.assert(!texture || texture instanceof cc.Texture2D, cc._LogInfos.Sprite_setTexture_2);
811832

812-
// it's possible to have an untextured sprite
813-
if (!this._texture || !this._texture.hasPremultipliedAlpha()) {
814-
this._blendFunc.src = cc.SRC_ALPHA;
815-
this._blendFunc.dst = cc.ONE_MINUS_SRC_ALPHA;
816-
this.opacityModifyRGB = false;
817-
} else {
818-
this._blendFunc.src = cc.BLEND_SRC;
819-
this._blendFunc.dst = cc.BLEND_DST;
820-
this.opacityModifyRGB = true;
821-
}
833+
this._renderCmd._setTexture(texture);
822834
},
823835

824836
_createRenderCmd: function(){
@@ -873,48 +885,6 @@ cc.Sprite.INDEX_NOT_INITIALIZED = -1;
873885
if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
874886
var _p = cc.Sprite.prototype;
875887

876-
_p.setOpacityModifyRGB = function (modify) {
877-
if (this._opacityModifyRGB !== modify) {
878-
this._opacityModifyRGB = modify;
879-
this.setNodeDirty(true);
880-
}
881-
};
882-
883-
_p.updateDisplayedOpacity = function (parentOpacity) {
884-
cc.Node.prototype.updateDisplayedOpacity.call(this, parentOpacity);
885-
this._setNodeDirtyForCache();
886-
};
887-
888-
_p.init = function () {
889-
var _t = this;
890-
if (arguments.length > 0)
891-
return _t.initWithFile(arguments[0], arguments[1]);
892-
893-
cc.Node.prototype.init.call(_t);
894-
_t.dirty = _t._recursiveDirty = false;
895-
896-
_t._blendFunc.src = cc.BLEND_SRC;
897-
_t._blendFunc.dst = cc.BLEND_DST;
898-
899-
_t.texture = null;
900-
_t._textureLoaded = true;
901-
_t._flippedX = _t._flippedY = false;
902-
903-
// default transform anchor: center
904-
_t.anchorX = 0.5;
905-
_t.anchorY = 0.5;
906-
907-
// zwoptex default values
908-
_t._offsetPosition.x = 0;
909-
_t._offsetPosition.y = 0;
910-
_t._hasChildren = false;
911-
912-
// updated in "useSelfRender"
913-
// Atlas: TexCoords
914-
_t.setTextureRect(cc.rect(0, 0, 0, 0), false, cc.size(0, 0));
915-
return true;
916-
};
917-
918888
_p.initWithTexture = function (texture, rect, rotated) {
919889
var _t = this;
920890
cc.assert(arguments.length != 0, cc._LogInfos.CCSpriteBatchNode_initWithTexture);
@@ -945,8 +915,7 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
945915
_t._flippedX = _t._flippedY = false;
946916

947917
// default transform anchor: center
948-
_t.anchorX = 0.5;
949-
_t.anchorY = 0.5;
918+
_t.setAnchorPoint(0.5, 0.5);
950919

951920
// zwoptex default values
952921
_t._offsetPosition.x = 0;
@@ -966,14 +935,13 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
966935
}
967936
if(_t.texture)
968937
_t.texture.removeEventListener("load", _t);
969-
texture.addEventListener("load", _t._textureLoadedCallback, _t);
938+
texture.addEventListener("load", _t._renderCmd._textureLoadedCallback, _t);
970939
_t.texture = texture;
971940
return true;
972941
}
973942

974-
if (!rect) {
943+
if (!rect)
975944
rect = cc.rect(0, 0, texture.width, texture.height);
976-
}
977945

978946
if(texture && texture.url) {
979947
var _x = rect.x + rect.width, _y = rect.y + rect.height;
@@ -994,35 +962,6 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
994962
return true;
995963
};
996964

997-
_p._textureLoadedCallback = function (sender) {
998-
var _t = this;
999-
if(_t._textureLoaded)
1000-
return;
1001-
1002-
_t._textureLoaded = true;
1003-
var locRect = _t._rect;
1004-
if (!locRect) {
1005-
locRect = cc.rect(0, 0, sender.width, sender.height);
1006-
} else if (cc._rectEqualToZero(locRect)) {
1007-
locRect.width = sender.width;
1008-
locRect.height = sender.height;
1009-
}
1010-
_t._originalTexture = sender;
1011-
1012-
_t.texture = sender;
1013-
_t.setTextureRect(locRect, _t._rectRotated);
1014-
1015-
//set the texture's color after the it loaded
1016-
var locColor = this._displayedColor;
1017-
if(locColor.r != 255 || locColor.g != 255 || locColor.b != 255)
1018-
_t._changeTextureColor();
1019-
1020-
// by default use "Self Render".
1021-
// if the sprite is added to a batchnode, then it will automatically switch to "batchnode Render"
1022-
_t.batchNode = _t._batchNode;
1023-
_t.dispatchEvent("load");
1024-
};
1025-
1026965
_p.setTextureRect = function (rect, rotated, untrimmedSize) {
1027966
var _t = this;
1028967
_t._rectRotated = rotated || false;
@@ -1055,7 +994,6 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
1055994

1056995
_p.updateTransform = function () {
1057996
var _t = this;
1058-
//cc.assert(_t._batchNode, "updateTransform is only valid when cc.Sprite is being rendered using an cc.SpriteBatchNode");
1059997

1060998
// re-calculate matrix only if it is dirty
1061999
if (_t.dirty) {
@@ -1095,23 +1033,6 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
10951033
this._hasChildren = true;
10961034
};
10971035

1098-
_p.setOpacity = function (opacity) {
1099-
cc.Node.prototype.setOpacity.call(this, opacity);
1100-
this._setNodeDirtyForCache();
1101-
};
1102-
1103-
_p.updateDisplayedColor = function (parentColor) {
1104-
var _t = this;
1105-
cc.Node.prototype.updateDisplayedColor.call(_t, parentColor);
1106-
var oColor = _t._oldDisplayColor;
1107-
var nColor = _t._displayedColor;
1108-
if (oColor.r === nColor.r && oColor.g === nColor.g && oColor.b === nColor.b)
1109-
return;
1110-
1111-
_t._changeTextureColor();
1112-
_t._setNodeDirtyForCache();
1113-
};
1114-
11151036
_p.setSpriteFrame = function (newFrame) {
11161037
var _t = this;
11171038
if(cc.isString(newFrame)){
@@ -1160,12 +1081,6 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
11601081
}
11611082
};
11621083

1163-
_p.isFrameDisplayed = function (frame) { //TODO there maybe has a bug
1164-
if (frame.getTexture() != this._texture)
1165-
return false;
1166-
return cc.rectEqualToRect(frame.getRect(), this._rect);
1167-
};
1168-
11691084
_p.setBatchNode = function (spriteBatchNode) {
11701085
var _t = this;
11711086
_t._batchNode = spriteBatchNode; // weak reference
@@ -1183,35 +1098,6 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
11831098
}
11841099
};
11851100

1186-
_p.setTexture = function (texture) {
1187-
var _t = this;
1188-
if(texture && (cc.isString(texture))){
1189-
texture = cc.textureCache.addImage(texture);
1190-
_t.setTexture(texture);
1191-
//TODO
1192-
var size = texture.getContentSize();
1193-
_t.setTextureRect(cc.rect(0,0, size.width, size.height));
1194-
//If image isn't loaded. Listen for the load event.
1195-
if(!texture._isLoaded){
1196-
texture.addEventListener("load", function(){
1197-
var size = texture.getContentSize();
1198-
_t.setTextureRect(cc.rect(0,0, size.width, size.height));
1199-
}, this);
1200-
}
1201-
return;
1202-
}
1203-
1204-
// CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteSheet
1205-
cc.assert(!texture || texture instanceof cc.Texture2D, cc._LogInfos.CCSpriteBatchNode_setTexture);
1206-
1207-
if (_t._texture != texture) {
1208-
if (texture && texture.getHtmlElementObj() instanceof HTMLImageElement) {
1209-
_t._originalTexture = texture;
1210-
}
1211-
_t._texture = texture;
1212-
}
1213-
};
1214-
12151101
_p = null;
12161102
} else {
12171103
cc.assert(cc.isFunction(cc._tmp.WebGLSprite), cc._LogInfos.MissingFile, "SpritesWebGL.js");

cocos2d/core/sprites/CCSpriteBatchNode.js

-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ cc.SpriteBatchNode = cc.Node.extend(/** @lends cc.SpriteBatchNode# */{
526526
},
527527

528528
_insertQuadFromSpriteForWebGL: function (sprite, index) {
529-
530529
cc.assert(sprite, cc._LogInfos.Sprite_insertQuadFromSprite_2);
531530

532531
if (!(sprite instanceof cc.Sprite)) {

0 commit comments

Comments
 (0)