Skip to content

Commit 853f40b

Browse files
committed
CCTMXLayer renderCmd
1 parent 574be51 commit 853f40b

File tree

2 files changed

+242
-223
lines changed

2 files changed

+242
-223
lines changed

cocos2d/tilemap/CCTMXLayer.js

Lines changed: 10 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
7878
//used for retina display
7979
_contentScaleFactor: null,
8080

81-
_cacheCanvas:null,
8281
_cacheContext:null,
8382
_cacheTexture:null,
8483
_className:"TMXLayer",
@@ -97,27 +96,13 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
9796
this._layerSize = cc.size(0, 0);
9897
this._mapTileSize = cc.size(0, 0);
9998

100-
if(cc._renderType === cc._RENDER_TYPE_CANVAS){
101-
var locCanvas = cc._canvas;
102-
var tmpCanvas = cc.newElement('canvas');
103-
tmpCanvas.width = locCanvas.width;
104-
tmpCanvas.height = locCanvas.height;
105-
this._cacheCanvas = tmpCanvas;
106-
this._cacheContext = this._cacheCanvas.getContext('2d');
107-
var tempTexture = new cc.Texture2D();
108-
tempTexture.initWithElement(tmpCanvas);
109-
tempTexture.handleLoadedTexture();
110-
this._cacheTexture = tempTexture;
111-
this.width = locCanvas.width;
112-
this.height = locCanvas.height;
113-
// This class uses cache, so its default cachedParent should be himself
114-
this._cachedParent = this;
115-
}
11699
if(mapInfo !== undefined)
117100
this.initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo);
101+
102+
this._renderCmd._initContentSize();
118103
},
119104

