123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927 |
- /****************************************************************************
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2011-2012 cocos2d-x.org
- Copyright (c) 2013-2014 Chukong Technologies Inc.
- Copyright (c) 2009 Jason Booth
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- /**
- * enum for jpg
- * @constant
- * @type Number
- */
- cc.IMAGE_FORMAT_JPEG = 0;
- /**
- * enum for png
- * @constant
- * @type Number
- */
- cc.IMAGE_FORMAT_PNG = 1;
- /**
- * enum for raw
- * @constant
- * @type Number
- */
- cc.IMAGE_FORMAT_RAWDATA = 9;
- /**
- * @param {Number} x
- * @return {Number}
- * Constructor
- */
- cc.NextPOT = function (x) {
- x = x - 1;
- x = x | (x >> 1);
- x = x | (x >> 2);
- x = x | (x >> 4);
- x = x | (x >> 8);
- x = x | (x >> 16);
- return x + 1;
- };
- /**
- * cc.RenderTexture is a generic rendering target. To render things into it,<br/>
- * simply construct a render target, call begin on it, call visit on any cocos<br/>
- * scenes or objects to render them, and call end. For convenience, render texture<br/>
- * adds a sprite as it's display child with the results, so you can simply add<br/>
- * the render texture to your scene and treat it like any other CocosNode.<br/>
- * There are also functions for saving the render texture to disk in PNG or JPG format.
- * @class
- * @extends cc.Node
- *
- * @property {cc.Sprite} sprite - The sprite.
- * @property {cc.Sprite} clearFlags - Code for "auto" update.
- * @property {Number} clearDepthVal - Clear depth value.
- * @property {Boolean} autoDraw - Indicate auto draw mode activate or not.
- * @property {Number} clearStencilVal - Clear stencil value.
- * @property {cc.Color} clearColorVal - Clear color value, valid only when "autoDraw" is true.
- */
- cc.RenderTexture = cc.Node.extend(/** @lends cc.RenderTexture# */{
- sprite:null,
- //
- // <p>Code for "auto" update<br/>
- // Valid flags: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.<br/>
- // They can be OR'ed. Valid when "autoDraw is YES.</p>
- // @public
- //
- clearFlags:0,
- clearDepthVal:0,
- autoDraw:false,
- //
- // the off-screen canvas for rendering and storing the texture
- // @type HTMLCanvasElement
- //
- _cacheCanvas:null,
- /**
- * stores a reference to the canvas context object
- * @type CanvasRenderingContext2D
- */
- _cacheContext:null,
- _fBO:0,
- _depthRenderBuffer:0,
- _oldFBO:0,
- _texture:null,
- _textureCopy:null,
- _uITextureImage:null,
- _pixelFormat:cc.Texture2D.PIXEL_FORMAT_RGBA8888,
- _clearColor:null,
- clearStencilVal:0,
- _clearColorStr:null,
- _className:"RenderTexture",
- /**
- * creates a RenderTexture object with width and height in Points and a pixel format, only RGB and RGBA formats are valid
- * Constructor of cc.RenderTexture for Canvas
- * @param {Number} width
- * @param {Number} height
- * @param {cc.IMAGE_FORMAT_JPEG|cc.IMAGE_FORMAT_PNG|cc.IMAGE_FORMAT_RAWDATA} format
- * @param {Number} depthStencilFormat
- * @example
- * // Example
- * var rt = new cc.RenderTexture(width, height, format, depthStencilFormat)
- * @function
- */
- ctor: null,
- _ctorForCanvas: function (width, height, format, depthStencilFormat) {
- cc.Node.prototype.ctor.call(this);
- this._cascadeColorEnabled = true;
- this._cascadeOpacityEnabled = true;
- this._clearColor = cc.color(255, 255, 255, 255);
- this._clearColorStr = "rgba(255,255,255,1)";
- this._cacheCanvas = cc.newElement('canvas');
- this._cacheContext = this._cacheCanvas.getContext('2d');
- this.anchorX = 0;
- this.anchorY = 0;
- if(width !== undefined && height !== undefined){
- format = format || cc.Texture2D.PIXEL_FORMAT_RGBA8888;
- depthStencilFormat = depthStencilFormat || 0;
- this.initWithWidthAndHeight(width, height, format, depthStencilFormat);
- }
- },
- _ctorForWebGL: function (width, height, format, depthStencilFormat) {
- cc.Node.prototype.ctor.call(this);
- this._cascadeColorEnabled = true;
- this._cascadeOpacityEnabled = true;
- this._clearColor = cc.color(0, 0, 0, 0);
- if(width !== undefined && height !== undefined){
- format = format || cc.Texture2D.PIXEL_FORMAT_RGBA8888;
- depthStencilFormat = depthStencilFormat || 0;
- this.initWithWidthAndHeight(width, height, format, depthStencilFormat);
- }
- },
- /**
- * Clear RenderTexture.
- * @function
- */
- cleanup:null,
- _cleanupForCanvas:function () {
- cc.Node.prototype.onExit.call(this);
- this._cacheContext = null;
- this._cacheCanvas = null;
- },
- _cleanupForWebGL: function () {
- cc.Node.prototype.onExit.call(this);
- //this.sprite = null;
- this._textureCopy = null;
- var gl = cc._renderContext;
- gl.deleteFramebuffer(this._fBO);
- if (this._depthRenderBuffer)
- gl.deleteRenderbuffer(this._depthRenderBuffer);
- this._uITextureImage = null;
- //if (this._texture)
- // this._texture.releaseTexture();
- },
- /**
- * Gets the sprite
- * @return {cc.Sprite}
- */
- getSprite:function () {
- return this.sprite;
- },
- /**
- * Set the sprite
- * @param {cc.Sprite} sprite
- */
- setSprite:function (sprite) {
- this.sprite = sprite;
- },
- /**
- * Initializes the instance of cc.RenderTexture
- * @function
- * @param {Number} width
- * @param {Number} height
- * @param {cc.IMAGE_FORMAT_JPEG|cc.IMAGE_FORMAT_PNG|cc.IMAGE_FORMAT_RAWDATA} [format]
- * @param {Number} [depthStencilFormat]
- * @return {Boolean}
- */
- initWithWidthAndHeight: null,
- _initWithWidthAndHeightForCanvas: function (width, height, format, depthStencilFormat) {
- var locCacheCanvas = this._cacheCanvas, locScaleFactor = cc.contentScaleFactor();
- locCacheCanvas.width = 0 | (width * locScaleFactor);
- locCacheCanvas.height = 0 | (height * locScaleFactor);
- this._cacheContext.translate(0, locCacheCanvas.height);
- var texture = new cc.Texture2D();
- texture.initWithElement(locCacheCanvas);
- texture.handleLoadedTexture();
- var locSprite = this.sprite = cc.Sprite.create(texture);
- locSprite.setBlendFunc(cc.ONE, cc.ONE_MINUS_SRC_ALPHA);
- // Disabled by default.
- this.autoDraw = false;
- // add sprite for backward compatibility
- this.addChild(locSprite);
- return true;
- },
- _initWithWidthAndHeightForWebGL: function (width, height, format, depthStencilFormat) {
- if(format == cc.Texture2D.PIXEL_FORMAT_A8)
- cc.log( "cc.RenderTexture._initWithWidthAndHeightForWebGL() : only RGB and RGBA formats are valid for a render texture;");
- var gl = cc._renderContext, locScaleFactor = cc.contentScaleFactor();
- width = 0 | (width * locScaleFactor);
- height = 0 | (height * locScaleFactor);
- this._oldFBO = gl.getParameter(gl.FRAMEBUFFER_BINDING);
- // textures must be power of two squared
- var powW , powH;
- if (cc.configuration.supportsNPOT()) {
- powW = width;
- powH = height;
- } else {
- powW = cc.NextPOT(width);
- powH = cc.NextPOT(height);
- }
- //void *data = malloc(powW * powH * 4);
- var dataLen = powW * powH * 4;
- var data = new Uint8Array(dataLen);
- //memset(data, 0, (int)(powW * powH * 4));
- for (var i = 0; i < powW * powH * 4; i++)
- data[i] = 0;
- this._pixelFormat = format;
- this._texture = new cc.Texture2D();
- if (!this._texture)
- return false;
- var locTexture = this._texture;
- locTexture.initWithData(data, this._pixelFormat, powW, powH, cc.size(width, height));
- //free( data );
- var oldRBO = gl.getParameter(gl.RENDERBUFFER_BINDING);
- if (cc.configuration.checkForGLExtension("GL_QCOM")) {
- this._textureCopy = new cc.Texture2D();
- if (!this._textureCopy) {
- return false;
- }
- this._textureCopy.initWithData(data, this._pixelFormat, powW, powH, cc.size(width, height));
- }
- // generate FBO
- this._fBO = gl.createFramebuffer();
- gl.bindFramebuffer(gl.FRAMEBUFFER, this._fBO);
- // associate texture with FBO
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, locTexture._webTextureObj, 0);
- if (depthStencilFormat != 0) {
- //create and attach depth buffer
- this._depthRenderBuffer = gl.createRenderbuffer();
- gl.bindRenderbuffer(gl.RENDERBUFFER, this._depthRenderBuffer);
- gl.renderbufferStorage(gl.RENDERBUFFER, depthStencilFormat, powW, powH);
- if(depthStencilFormat == gl.DEPTH_STENCIL)
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthRenderBuffer);
- else if(depthStencilFormat == gl.STENCIL_INDEX || depthStencilFormat == gl.STENCIL_INDEX8)
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthRenderBuffer);
- else if(depthStencilFormat == gl.DEPTH_COMPONENT16)
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this._depthRenderBuffer);
- }
- // check if it worked (probably worth doing :) )
- if(gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE)
- cc.log("Could not attach texture to the framebuffer");
- locTexture.setAliasTexParameters();
- this.sprite = cc.Sprite.create(locTexture);
- var locSprite = this.sprite;
- locSprite.scaleY = -1;
- locSprite.setBlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
- gl.bindRenderbuffer(gl.RENDERBUFFER, oldRBO);
- gl.bindFramebuffer(gl.FRAMEBUFFER, this._oldFBO);
- // Disabled by default.
- this.autoDraw = false;
- // add sprite for backward compatibility
- this.addChild(locSprite);
- return true;
- },
- /**
- * starts grabbing
- * @function
- */
- begin: null,
- _beginForCanvas: function () {
- cc._renderContext = this._cacheContext;
- cc.view._setScaleXYForRenderTexture();
- /*// Save the current matrix
- cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
- cc.kmGLPushMatrix();
- cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
- cc.kmGLPushMatrix();*/
- },
- _beginForWebGL: function () {
- // Save the current matrix
- cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
- cc.kmGLPushMatrix();
- cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
- cc.kmGLPushMatrix();
- var director = cc.director;
- director.setProjection(director.getProjection());
- var texSize = this._texture.getContentSizeInPixels();
- // Calculate the adjustment ratios based on the old and new projections
- var size = cc.director.getWinSizeInPixels();
- var widthRatio = size.width / texSize.width;
- var heightRatio = size.height / texSize.height;
- var gl = cc._renderContext;
- // Adjust the orthographic projection and viewport
- gl.viewport(0, 0, texSize.width, texSize.height);
- var orthoMatrix = new cc.kmMat4();
- cc.kmMat4OrthographicProjection(orthoMatrix, -1.0 / widthRatio, 1.0 / widthRatio,
- -1.0 / heightRatio, 1.0 / heightRatio, -1, 1);
- cc.kmGLMultMatrix(orthoMatrix);
- this._oldFBO = gl.getParameter(gl.FRAMEBUFFER_BINDING);
- gl.bindFramebuffer(gl.FRAMEBUFFER, this._fBO);//Will direct drawing to the frame buffer created above
- /* Certain Qualcomm Andreno gpu's will retain data in memory after a frame buffer switch which corrupts the render to the texture.
- * The solution is to clear the frame buffer before rendering to the texture. However, calling glClear has the unintended result of clearing the current texture.
- * Create a temporary texture to overcome this. At the end of CCRenderTexture::begin(), switch the attached texture to the second one, call glClear,
- * and then switch back to the original texture. This solution is unnecessary for other devices as they don't have the same issue with switching frame buffers.
- */
- if (cc.configuration.checkForGLExtension("GL_QCOM")) {
- // -- bind a temporary texture so we can clear the render buffer without losing our texture
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._textureCopy._webTextureObj, 0);
- //cc.checkGLErrorDebug();
- gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._texture._webTextureObj, 0);
- }
- },
- /**
- * starts rendering to the texture while clearing the texture first.<br/>
- * This is more efficient then calling -clear first and then -begin
- * @param {Number} r red 0-255
- * @param {Number} g green 0-255
- * @param {Number} b blue 0-255
- * @param {Number} a alpha 0-255 0 is transparent
- * @param {Number} [depthValue=]
- * @param {Number} [stencilValue=]
- */
- beginWithClear:function (r, g, b, a, depthValue, stencilValue) {
- var gl = cc._renderContext;
- depthValue = depthValue || gl.COLOR_BUFFER_BIT;
- stencilValue = stencilValue || (gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
- this._beginWithClear(r , g , b , a , depthValue, stencilValue, (gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT));
- },
- _beginWithClear: null,
- _beginWithClearForCanvas: function (r, g, b, a, depthValue, stencilValue, flags) {
- this.begin();
- r = r || 0;
- g = g || 0;
- b = b || 0;
- a = isNaN(a) ? 1 : a;
- //var context = cc._renderContext;
- var context = this._cacheContext;
- var locCanvas = this._cacheCanvas;
- context.save();
- context.fillStyle = "rgba(" + (0 | r) + "," + (0 | g) + "," + (0 | b) + "," + a / 255 + ")";
- context.clearRect(0, 0, locCanvas.width, -locCanvas.height);
- context.fillRect(0, 0, locCanvas.width, -locCanvas.height);
- context.restore();
- },
- _beginWithClearForWebGL: function (r, g, b, a, depthValue, stencilValue, flags) {
- r = r / 255;
- g = g / 255;
- b = b / 255;
- a = a / 255;
- this.begin();
- var gl = cc._renderContext;
- // save clear color
- var clearColor = [0.0, 0.0, 0.0, 0.0];
- var depthClearValue = 0.0;
- var stencilClearValue = 0;
- if (flags & gl.COLOR_BUFFER_BIT) {
- clearColor = gl.getParameter(gl.COLOR_CLEAR_VALUE);
- gl.clearColor(r, g, b, a);
- }
- if (flags & gl.DEPTH_BUFFER_BIT) {
- depthClearValue = gl.getParameter(gl.DEPTH_CLEAR_VALUE);
- gl.clearDepth(depthValue);
- }
- if (flags & gl.STENCIL_BUFFER_BIT) {
- stencilClearValue = gl.getParameter(gl.STENCIL_CLEAR_VALUE);
- gl.clearStencil(stencilValue);
- }
- gl.clear(flags);
- // restore
- if (flags & gl.COLOR_BUFFER_BIT)
- gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
- if (flags & gl.DEPTH_BUFFER_BIT)
- gl.clearDepth(depthClearValue);
- if (flags & gl.STENCIL_BUFFER_BIT)
- gl.clearStencil(stencilClearValue);
- },
- /**
- * ends grabbing
- * @function
- */
- end: null,
- _endForCanvas: function () {
- cc._renderContext = cc._mainRenderContextBackup;
- cc.view._resetScale();
- //TODO
- /*//restore viewport
- director.setViewport();
- cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
- cc.kmGLPopMatrix();
- cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
- cc.kmGLPopMatrix();*/
- },
- _endForWebGL: function () {
- var gl = cc._renderContext;
- var director = cc.director;
- gl.bindFramebuffer(gl.FRAMEBUFFER, this._oldFBO);
- //restore viewport
- director.setViewport();
- cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
- cc.kmGLPopMatrix();
- cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
- cc.kmGLPopMatrix();
- /* var size = director.getWinSizeInPixels();
- // restore viewport
- gl.viewport(0, 0, size.width * cc.contentScaleFactor(), size.height * cc.contentScaleFactor());
- // special viewport for 3d projection + retina display
- if (director.getProjection() == cc.Director.PROJECTION_3D && cc.contentScaleFactor() != 1) {
- gl.viewport((-size.width / 2), (-size.height / 2), (size.width * cc.contentScaleFactor()), (size.height * cc.contentScaleFactor()));
- }
- director.setProjection(director.getProjection());*/
- },
- /**
- * clears the texture with a color
- * @param {Number|cc.Rect} r red 0-1
- * @param {Number} g green 0-1
- * @param {Number} b blue 0-1
- * @param {Number} a alpha 0-1
- */
- clear:function (r, g, b, a) {
- this.beginWithClear(r, g, b, a);
- this.end();
- },
- /**
- * clears the texture with rect.
- * @function
- * @param {number} x
- * @param {number} y
- * @param {number} width
- * @param {number} height
- */
- clearRect:null,
- _clearRectForCanvas:function(x, y, width, height){
- this._cacheContext.clearRect(x, y, width, -height);
- },
- _clearRectForWebGL:function(x, y, width, height){
- //TODO need to implement
- },
- /**
- * clears the texture with a specified depth value
- * @function
- * @param {Number} depthValue
- */
- clearDepth:null,
- _clearDepthForCanvas:function (depthValue) {
- cc.log("clearDepth isn't supported on Cocos2d-Html5");
- },
- _clearDepthForWebGL:function (depthValue) {
- this.begin();
- var gl = cc._renderContext;
- //! save old depth value
- var depthClearValue = gl.getParameter(gl.DEPTH_CLEAR_VALUE);
- gl.clearDepth(depthValue);
- gl.clear(gl.DEPTH_BUFFER_BIT);
- // restore clear color
- gl.clearDepth(depthClearValue);
- this.end();
- },
- /**
- * clears the texture with a specified stencil value
- * @function
- * @param {Number} stencilValue
- */
- clearStencil:null,
- _clearStencilForCanvas:function (stencilValue) {
- cc.log("clearDepth isn't supported on Cocos2d-Html5");
- },
- _clearStencilForWebGL:function (stencilValue) {
- var gl = cc._renderContext;
- // save old stencil value
- var stencilClearValue = gl.getParameter(gl.STENCIL_CLEAR_VALUE);
- gl.clearStencil(stencilValue);
- gl.clear(gl.STENCIL_BUFFER_BIT);
- // restore clear color
- gl.clearStencil(stencilClearValue);
- },
- /**
- * Recursive method that visit its children and draw them
- * @function
- * @param {CanvasRenderingContext2D|WebGLRenderingContext} ctx
- */
- visit:null,
- _visitForCanvas:function (ctx) {
- // override visit.
- // Don't call visit on its children
- if (!this._visible)
- return;
- ctx = ctx || cc._renderContext;
- ctx.save();
- this.draw(ctx); // update children of RenderTexture before
- this.transform(ctx);
- this.sprite.visit(); // draw the RenderTexture
- ctx.restore();
- this.arrivalOrder = 0;
- },
- _visitForWebGL:function (ctx) {
- // override visit.
- // Don't call visit on its children
- if (!this._visible)
- return;
- cc.kmGLPushMatrix();
- var locGrid = this.grid;
- if (locGrid && locGrid.isActive()) {
- locGrid.beforeDraw();
- this.transformAncestors();
- }
- this.transform(ctx);
- this.sprite.visit();
- this.draw(ctx);
- if (locGrid && locGrid.isActive())
- locGrid.afterDraw(this);
- cc.kmGLPopMatrix();
- this.arrivalOrder = 0;
- },
- /**
- * Render function using the canvas 2d context or WebGL context, internal usage only, please do not call this function
- * @function
- * @param {CanvasRenderingContext2D | WebGLRenderingContext} ctx The render context
- */
- draw:null,
- _drawForCanvas: function (ctx) {
- ctx = ctx || cc._renderContext;
- if (this.autoDraw) {
- this.begin();
- if (this.clearFlags) {
- var locCanvas = this._cacheCanvas;
- ctx.save();
- ctx.fillStyle = this._clearColorStr;
- ctx.clearRect(0, 0, locCanvas.width, -locCanvas.height);
- ctx.fillRect(0, 0, locCanvas.width, -locCanvas.height);
- ctx.restore();
- }
- //! make sure all children are drawn
- this.sortAllChildren();
- var locChildren = this._children;
- var childrenLen = locChildren.length;
- var selfSprite = this.sprite;
- for (var i = 0; i < childrenLen; i++) {
- var getChild = locChildren[i];
- if (getChild != selfSprite)
- getChild.visit();
- }
- this.end();
- }
- },
- _drawForWebGL: function (ctx) {
- var gl = cc._renderContext;
- if (this.autoDraw) {
- this.begin();
- var locClearFlags = this.clearFlags;
- if (locClearFlags) {
- var oldClearColor = [0.0, 0.0, 0.0, 0.0];
- var oldDepthClearValue = 0.0;
- var oldStencilClearValue = 0;
- // backup and set
- if (locClearFlags & gl.COLOR_BUFFER_BIT) {
- oldClearColor = gl.getParameter(gl.COLOR_CLEAR_VALUE);
- gl.clearColor(this._clearColor.r/255, this._clearColor.g/255, this._clearColor.b/255, this._clearColor.a/255);
- }
- if (locClearFlags & gl.DEPTH_BUFFER_BIT) {
- oldDepthClearValue = gl.getParameter(gl.DEPTH_CLEAR_VALUE);
- gl.clearDepth(this.clearDepthVal);
- }
- if (locClearFlags & gl.STENCIL_BUFFER_BIT) {
- oldStencilClearValue = gl.getParameter(gl.STENCIL_CLEAR_VALUE);
- gl.clearStencil(this.clearStencilVal);
- }
- // clear
- gl.clear(locClearFlags);
- // restore
- if (locClearFlags & gl.COLOR_BUFFER_BIT)
- gl.clearColor(oldClearColor[0], oldClearColor[1], oldClearColor[2], oldClearColor[3]);
- if (locClearFlags & gl.DEPTH_BUFFER_BIT)
- gl.clearDepth(oldDepthClearValue);
- if (locClearFlags & gl.STENCIL_BUFFER_BIT)
- gl.clearStencil(oldStencilClearValue);
- }
- //! make sure all children are drawn
- this.sortAllChildren();
- var locChildren = this._children;
- for (var i = 0; i < locChildren.length; i++) {
- var getChild = locChildren[i];
- if (getChild != this.sprite)
- getChild.visit();
- }
- this.end();
- }
- },
- /**
- * creates a new CCImage from with the texture's data. Caller is responsible for releasing it by calling delete.
- * @return {*}
- */
- newCCImage:function(flipImage){
- cc.log("saveToFile isn't supported on cocos2d-html5");
- return null;
- },
- _memcpy:function (destArr, destIndex, srcArr, srcIndex, size) {
- for (var i = 0; i < size; i++) {
- destArr[destIndex + i] = srcArr[srcIndex + i];
- }
- },
- /**
- * saves the texture into a file using JPEG format. The file will be saved in the Documents folder.
- * Returns YES if the operation is successful.
- * (doesn't support in HTML5)
- * @param {Number} filePath
- * @param {Number} format
- */
- saveToFile:function (filePath, format) {
- cc.log("saveToFile isn't supported on Cocos2d-Html5");
- },
- /**
- * Listen "come to background" message, and save render texture. It only has effect on Android.
- * @param {cc.Class} obj
- */
- listenToBackground:function (obj) {
- cc.log("listenToBackground isn't supported on Cocos2d-Html5");
- },
- /**
- * Listen "come to foreground" message and restore the frame buffer object. It only has effect on Android.
- * @param {cc.Class} obj
- */
- listenToForeground:function (obj) {
- cc.log("listenToForeground isn't supported on Cocos2d-Html5");
- },
- /**
- * Valid flags: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT. They can be OR'ed. Valid when "autoDraw is YES.
- * @return {Number}
- */
- getClearFlags:function () {
- return this.clearFlags;
- },
- /**
- * Set the clearFlags
- * @param {Number} clearFlags
- */
- setClearFlags:function (clearFlags) {
- this.clearFlags = clearFlags;
- },
- /**
- * Clear color value. Valid only when "autoDraw" is true.
- * @function
- * @return {cc.Color}
- */
- getClearColor:function () {
- return this._clearColor;
- },
- /**
- * Set the clear color value. Valid only when "autoDraw" is true.
- * @function
- * @param {cc.Color} clearColor The clear color
- */
- setClearColor:null,
- _setClearColorForCanvas:function (clearColor) {
- var locClearColor = this._clearColor;
- locClearColor.r = clearColor.r;
- locClearColor.g = clearColor.g;
- locClearColor.b = clearColor.b;
- locClearColor.a = clearColor.a;
- this._clearColorStr = "rgba(" + (0 | clearColor.r) + "," + (0 | clearColor.g) + "," + (0 | clearColor.b) + "," + clearColor.a / 255 + ")";
- },
- _setClearColorForWebGL:function (clearColor) {
- var locClearColor = this._clearColor;
- locClearColor.r = clearColor.r;
- locClearColor.g = clearColor.g;
- locClearColor.b = clearColor.b;
- locClearColor.a = clearColor.a;
- },
- /**
- * Value for clearDepth. Valid only when autoDraw is true.
- * @return {Number}
- */
- getClearDepth:function () {
- return this.clearDepthVal;
- },
- /**
- * Set value for clearDepth. Valid only when autoDraw is true.
- * @param {Number} clearDepth
- */
- setClearDepth:function (clearDepth) {
- this.clearDepthVal = clearDepth;
- },
- /**
- * Value for clear Stencil. Valid only when autoDraw is true
- * @return {Number}
- */
- getClearStencil:function () {
- return this.clearStencilVal;
- },
- /**
- * Set value for clear Stencil. Valid only when autoDraw is true
- * @return {Number}
- */
- setClearStencil:function (clearStencil) {
- this.clearStencilVal = clearStencil;
- },
- /**
- * When enabled, it will render its children into the texture automatically. Disabled by default for compatiblity reasons. <br/>
- * Will be enabled in the future.
- * @return {Boolean}
- */
- isAutoDraw:function () {
- return this.autoDraw;
- },
- /**
- * When enabled, it will render its children into the texture automatically. Disabled by default for compatiblity reasons. <br/>
- * Will be enabled in the future.
- * @return {Boolean}
- */
- setAutoDraw:function (autoDraw) {
- this.autoDraw = autoDraw;
- }
- });
- var _p = cc.RenderTexture.prototype;
- if(cc._renderType == cc._RENDER_TYPE_WEBGL){
- _p.ctor = _p._ctorForWebGL;
- _p.cleanup = _p._cleanupForWebGL;
- _p.initWithWidthAndHeight = _p._initWithWidthAndHeightForWebGL;
- _p.begin = _p._beginForWebGL;
- _p._beginWithClear = _p._beginWithClearForWebGL;
- _p.end = _p._endForWebGL;
- _p.clearRect = _p._clearRectForWebGL;
- _p.clearDepth = _p._clearDepthForWebGL;
- _p.clearStencil = _p._clearStencilForWebGL;
- _p.visit = _p._visitForWebGL;
- _p.draw = _p._drawForWebGL;
- _p.setClearColor = _p._setClearColorForWebGL;
- } else {
- _p.ctor = _p._ctorForCanvas;
- _p.cleanup = _p._cleanupForCanvas;
- _p.initWithWidthAndHeight = _p._initWithWidthAndHeightForCanvas;
- _p.begin = _p._beginForCanvas;
- _p._beginWithClear = _p._beginWithClearForCanvas;
- _p.end = _p._endForCanvas;
- _p.clearRect = _p._clearRectForCanvas;
- _p.clearDepth = _p._clearDepthForCanvas;
- _p.clearStencil = _p._clearStencilForCanvas;
- _p.visit = _p._visitForCanvas;
- _p.draw = _p._drawForCanvas;
- _p.setClearColor = _p._setClearColorForCanvas;
- }
- // Extended
- /** @expose */
- _p.clearColorVal;
- cc.defineGetterSetter(_p, "clearColorVal", _p.getClearColor, _p.setClearColor);
- /**
- * creates a RenderTexture object with width and height in Points and a pixel format, only RGB and RGBA formats are valid
- * @deprecated since v3.0 please use new cc.RenderTexture() instead.
- * @param {Number} width
- * @param {Number} height
- * @param {cc.IMAGE_FORMAT_JPEG|cc.IMAGE_FORMAT_PNG|cc.IMAGE_FORMAT_RAWDATA} format
- * @param {Number} depthStencilFormat
- * @return {cc.RenderTexture}
- * @example
- * // Example
- * var rt = cc.RenderTexture.create()
- */
- cc.RenderTexture.create = function (width, height, format, depthStencilFormat) {
- return new cc.RenderTexture(width, height, format, depthStencilFormat);
- };
|