1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2011 Zynga 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.
- ****************************************************************************/
- //CONSTANTS:
- //----------------------Possible texture pixel formats----------------------------
- /**
- * 32-bit texture: RGBA8888
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888 = 0;
- /**
- * 24-bit texture: RGBA888
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_RGB888 = 1;
- /**
- * 16-bit texture without Alpha channel
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_RGB565 = 2;
- /**
- * 8-bit textures used as masks
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_A8 = 3;
- /**
- * 8-bit intensity texture
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_I8 = 4;
- /**
- * 16-bit textures used as masks
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_AI88 = 5;
- /**
- * 16-bit textures: RGBA4444
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444 = 6;
- /**
- * 16-bit textures: RGB5A1
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1 = 7;
- /**
- * 4-bit PVRTC-compressed texture: PVRTC4
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4 = 8;
- /**
- * 2-bit PVRTC-compressed texture: PVRTC2
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2 = 9;
- /**
- * Default texture format: RGBA8888
- * @constant
- * @type {Number}
- */
- cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT = cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888;
- // If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
- // Default is: RGBA8888 (32-bit textures)
- cc._defaultAlphaPixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT;
- // By default PVR images are treated as if they don't have the alpha channel premultiplied
- cc.PVRHaveAlphaPremultiplied_ = false;
- /**
- Extension to set the Min / Mag filter
- */
- cc._texParams = function (minFilter, magFilter, wrapS, wrapT) {
- this.minFilter = minFilter || 0;
- this.magFilter = magFilter || 0;
- this.wrapS = wrapS || 0;
- this.wrapT = wrapT || 0;
- };
- /**
- * <p>
- * This class allows to easily create OpenGL 2D textures from images, text or raw data. <br/>
- * The created cc.Texture2D object will always have power-of-two dimensions. <br/>
- * Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions <br/>
- * i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0). <br/>
- * Be aware that the content of the generated textures will be upside-down! </p>
- * @class
- * @extends cc.Class
- */
- cc.Texture2DWebGL = cc.Class.extend(/** @lends cc.Texture2D# */{
- // By default PVR images are treated as if they don't have the alpha channel premultiplied
- _pVRHaveAlphaPremultiplied:null,
- _pixelFormat:null,
- _pixelsWide:null,
- _pixelsHigh:null,
- _name:null,
- _contentSize:null,
- _maxS:null,
- _maxT:null,
- _hasPremultipliedAlpha:null,
- _hasMipmaps:false,
- _shaderProgram:null,
- _isLoaded:false,
- _htmlElementObj:null,
- _webTextureObj:null,
- _loadedEventListeners:null,
- /*public:*/
- ctor:function () {
- this._pixelsWide = 0;
- this._pixelsHigh = 0;
- this._name = "";
- this._maxS = 0;
- this._maxT = 0;
- this._hasPremultipliedAlpha = false;
- this._contentSize = cc.size(0, 0);
- this._hasMipmaps = false;
- this._pVRHaveAlphaPremultiplied = true;
- this._pixelFormat = cc.Texture2D.defaultAlphaPixelFormat();
- this._shaderProgram = null;
- this._isLoaded = false;
- this._htmlElementObj = null;
- this._webTextureObj = null;
- },
- releaseTexture:function () {
- if (this._webTextureObj)
- cc.renderContext.deleteTexture(this._webTextureObj);
- },
- /**
- * pixel format of the texture
- * @return {Number}
- */
- getPixelFormat:function () {
- return this._pixelFormat;
- },
- /**
- * width in pixels
- * @return {Number}
- */
- getPixelsWide:function () {
- return this._pixelsWide;
- },
- /**
- * hight in pixels
- * @return {Number}
- */
- getPixelsHigh:function () {
- return this._pixelsHigh;
- },
- /**
- * get WebGLTexture Object
- * @return {WebGLTexture}
- */
- getName:function () {
- return this._webTextureObj;
- },
- /**
- * content size
- * @return {cc.Size}
- */
- getContentSize:function () {
- return cc.size(this._contentSize.width / cc.CONTENT_SCALE_FACTOR(), this._contentSize.height / cc.CONTENT_SCALE_FACTOR());
- },
- getContentSizeInPixels:function () {
- return cc.size(this._contentSize);
- },
- /** texture max S */
- getMaxS:function () {
- return this._maxS;
- },
- setMaxS:function (maxS) {
- this._maxS = maxS;
- },
- /** texture max T */
- getMaxT:function () {
- return this._maxT;
- },
- setMaxT:function (maxT) {
- this._maxT = maxT;
- },
- /**
- * return shader program used by drawAtPoint and drawInRect
- * @return {cc.GLProgram}
- */
- getShaderProgram:function () {
- return this._shaderProgram;
- },
- /**
- * set shader program used by drawAtPoint and drawInRect
- * @param {cc.GLProgram} shaderProgram
- */
- setShaderProgram:function (shaderProgram) {
- this._shaderProgram = shaderProgram;
- },
- /**
- * whether or not the texture has their Alpha premultiplied
- * @return {Boolean}
- */
- hasPremultipliedAlpha:function () {
- return this._hasPremultipliedAlpha;
- },
- hasMipmaps:function () {
- return this._hasMipmaps;
- },
- description:function () {
- return "<cc.Texture2D | Name = " + this._name + " | Dimensions = " + this._pixelsWide + " x " + this._pixelsHigh
- + " | Coordinates = (" + this._maxS + ", " + this._maxT + ")>";
- },
- /**
- * These functions are needed to create mutable textures
- * @param {Array} data
- */
- releaseData:function (data) {
- data = null;
- },
- keepData:function (data, length) {
- //The texture data mustn't be saved becuase it isn't a mutable texture.
- return data;
- },
- /**
- * Intializes with a texture2d with data
- * @param {Array} data
- * @param {Number} pixelFormat
- * @param {Number} pixelsWide
- * @param {Number} pixelsHigh
- * @param {cc.Size} contentSize
- * @return {Boolean}
- */
- initWithData:function (data, pixelFormat, pixelsWide, pixelsHigh, contentSize) {
- var gl = cc.renderContext;
- var bitsPerPixel = 0;
- //Hack: bitsPerPixelForFormat returns wrong number for RGB_888 textures. See function.
- if (pixelFormat === cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888) {
- bitsPerPixel = 24;
- } else {
- bitsPerPixel = this.bitsPerPixelForFormat(pixelFormat);
- }
- var bytesPerRow = pixelsWide * bitsPerPixel / 8;
- if (bytesPerRow % 8 === 0) {
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, 8);
- } else if (bytesPerRow % 4 === 0) {
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
- } else if (bytesPerRow % 2 === 0) {
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, 2);
- } else {
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
- }
- this._webTextureObj = gl.createTexture();
- cc.glBindTexture2D(this);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
- // Specify OpenGL texture image
- switch (pixelFormat) {
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888:
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
- break;
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888:
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, pixelsWide, pixelsHigh, 0, gl.RGB, gl.UNSIGNED_BYTE, data);
- break;
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444:
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_SHORT_4_4_4_4, data);
- break;
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1:
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_SHORT_5_5_5_1, data);
- break;
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565:
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, pixelsWide, pixelsHigh, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, data);
- break;
- case cc.TEXTURE_2D_PIXEL_FORMAT_AI88:
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE_ALPHA, pixelsWide, pixelsHigh, 0, gl.LUMINANCE_ALPHA, gl.UNSIGNED_BYTE, data);
- break;
- case cc.TEXTURE_2D_PIXEL_FORMAT_A8:
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, pixelsWide, pixelsHigh, 0, gl.ALPHA, gl.UNSIGNED_BYTE, data);
- break;
- case cc.TEXTURE_2D_PIXEL_FORMAT_I8:
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, pixelsWide, pixelsHigh, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, data);
- break;
- default:
- throw "NSInternalInconsistencyException";
- break;
- }
- this._contentSize.width = contentSize.width;
- this._contentSize.height = contentSize.height;
- this._pixelsWide = pixelsWide;
- this._pixelsHigh = pixelsHigh;
- this._pixelFormat = pixelFormat;
- this._maxS = contentSize.width / pixelsWide;
- this._maxT = contentSize.height / pixelsHigh;
- this._hasPremultipliedAlpha = false;
- this._hasMipmaps = false;
- this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURE));
- this._isLoaded = true;
- return true;
- },
- /**
- Drawing extensions to make it easy to draw basic quads using a CCTexture2D object.
- These functions require gl.TEXTURE_2D and both gl.VERTEX_ARRAY and gl.TEXTURE_COORD_ARRAY client states to be enabled.
- */
- /**
- * draws a texture at a given point
- * @param {cc.Point} point
- */
- drawAtPoint:function (point) {
- var coordinates = [
- 0.0, this._maxT,
- this._maxS, this._maxT,
- 0.0, 0.0,
- this._maxS, 0.0 ];
- var width = this._pixelsWide * this._maxS,
- height = this._pixelsHigh * this._maxT;
- var vertices = [
- point.x, point.y, 0.0,
- width + point.x, point.y, 0.0,
- point.x, height + point.y, 0.0,
- width + point.x, height + point.y, 0.0 ];
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS);
- this._shaderProgram.use();
- this._shaderProgram.setUniformsForBuiltins();
- cc.glBindTexture2D(this);
- var gl = cc.renderContext;
- gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, vertices);
- gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, coordinates);
- gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
- },
- /**
- * draws a texture inside a rect
- * @param {cc.Rect} rect
- */
- drawInRect:function (rect) {
- var coordinates = [
- 0.0, this._maxT,
- this._maxS, this._maxT,
- 0.0, 0.0,
- this._maxS, 0.0];
- var vertices = [ rect.x, rect.y, /*0.0,*/
- rect.x + rect.width, rect.y, /*0.0,*/
- rect.x, rect.y + rect.height, /*0.0,*/
- rect.x + rect.width, rect.y + rect.height /*0.0*/ ];
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS);
- this._shaderProgram.use();
- this._shaderProgram.setUniformsForBuiltins();
- cc.glBindTexture2D(this);
- var gl = cc.renderContext;
- gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, vertices);
- gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, coordinates);
- gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
- },
- /**
- Extensions to make it easy to create a CCTexture2D object from an image file.
- Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA).
- */
- /**
- * Initializes a texture from a UIImage object
- * @param uiImage
- * @return {Boolean}
- */
- initWithImage:function (uiImage) {
- if (uiImage == null) {
- cc.log("cocos2d: cc.Texture2D. Can't create Texture. UIImage is nil");
- return false;
- }
- var imageWidth = uiImage.getWidth();
- var imageHeight = uiImage.getHeight();
- var conf = cc.Configuration.getInstance();
- var maxTextureSize = conf.getMaxTextureSize();
- if (imageWidth > maxTextureSize || imageHeight > maxTextureSize) {
- cc.log("cocos2d: WARNING: Image (" + imageWidth + " x " + imageHeight + ") is bigger than the supported " + maxTextureSize + " x " + maxTextureSize);
- return false;
- }
- this._isLoaded = true;
- // always load premultiplied images
- return this._initPremultipliedATextureWithImage(uiImage, imageWidth, imageHeight);
- },
- initWithElement:function (element) {
- if (!element)
- return;
- this._webTextureObj = cc.renderContext.createTexture();
- this._htmlElementObj = element;
- },
- /**
- * HTMLElement Object getter
- * @return {HTMLElement}
- */
- getHtmlElementObj:function(){
- return this._htmlElementObj;
- },
- isLoaded:function () {
- return this._isLoaded;
- },
- handleLoadedTexture:function () {
- this._isLoaded = true;
- //upload image to buffer
- var gl = cc.renderContext;
- cc.glBindTexture2D(this);
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
- // Specify OpenGL texture image
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._htmlElementObj);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
- this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURE));
- cc.glBindTexture2D(null);
- var pixelsWide = this._htmlElementObj.width;
- var pixelsHigh = this._htmlElementObj.height;
- this._pixelsWide = this._contentSize.width = pixelsWide;
- this._pixelsHigh = this._contentSize.height = pixelsHigh;
- this._pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888;
- this._maxS = 1;
- this._maxT = 1;
- this._hasPremultipliedAlpha = false;
- this._hasMipmaps = false;
- this._callLoadedEventCallbacks();
- },
- /**
- Extensions to make it easy to create a cc.Texture2D object from a string of text.
- Note that the generated textures are of type A8 - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA).
- */
- /**
- * Initializes a texture from a string with dimensions, alignment, font name and font size (note: initWithString does not support on HTML5)
- * @param {String} text
- * @param {String | cc.FontDefinition} fontName or fontDefinition
- * @param {Number} fontSize
- * @param {cc.Size} dimensions
- * @param {Number} hAlignment
- * @param {Number} vAlignment
- * @return {Boolean}
- */
- initWithString:function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) {
- if (arguments.length == 3) {
- fontName = arguments[1];
- fontSize = arguments[2];
- dimensions = cc.size(0, 0);
- hAlignment = cc.TEXT_ALIGNMENT_CENTER;
- vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
- }
- /*if (cc.ENABLE_CACHE_TEXTURE_DATA) {
- // cache the texture data
- cc.VolatileTexture.addStringTexture(this, text, dimensions, alignment, fontName, fontSize);
- }*/
- var image = new cc.Image();
- var eAlign;
- if (cc.VERTICAL_TEXT_ALIGNMENT_TOP === vAlignment) {
- eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_TOP
- : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_TOP_LEFT : cc.ALIGN_TOP_RIGHT;
- } else if (cc.VERTICAL_TEXT_ALIGNMENT_CENTER === vAlignment) {
- eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_CENTER
- : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_LEFT : cc.ALIGN_RIGHT;
- } else if (cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM === vAlignment) {
- eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_BOTTOM
- : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_BOTTOM_LEFT : cc.ALIGN_BOTTOM_RIGHT;
- } else {
- cc.log("Not supported alignment format!");
- return false;
- }
- if (!image.initWithString(text, dimensions.width, dimensions.height, eAlign, fontName, fontSize))
- return false;
- return this.initWithImage(image);
- },
- /**
- * Initializes a texture from a ETC file (note: initWithETCFile does not support on HTML5)
- * @note Compatible to Cocos2d-x
- * @param {String} file
- * @return {Boolean}
- */
- initWithETCFile:function (file) {
- return false;
- },
- /**
- * Initializes a texture from a PVR file
- * @param {String} file
- * @return {Boolean}
- */
- initWithPVRFile:function (file) {
- var ret = false;
- // nothing to do with cc.Object.init
- var pvr = new cc.TexturePVR;
- ret = pvr.initWithContentsOfFile(file);
- if (ret) {
- pvr.setRetainName(true); // don't dealloc texture on release
- this._name = pvr.getName();
- this._maxS = 1.0;
- this._maxT = 1.0;
- this._pixelsWide = pvr.getWidth();
- this._pixelsHigh = pvr.getHeight();
- this._contentSize.width = this._pixelsWide;
- this._contentSize.height = this._pixelsHigh;
- this._hasPremultipliedAlpha = cc.PVRHaveAlphaPremultiplied_;
- this._pixelFormat = pvr.getFormat();
- this.setAntiAliasTexParameters();
- } else {
- cc.log("cocos2d: Couldn't load PVR image " + file);
- }
- return ret;
- },
- /**
- Extensions to make it easy to create a cc.Texture2D object from a PVRTC file
- Note that the generated textures don't have their alpha premultiplied - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA).
- */
- /**
- * Initializes a texture from a PVRTC buffer
- * @note compatible to cocos2d-iphone interface.
- * @param {Array} data
- * @param {Number} level
- * @param {Number} bpp
- * @param {Boolean} hasAlpha
- * @param {Number} length
- * @param {Number} pixelFormat
- * @return {Boolean}
- */
- initWithPVRTCData:function (data, level, bpp, hasAlpha, length, pixelFormat) {
- if (!(cc.Configuration.getInstance().supportsPVRTC())) {
- cc.log("cocos2d: WARNING: PVRTC images is not supported.");
- return false;
- }
- return true;
- },
- /**
- * sets the min filter, mag filter, wrap s and wrap t texture parameters. <br/>
- * If the texture size is NPOT (non power of 2), then in can only use gl.CLAMP_TO_EDGE in gl.TEXTURE_WRAP_{S,T}.
- * @param texParams
- */
- setTexParameters:function (texParams) {
- var gl = cc.renderContext;
- cc.Assert((this._pixelsWide == cc.NextPOT(this._pixelsWide) && this._pixelsHigh == cc.NextPOT(this._pixelsHigh)) ||
- (texParams.wrapS == gl.CLAMP_TO_EDGE && texParams.wrapT == gl.CLAMP_TO_EDGE),
- "WebGLRenderingContext.CLAMP_TO_EDGE should be used in NPOT textures");
- cc.glBindTexture2D(this);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texParams.minFilter);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texParams.magFilter);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, texParams.wrapS);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, texParams.wrapT);
- //TODO
- //VolatileTexture::setTexParameters(this, texParams);
- },
- /**
- * sets antialias texture parameters: <br/>
- * - GL_TEXTURE_MIN_FILTER = GL_NEAREST <br/>
- * - GL_TEXTURE_MAG_FILTER = GL_NEAREST
- */
- setAntiAliasTexParameters:function () {
- var gl = cc.renderContext;
- cc.glBindTexture2D(this);
- if (!this._hasMipmaps)
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
- else
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
- //TODO
- /*#if CC_ENABLE_CACHE_TEXTURE_DATA
- ccTexParams texParams = {m_bHasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR,GL_LINEAR,GL_NONE,GL_NONE};
- VolatileTexture::setTexParameters(this, &texParams);
- #endif*/
- },
- /**
- * sets alias texture parameters:
- * GL_TEXTURE_MIN_FILTER = GL_NEAREST
- * GL_TEXTURE_MAG_FILTER = GL_NEAREST
- */
- setAliasTexParameters:function () {
- var gl = cc.renderContext;
- cc.glBindTexture2D(this);
- if (!this._hasMipmaps)
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
- else
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
- //TODO
- /*#if CC_ENABLE_CACHE_TEXTURE_DATA
- ccTexParams texParams = {m_bHasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST,GL_NEAREST,GL_NONE,GL_NONE};
- VolatileTexture::setTexParameters(this, &texParams);
- #endif*/
- },
- /**
- * Generates mipmap images for the texture.<br/>
- * It only works if the texture size is POT (power of 2).
- */
- generateMipmap:function () {
- cc.Assert(this._pixelsWide == cc.NextPOT(this._pixelsWide) && this._pixelsHigh == cc.NextPOT(this._pixelsHigh), "Mimpap texture only works in POT textures");
- cc.glBindTexture2D(this);
- cc.renderContext.generateMipmap(cc.renderContext.TEXTURE_2D);
- this._hasMipmaps = true;
- },
- /**
- * returns the pixel format.
- * @return {String}
- */
- stringForFormat:function () {
- switch (this._pixelFormat) {
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888:
- return "RGBA8888";
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888:
- return "RGB888";
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565:
- return "RGB565";
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444:
- return "RGBA4444";
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1:
- return "RGB5A1";
- case cc.TEXTURE_2D_PIXEL_FORMAT_AI88:
- return "AI88";
- case cc.TEXTURE_2D_PIXEL_FORMAT_A8:
- return "A8";
- case cc.TEXTURE_2D_PIXEL_FORMAT_I8:
- return "I8";
- case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4:
- return "PVRTC4";
- case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2:
- return "PVRTC2";
- default:
- cc.log("stringForFormat: " + this._pixelFormat + ", cannot give useful result, it's a unrecognized pixel format");
- break;
- }
- return "";
- },
- /**
- * returns the bits-per-pixel of the in-memory OpenGL texture
- * @return {Number}
- */
- bitsPerPixelForFormat:function (format) {
- format = format || this._pixelFormat;
- switch (format) {
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888:
- return 32;
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888:
- return 32;
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565:
- return 16;
- case cc.TEXTURE_2D_PIXEL_FORMAT_A8:
- return 8;
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444:
- return 16;
- case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1:
- return 16;
- case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4:
- return 4;
- case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2:
- return 2;
- case cc.TEXTURE_2D_PIXEL_FORMAT_I8:
- return 8;
- case cc.TEXTURE_2D_PIXEL_FORMAT_AI88:
- return 16;
- default:
- cc.log("bitsPerPixelForFormat: " + this._pixelFormat + ", cannot give useful result, it's a illegal pixel format");
- return -1;
- }
- },
- _initPremultipliedATextureWithImage:function (uiImage, width, height) {
- var tempData = uiImage.getData();
- var inPixel32 = null;
- var inPixel8 = null;
- var outPixel16 = null;
- var hasAlpha = uiImage.hasAlpha();
- var imageSize = cc.size(uiImage.getWidth(), uiImage.getHeight());
- var pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT;
- var bpp = uiImage.getBitsPerComponent();
- var i;
- // compute pixel format
- if (!hasAlpha) {
- if (bpp >= 8) {
- pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGB888;
- } else {
- cc.log("cocos2d: cc.Texture2D: Using RGB565 texture since image has no alpha");
- pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGB565;
- }
- }
- // Repack the pixel data into the right format
- var length = width * height;
- if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB565) {
- if (hasAlpha) {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
- tempData = new Uint16Array(width * height);
- inPixel32 = uiImage.getData();
- for (i = 0; i < length; ++i) {
- tempData[i] =
- ((((inPixel32[i] >> 0) & 0xFF) >> 3) << 11) | // R
- ((((inPixel32[i] >> 8) & 0xFF) >> 2) << 5) | // G
- ((((inPixel32[i] >> 16) & 0xFF) >> 3) << 0); // B
- }
- } else {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB"
- tempData = new Uint16Array(width * height);
- inPixel8 = uiImage.getData();
- for (i = 0; i < length; ++i) {
- tempData[i] =
- (((inPixel8[i] & 0xFF) >> 3) << 11) | // R
- (((inPixel8[i] & 0xFF) >> 2) << 5) | // G
- (((inPixel8[i] & 0xFF) >> 3) << 0); // B
- }
- }
- } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444) {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA"
- tempData = new Uint16Array(width * height);
- inPixel32 = uiImage.getData();
- for (i = 0; i < length; ++i) {
- tempData[i] =
- ((((inPixel32[i] >> 0) & 0xFF) >> 4) << 12) | // R
- ((((inPixel32[i] >> 8) & 0xFF) >> 4) << 8) | // G
- ((((inPixel32[i] >> 16) & 0xFF) >> 4) << 4) | // B
- ((((inPixel32[i] >> 24) & 0xFF) >> 4) << 0); // A
- }
- } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1) {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGBBBBBA"
- tempData = new Uint16Array(width * height);
- inPixel32 = uiImage.getData();
- for (i = 0; i < length; ++i) {
- tempData[i] =
- ((((inPixel32[i] >> 0) & 0xFF) >> 3) << 11) | // R
- ((((inPixel32[i] >> 8) & 0xFF) >> 3) << 6) | // G
- ((((inPixel32[i] >> 16) & 0xFF) >> 3) << 1) | // B
- ((((inPixel32[i] >> 24) & 0xFF) >> 7) << 0); // A
- }
- } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_A8) {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "AAAAAAAA"
- tempData = new Uint8Array(width * height);
- inPixel32 = uiImage.getData();
- for (i = 0; i < length; ++i) {
- tempData[i] = (inPixel32 >> 24) & 0xFF; // A
- }
- }
- if (hasAlpha && pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB888) {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRRRRGGGGGGGGBBBBBBBB"
- inPixel32 = uiImage.getData();
- tempData = new Uint8Array(width * height * 3);
- for (i = 0; i < length; ++i) {
- tempData[i * 3] = (inPixel32 >> 0) & 0xFF; // R
- tempData[i * 3 + 1] = (inPixel32 >> 8) & 0xFF; // G
- tempData[i * 3 + 2] = (inPixel32 >> 16) & 0xFF; // B
- }
- }
- this.initWithData(tempData, pixelFormat, width, height, imageSize);
- if (tempData != uiImage.getData())
- tempData = null;
- this._hasPremultipliedAlpha = uiImage.isPremultipliedAlpha();
- return true;
- },
- addLoadedEventListener: function (callback, target) {
- if(!this._loadedEventListeners)
- this._loadedEventListeners = [];
- this._loadedEventListeners.push({eventCallback: callback, eventTarget: target});
- },
- removeLoadedEventListener:function(target){
- if(!this._loadedEventListeners)
- return;
- var locListeners = this._loadedEventListeners;
- for(var i = 0; i < locListeners.length; i++){
- var selCallback = locListeners[i];
- if(selCallback.eventTarget == target){
- locListeners.splice(i, 1);
- }
- }
- },
- _callLoadedEventCallbacks: function () {
- if(!this._loadedEventListeners)
- return;
- var locListeners = this._loadedEventListeners;
- for (var i = 0, len = locListeners.length; i < len; i++) {
- var selCallback = locListeners[i];
- cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
- }
- locListeners.length = 0;
- }
- });
- /**
- * <p>
- * This class allows to easily create Canvas 2D textures from images, text or raw data. <br/>
- * The created cc.Texture2D object will always have power-of-two dimensions. <br/>
- * Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions <br/>
- * i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0). <br/>
- * Be aware that the content of the generated textures will be upside-down! </p>
- * @class
- * @extends cc.Class
- */
- cc.Texture2DCanvas = cc.Class.extend(/** @lends cc.Texture2D# */{
- _contentSize:null,
- _isLoaded:false,
- _htmlElementObj:null,
- _loadedEventListeners:null,
- /*public:*/
- ctor:function () {
- this._contentSize = cc.size(0,0);
- this._isLoaded = false;
- this._htmlElementObj = null;
- },
- /**
- * width in pixels
- * @return {Number}
- */
- getPixelsWide:function () {
- return this._contentSize.width;
- },
- /**
- * hight in pixels
- * @return {Number}
- */
- getPixelsHigh:function () {
- return this._contentSize.height;
- },
- /**
- * content size
- * @return {cc.Size}
- */
- getContentSize:function () {
- var locScaleFactor = cc.CONTENT_SCALE_FACTOR();
- return cc.size(this._contentSize.width / locScaleFactor, this._contentSize.height / locScaleFactor);
- },
- getContentSizeInPixels:function () {
- return cc.size(this._contentSize);
- },
- initWithElement:function (element) {
- if (!element)
- return;
- this._htmlElementObj = element;
- },
- /**
- * HTMLElement Object getter
- * @return {HTMLElement}
- */
- getHtmlElementObj:function(){
- return this._htmlElementObj;
- },
- isLoaded:function () {
- return this._isLoaded;
- },
- handleLoadedTexture:function () {
- this._isLoaded = true;
- var locElement = this._htmlElementObj;
- this._contentSize.width = locElement.width;
- this._contentSize.height = locElement.height;
- this._callLoadedEventCallbacks();
- },
- description:function () {
- return "<cc.Texture2D | width = " + this._contentSize.width + " height " + this._contentSize.height+">";
- },
- /**
- * Initializes with a texture2d with data
- * @param {Array} data
- * @param {Number} pixelFormat
- * @param {Number} pixelsWide
- * @param {Number} pixelsHigh
- * @param {cc.Size} contentSize
- * @return {Boolean}
- */
- initWithData:function (data, pixelFormat, pixelsWide, pixelsHigh, contentSize) {
- //support only in WebGl rendering mode
- return false;
- },
- /**
- Extensions to make it easy to create a CCTexture2D object from an image file.
- Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA).
- */
- /**
- * Initializes a texture from a UIImage object
- * @param uiImage
- * @return {Boolean}
- */
- initWithImage:function (uiImage) {
- //support only in WebGl rendering mode
- return false;
- },
- /**
- Extensions to make it easy to create a cc.Texture2D object from a string of text.
- Note that the generated textures are of type A8 - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA).
- */
- /**
- * Initializes a texture from a string with dimensions, alignment, font name and font size (note: initWithString does not support on HTML5)
- * @param {String} text
- * @param {String | cc.FontDefinition} fontName or fontDefinition
- * @param {Number} fontSize
- * @param {cc.Size} dimensions
- * @param {Number} hAlignment
- * @param {Number} vAlignment
- * @return {Boolean}
- */
- initWithString:function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) {
- //support only in WebGl rendering mode
- return false;
- },
- releaseTexture:function () {
- //support only in WebGl rendering mode
- },
- /**
- * get WebGLTexture Object
- * @return {WebGLTexture}
- */
- getName:function () {
- //support only in WebGl rendering mode
- return null;
- },
- /** texture max S */
- getMaxS:function () {
- //support only in WebGl rendering mode
- return 1;
- },
- setMaxS:function (maxS) {
- //support only in WebGl rendering mode
- },
- /** texture max T */
- getMaxT:function () {
- return 1;
- },
- setMaxT:function (maxT) {
- //support only in WebGl rendering mode
- },
- /**
- * return shader program used by drawAtPoint and drawInRect
- * @return {cc.GLProgram}
- */
- getShaderProgram:function () {
- //support only in WebGl rendering mode
- return null;
- },
- /**
- * set shader program used by drawAtPoint and drawInRect
- * @param {cc.GLProgram} shaderProgram
- */
- setShaderProgram:function (shaderProgram) {
- //support only in WebGl rendering mode
- },
- /**
- * whether or not the texture has their Alpha premultiplied
- * @return {Boolean}
- */
- hasPremultipliedAlpha:function () {
- //support only in WebGl rendering mode
- return false;
- },
- hasMipmaps:function () {
- //support only in WebGl rendering mode
- return false;
- },
- /**
- * These functions are needed to create mutable textures
- * @param {Array} data
- */
- releaseData:function (data) {
- //support only in WebGl rendering mode
- data = null;
- },
- keepData:function (data, length) {
- //support only in WebGl rendering mode
- return data;
- },
- /**
- Drawing extensions to make it easy to draw basic quads using a CCTexture2D object.
- These functions require gl.TEXTURE_2D and both gl.VERTEX_ARRAY and gl.TEXTURE_COORD_ARRAY client states to be enabled.
- */
- /**
- * draws a texture at a given point
- * @param {cc.Point} point
- */
- drawAtPoint:function (point) {
- //support only in WebGl rendering mode
- },
- /**
- * draws a texture inside a rect
- * @param {cc.Rect} rect
- */
- drawInRect:function (rect) {
- //support only in WebGl rendering mode
- },
- /**
- * Initializes a texture from a ETC file (note: initWithETCFile does not support on HTML5)
- * @note Compatible to Cocos2d-x
- * @param {String} file
- * @return {Boolean}
- */
- initWithETCFile:function (file) {
- //support only in WebGl rendering mode
- return false;
- },
- /**
- * Initializes a texture from a PVR file
- * @param {String} file
- * @return {Boolean}
- */
- initWithPVRFile:function (file) {
- //support only in WebGl rendering mode
- return false;
- },
- /**
- Extensions to make it easy to create a cc.Texture2D object from a PVRTC file
- Note that the generated textures don't have their alpha premultiplied - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA).
- */
- /**
- * Initializes a texture from a PVRTC buffer
- * @note compatible to cocos2d-iphone interface.
- * @param {Array} data
- * @param {Number} level
- * @param {Number} bpp
- * @param {Boolean} hasAlpha
- * @param {Number} length
- * @param {Number} pixelFormat
- * @return {Boolean}
- */
- initWithPVRTCData:function (data, level, bpp, hasAlpha, length, pixelFormat) {
- //support only in WebGl rendering mode
- return true;
- },
- /**
- * sets the min filter, mag filter, wrap s and wrap t texture parameters. <br/>
- * If the texture size is NPOT (non power of 2), then in can only use gl.CLAMP_TO_EDGE in gl.TEXTURE_WRAP_{S,T}.
- * @param texParams
- */
- setTexParameters:function (texParams) {
- //support only in WebGl rendering mode
- },
- /**
- * sets antialias texture parameters: <br/>
- * - GL_TEXTURE_MIN_FILTER = GL_NEAREST <br/>
- * - GL_TEXTURE_MAG_FILTER = GL_NEAREST
- */
- setAntiAliasTexParameters:function () {
- //support only in WebGl rendering mode
- },
- /**
- * sets alias texture parameters:
- * GL_TEXTURE_MIN_FILTER = GL_NEAREST
- * GL_TEXTURE_MAG_FILTER = GL_NEAREST
- */
- setAliasTexParameters:function () {
- //support only in WebGl rendering mode
- },
- /**
- * Generates mipmap images for the texture.<br/>
- * It only works if the texture size is POT (power of 2).
- */
- generateMipmap:function () {
- //support only in WebGl rendering mode
- },
- /**
- * returns the pixel format.
- * @return {String}
- */
- stringForFormat:function () {
- //support only in WebGl rendering mode
- return "";
- },
- /**
- * returns the bits-per-pixel of the in-memory OpenGL texture
- * @return {Number}
- */
- bitsPerPixelForFormat:function (format) {
- //support only in WebGl rendering mode
- return -1;
- },
- addLoadedEventListener:function(callback, target){
- if(!this._loadedEventListeners)
- this._loadedEventListeners = [];
- this._loadedEventListeners.push({eventCallback:callback, eventTarget:target});
- },
- removeLoadedEventListener:function(target){
- if(!this._loadedEventListeners)
- return;
- var locListeners = this._loadedEventListeners;
- for(var i = 0; i < locListeners.length; i++){
- var selCallback = locListeners[i];
- if(selCallback.eventTarget == target){
- locListeners.splice(i, 1);
- }
- }
- },
- _callLoadedEventCallbacks:function(){
- if(!this._loadedEventListeners)
- return;
- var locListeners = this._loadedEventListeners;
- for(var i = 0, len = locListeners.length; i < len; i++){
- var selCallback = locListeners[i];
- cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
- }
- locListeners.length = 0;
- }
- });
- cc.Texture2D = cc.Browser.supportWebGL ? cc.Texture2DWebGL : cc.Texture2DCanvas;
- /**
- * <p>
- * sets the default pixel format for UIImagescontains alpha channel. <br/>
- * If the UIImage contains alpha channel, then the options are: <br/>
- * - generate 32-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888 (default one) <br/>
- * - generate 24-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB888 <br/>
- * - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444 <br/>
- * - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1 <br/>
- * - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB565 <br/>
- * - generate 8-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_A8 (only use it if you use just 1 color) <br/>
- * <br/>
- * How does it work ? <br/>
- * - If the image is an RGBA (with Alpha) then the default pixel format will be used (it can be a 8-bit, 16-bit or 32-bit texture) <br/>
- * - If the image is an RGB (without Alpha) then an RGB565 or RGB888 texture will be used (16-bit texture) <br/>
- * </p>
- * @param {Number} format
- */
- cc.Texture2D.setDefaultAlphaPixelFormat = function (format) {
- cc._defaultAlphaPixelFormat = format;
- };
- /**
- * returns the alpha pixel format
- * @return {Number}
- */
- cc.Texture2D.defaultAlphaPixelFormat = function () {
- return cc._defaultAlphaPixelFormat;
- };
- cc.Texture2D.getDefaultAlphaPixelFormat = function () {
- return cc._defaultAlphaPixelFormat;
- };
- /**
- * <p>
- * treats (or not) PVR files as if they have alpha premultiplied. <br/>
- * Since it is impossible to know at runtime if the PVR images have the alpha channel premultiplied, it is <br/>
- * possible load them as if they have (or not) the alpha channel premultiplied. <br/>
- * <br/>
- * By default it is disabled. <br/>
- * </p>
- * @param haveAlphaPremultiplied
- * @constructor
- */
- cc.Texture2D.PVRImagesHavePremultipliedAlpha = function (haveAlphaPremultiplied) {
- cc.PVRHaveAlphaPremultiplied_ = haveAlphaPremultiplied;
- };
|