120-
_createRendererCmd: function(){
105+
_createRenderCmd: function(){
121106
if(cc._renderType === cc._RENDER_TYPE_CANVAS)
122107
return new cc.TMXLayer.CanvasRenderCmd(this);
123108
else
@@ -131,60 +116,17 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
131116
* @param {Number} [height] The untransformed size's height of the TMXLayer.
132117
*/
133118
setContentSize:function (size, height) {
134-
var locContentSize = this._contentSize;
135119
cc.Node.prototype.setContentSize.call(this, size, height);
136-
137-
if(cc._renderType === cc._RENDER_TYPE_CANVAS){
138-
var locCanvas = this._cacheCanvas;
139-
var scaleFactor = cc.contentScaleFactor();
140-
locCanvas.width = 0 | (locContentSize.width * 1.5 * scaleFactor);
141-
locCanvas.height = 0 | (locContentSize.height * 1.5 * scaleFactor);
142-
143-
if(this.layerOrientation === cc.TMX_ORIENTATION_HEX)
144-
this._cacheContext.translate(0, locCanvas.height - (this._mapTileSize.height * 0.5)); //translate for hexagonal
145-
else
146-
this._cacheContext.translate(0, locCanvas.height);
147-
var locTexContentSize = this._cacheTexture._contentSize;
148-
locTexContentSize.width = locCanvas.width;
149-
locTexContentSize.height = locCanvas.height;
150-
151-
// Init sub caches if needed
152-
var totalPixel = locCanvas.width * locCanvas.height;
153-
if(totalPixel > this._maxCachePixel) {
154-
if(!this._subCacheCanvas) this._subCacheCanvas = [];
155-
if(!this._subCacheContext) this._subCacheContext = [];
156-
157-
this._subCacheCount = Math.ceil( totalPixel / this._maxCachePixel );
158-
var locSubCacheCanvas = this._subCacheCanvas, i;
159-
for(i = 0; i < this._subCacheCount; i++) {
160-
if(!locSubCacheCanvas[i]) {
161-
locSubCacheCanvas[i] = document.createElement('canvas');
162-
this._subCacheContext[i] = locSubCacheCanvas[i].getContext('2d');
163-
}
164-
var tmpCanvas = locSubCacheCanvas[i];
165-
tmpCanvas.width = this._subCacheWidth = Math.round( locCanvas.width / this._subCacheCount );
166-
tmpCanvas.height = locCanvas.height;
167-
}
168-
// Clear wasted cache to release memory
169-
for(i = this._subCacheCount; i < locSubCacheCanvas.length; i++) {
170-
tmpCanvas.width = 0;
171-
tmpCanvas.height = 0;
172-
}
173-
}
174-
// Otherwise use count as a flag to disable sub caches
175-
else this._subCacheCount = 0;
176-
}
120+
this._renderCmd.setContentSize(size, height);
177121
},
178122

179123
/**
180124
* Return texture of cc.SpriteBatchNode
181125
* @function
182126
* @return {cc.Texture2D}
183127
*/
184-
getTexture: null,
185-
186-
_getTextureForCanvas:function () {
187-
return this._cacheTexture;
128+
getTexture: function(){
129+
this._renderCmd.getTexture();
188130
},
189131

190132
/**
@@ -193,48 +135,8 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
193135
* @override
194136
* @param {CanvasRenderingContext2D} ctx
195137
*/
196-
visit: null,
197-
198-
_visitForCanvas: function (ctx) {
199-
//TODO it will implement dynamic compute child cutting automation.
200-
var i, len, locChildren = this._children;
201-
// quick return if not visible
202-
if (!this._visible || !locChildren || locChildren.length === 0)
203-
return;
204-
205-
if( this._parent)
206-
this._curLevel = this._parent._curLevel + 1;
207-
208-
this.transform();
209-
210-
if (this._cacheDirty) {
211-
var locCacheContext = this._cacheContext, locCanvas = this._cacheCanvas, locView = cc.view,
212-
instanceID = this.__instanceId, renderer = cc.renderer;
213-
//begin cache
214-
renderer._turnToCacheMode(instanceID);
215-
216-
this.sortAllChildren();
217-
for (i = 0, len = locChildren.length; i < len; i++) {
218-
if (locChildren[i]){
219-
locChildren[i].visit();
220-
locChildren[i]._cacheDirty = false;
221-
}
222-
}
223-
224-
//copy cached render cmd array to TMXLayer renderer
225-
this._renderCmd._copyRendererCmds(renderer._cacheToCanvasCmds[instanceID]);
226-
227-
locCacheContext.save();
228-
locCacheContext.clearRect(0, 0, locCanvas.width, -locCanvas.height);
229-
var t = cc.affineTransformInvert(this._transformWorld);
230-
locCacheContext.transform(t.a, t.c, t.b, t.d, t.tx * locView.getScaleX(), -t.ty * locView.getScaleY());
231-
232-
//draw to cache canvas
233-
renderer._renderingToCacheCanvas(locCacheContext, instanceID);
234-
locCacheContext.restore();
235-
this._cacheDirty = false;
236-
}
237-
cc.renderer.pushRenderCommand(this._renderCmd);
138+
visit: function(ctx){
139+
this._renderCmd.visit();
238140
},
239141

240142
//set the cache dirty flag for canvas
@@ -250,38 +152,8 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
250152
* @function
251153
* @param {CanvasRenderingContext2D} ctx
252154
*/
253-
draw:null,
254-
255-
_drawForCanvas:function (ctx) {
256-
var context = ctx || cc._renderContext;
257-
//context.globalAlpha = this._opacity / 255;
258-
var posX = 0 | ( -this._anchorPointInPoints.x), posY = 0 | ( -this._anchorPointInPoints.y);
259-
var eglViewer = cc.view;
260-
var locCacheCanvas = this._cacheCanvas;
261-
//direct draw image by canvas drawImage
262-
if (locCacheCanvas) {
263-
var locSubCacheCount = this._subCacheCount, locCanvasHeight = locCacheCanvas.height * eglViewer._scaleY;
264-
var halfTileSize = this._mapTileSize.height * 0.5 * eglViewer._scaleY;
265-
if(locSubCacheCount > 0) {
266-
var locSubCacheCanvasArr = this._subCacheCanvas;
267-
for(var i = 0; i < locSubCacheCount; i++){
268-
var selSubCanvas = locSubCacheCanvasArr[i];
269-
if (this.layerOrientation === cc.TMX_ORIENTATION_HEX)
270-
context.drawImage(locSubCacheCanvasArr[i], 0, 0, selSubCanvas.width, selSubCanvas.height,
271-
posX + i * this._subCacheWidth * eglViewer._scaleX, -(posY + locCanvasHeight) + halfTileSize, selSubCanvas.width * eglViewer._scaleX, locCanvasHeight);
272-
else
273-
context.drawImage(locSubCacheCanvasArr[i], 0, 0, selSubCanvas.width, selSubCanvas.height,
274-
posX + i * this._subCacheWidth * eglViewer._scaleX, -(posY + locCanvasHeight), selSubCanvas.width * eglViewer._scaleX, locCanvasHeight);
275-
}
276-
} else{
277-
if (this.layerOrientation === cc.TMX_ORIENTATION_HEX)
278-
context.drawImage(locCacheCanvas, 0, 0, locCacheCanvas.width, locCacheCanvas.height,
279-
posX, -(posY + locCanvasHeight) + halfTileSize, locCacheCanvas.width * eglViewer._scaleX, locCanvasHeight);
280-
else
281-
context.drawImage(locCacheCanvas, 0, 0, locCacheCanvas.width, locCacheCanvas.height,
282-
posX, -(posY + locCanvasHeight), locCacheCanvas.width * eglViewer._scaleX, locCanvasHeight);
283-
}
284-
}
155+
draw: function(ctx){
156+
this._renderCmd.draw(ctx);
285157
},
286158

287159
/**
@@ -1080,16 +952,6 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
1080952

1081953
var _p = cc.TMXLayer.prototype;
1082954

1083-
if(cc._renderType == cc._RENDER_TYPE_WEBGL){
1084-
_p.draw = cc.SpriteBatchNode.prototype.draw;
1085-
_p.visit = cc.SpriteBatchNode.prototype.visit;
1086-
_p.getTexture = cc.SpriteBatchNode.prototype.getTexture;
1087-
}else{
1088-
_p.draw = _p._drawForCanvas;
1089-
_p.visit = _p._visitForCanvas;
1090-
_p.getTexture = _p._getTextureForCanvas;
1091-
}
1092-
1093955
/** @expose */
1094956
cc.defineGetterSetter(_p, "texture", _p.getTexture, _p.setTexture);
1095957

0 commit comments

Comments
 (0)