/**************************************************************************** Copyright (c) 2013-2014 Chukong Technologies Inc. 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. ****************************************************************************/ /** * cc.ProgressTimer's rendering objects of WebGL */ (function () { var MAX_VERTEX_COUNT = 8; cc.ProgressTimer.WebGLRenderCmd = function (renderableObject) { this._rootCtor(renderableObject); this._needDraw = true; this._progressDirty = true; this._bl = cc.p(); this._tr = cc.p(); this._transformUpdating = false; this.initCmd(); }; var proto = cc.ProgressTimer.WebGLRenderCmd.prototype = Object.create(cc.Node.WebGLRenderCmd.prototype); proto.constructor = cc.ProgressTimer.WebGLRenderCmd; proto.transform = function (parentCmd, recursive) { this.originTransform(parentCmd, recursive); var sp = this._node._sprite; sp._renderCmd.transform(this, recursive); var lx = sp._offsetPosition.x, rx = lx + sp._rect.width, by = sp._offsetPosition.y, ty = by + sp._rect.height, wt = this._worldTransform; this._bl.x = lx * wt.a + by * wt.c + wt.tx; this._bl.y = lx * wt.b + by * wt.d + wt.ty; this._tr.x = rx * wt.a + ty * wt.c + wt.tx; this._tr.y = rx * wt.b + ty * wt.d + wt.ty; this._transformUpdating = true; this._updateProgressData(); this._transformUpdating = false; }; proto.rendering = function (ctx) { var node = this._node; var context = ctx || cc._renderContext; if (this._vertexDataCount === 0 || !node._sprite) return; this._glProgramState.apply(); this._shaderProgram._updateProjectionUniform(); var blendFunc = node._sprite._blendFunc; cc.glBlendFunc(blendFunc.src, blendFunc.dst); cc.glBindTexture2D(node._sprite.texture); context.bindBuffer(context.ARRAY_BUFFER, this._vertexWebGLBuffer); context.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION); context.enableVertexAttribArray(cc.VERTEX_ATTRIB_COLOR); context.enableVertexAttribArray(cc.VERTEX_ATTRIB_TEX_COORDS); if (this._vertexDataDirty) { context.bufferSubData(context.ARRAY_BUFFER, 0, this._float32View); this._vertexDataDirty = false; } var locVertexDataLen = cc.V3F_C4B_T2F.BYTES_PER_ELEMENT; context.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, context.FLOAT, false, locVertexDataLen, 0); context.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, locVertexDataLen, 12); context.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, context.FLOAT, false, locVertexDataLen, 16); if (node._type === cc.ProgressTimer.TYPE_RADIAL) context.drawArrays(context.TRIANGLE_FAN, 0, this._vertexDataCount); else if (node._type === cc.ProgressTimer.TYPE_BAR) { if (!node._reverseDirection) context.drawArrays(context.TRIANGLE_STRIP, 0, this._vertexDataCount); else { context.drawArrays(context.TRIANGLE_STRIP, 0, this._vertexDataCount / 2); context.drawArrays(context.TRIANGLE_STRIP, 4, this._vertexDataCount / 2); // 2 draw calls cc.g_NumberOfDraws++; } } cc.g_NumberOfDraws++; }; proto._syncStatus = function (parentCmd) { var node = this._node; if (!node._sprite) return; var flags = cc.Node._dirtyFlags, locFlag = this._dirtyFlag; var parentNode = parentCmd ? parentCmd._node : null; if (parentNode && parentNode._cascadeColorEnabled && (parentCmd._dirtyFlag & flags.colorDirty)) locFlag |= flags.colorDirty; if (parentNode && parentNode._cascadeOpacityEnabled && (parentCmd._dirtyFlag & flags.opacityDirty)) locFlag |= flags.opacityDirty; if (parentCmd && (parentCmd._dirtyFlag & flags.transformDirty)) locFlag |= flags.transformDirty; this._dirtyFlag = locFlag; var spriteCmd = node._sprite._renderCmd; var spriteFlag = spriteCmd._dirtyFlag; var colorDirty = (locFlag | spriteFlag) & flags.colorDirty, opacityDirty = (locFlag | spriteFlag) & flags.opacityDirty; if (colorDirty) { spriteCmd._syncDisplayColor(); spriteCmd._dirtyFlag &= ~flags.colorDirty; this._dirtyFlag &= ~flags.colorDirty; } if (opacityDirty) { spriteCmd._syncDisplayOpacity(); spriteCmd._dirtyFlag &= ~flags.opacityDirty; this._dirtyFlag &= ~flags.opacityDirty; } if (colorDirty || opacityDirty) { this._updateColor(); } if (locFlag & flags.transformDirty) { //update the transform this.transform(parentCmd); } if (locFlag & flags.textureDirty) { this._updateProgressData(); this._dirtyFlag &= ~flags.textureDirty; } spriteCmd._dirtyFlag = 0; }; proto.updateStatus = function () { var node = this._node; if (!node._sprite) return; var flags = cc.Node._dirtyFlags, locFlag = this._dirtyFlag; var spriteCmd = node._sprite._renderCmd; var spriteFlag = spriteCmd._dirtyFlag; var colorDirty = (locFlag | spriteFlag) & flags.colorDirty, opacityDirty = (locFlag | spriteFlag) & flags.opacityDirty; if (colorDirty) { spriteCmd._updateDisplayColor(); spriteCmd._dirtyFlag = spriteCmd._dirtyFlag & flags.colorDirty ^ spriteCmd._dirtyFlag; this._dirtyFlag = this._dirtyFlag & flags.colorDirty ^ this._dirtyFlag; } if (opacityDirty) { spriteCmd._updateDisplayOpacity(); spriteCmd._dirtyFlag = spriteCmd._dirtyFlag & flags.opacityDirty ^ spriteCmd._dirtyFlag; this._dirtyFlag = this._dirtyFlag & flags.opacityDirty ^ this._dirtyFlag; } if (colorDirty || opacityDirty) { this._updateColor(); } if (locFlag & flags.transformDirty) { //update the transform this.transform(this.getParentRenderCmd(), true); } if (locFlag & flags.orderDirty) { this._dirtyFlag = this._dirtyFlag & flags.orderDirty ^ this._dirtyFlag; } if (locFlag & flags.textureDirty) { this._updateProgressData(); this._dirtyFlag = this._dirtyFlag & flags.textureDirty ^ this._dirtyFlag; } }; proto.releaseData = function () { if (this._vertexData) { //release all previous information var webglBuffer = this._vertexWebGLBuffer; setTimeout(function () { cc._renderContext.deleteBuffer(webglBuffer); }, 0.1); this._vertexWebGLBuffer = null; this._vertexData = null; this._float32View = null; this._vertexArrayBuffer = null; this._vertexDataCount = 0; } }; proto.initCmd = function () { if (!this._vertexData) { this._vertexWebGLBuffer = cc._renderContext.createBuffer(); var vertexDataLen = cc.V3F_C4B_T2F.BYTES_PER_ELEMENT; this._vertexArrayBuffer = new ArrayBuffer(MAX_VERTEX_COUNT * vertexDataLen); this._float32View = new Float32Array(this._vertexArrayBuffer); this._vertexData = []; for (var i = 0; i < MAX_VERTEX_COUNT; i++) { this._vertexData[i] = new cc.V3F_C4B_T2F(null, null, null, this._vertexArrayBuffer, i * vertexDataLen); } // Init buffer data gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexWebGLBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._float32View, gl.DYNAMIC_DRAW); this._vertexDataCount = 0; this._vertexDataDirty = true; //shader program this._shaderProgram = cc.shaderCache.programForKey(cc.SHADER_SPRITE_POSITION_TEXTURECOLOR); } }; proto.resetVertexData = function () { this._vertexDataCount = 0; }; proto._updateProgressData = function () { var node = this._node; var locType = node._type; if (locType === cc.ProgressTimer.TYPE_RADIAL) this._updateRadial(); else if (locType === cc.ProgressTimer.TYPE_BAR) this._updateBar(); this._vertexDataDirty = true; }; proto._updateProgress = function () { this.setDirtyFlag(cc.Node._dirtyFlags.textureDirty); }; /** *
* Update does the work of mapping the texture onto the triangles for the bar
* It now doesn't occur the cost of free/alloc data every update cycle.
* It also only changes the percentage point but no other points if they have not been modified.
*
* It now deals with flipped texture. If you run into this problem, just use the
* sprite property and enable the methods flipX, flipY.
*
* Update does the work of mapping the texture onto the triangles
* It now doesn't occur the cost of free/alloc data every update cycle.
* It also only changes the percentage point but no other points if they have not been modified.
*
* It now deals with flipped texture. If you run into this problem, just use the
* sprite property and enable the methods flipX, flipY.
*