1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 //CONSTANTS: 28 29 //----------------------Possible texture pixel formats---------------------------- 30 /** 31 * 32-bit texture: RGBA8888 32 * @constant 33 * @type {Number} 34 */ 35 cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888 = 0; 36 37 /** 38 * 24-bit texture: RGBA888 39 * @constant 40 * @type {Number} 41 */ 42 cc.TEXTURE_2D_PIXEL_FORMAT_RGB888 = 1; 43 44 /** 45 * 16-bit texture without Alpha channel 46 * @constant 47 * @type {Number} 48 */ 49 cc.TEXTURE_2D_PIXEL_FORMAT_RGB565 = 2; 50 51 /** 52 * 8-bit textures used as masks 53 * @constant 54 * @type {Number} 55 */ 56 cc.TEXTURE_2D_PIXEL_FORMAT_A8 = 3; 57 58 /** 59 * 8-bit intensity texture 60 * @constant 61 * @type {Number} 62 */ 63 cc.TEXTURE_2D_PIXEL_FORMAT_I8 = 4; 64 65 /** 66 * 16-bit textures used as masks 67 * @constant 68 * @type {Number} 69 */ 70 cc.TEXTURE_2D_PIXEL_FORMAT_AI88 = 5; 71 72 /** 73 * 16-bit textures: RGBA4444 74 * @constant 75 * @type {Number} 76 */ 77 cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444 = 6; 78 79 /** 80 * 16-bit textures: RGB5A1 81 * @constant 82 * @type {Number} 83 */ 84 cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1 = 7; 85 86 /** 87 * 4-bit PVRTC-compressed texture: PVRTC4 88 * @constant 89 * @type {Number} 90 */ 91 cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4 = 8; 92 93 /** 94 * 2-bit PVRTC-compressed texture: PVRTC2 95 * @constant 96 * @type {Number} 97 */ 98 cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2 = 9; 99 100 /** 101 * Default texture format: RGBA8888 102 * @constant 103 * @type {Number} 104 */ 105 cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT = cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888; 106 107 // If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit) 108 // Default is: RGBA8888 (32-bit textures) 109 cc._defaultAlphaPixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT; 110 111 // By default PVR images are treated as if they don't have the alpha channel premultiplied 112 cc.PVRHaveAlphaPremultiplied_ = false; 113 114 /** 115 Extension to set the Min / Mag filter 116 */ 117 cc._texParams = function (minFilter, magFilter, wrapS, wrapT) { 118 this.minFilter = minFilter || 0; 119 this.magFilter = magFilter || 0; 120 this.wrapS = wrapS || 0; 121 this.wrapT = wrapT || 0; 122 }; 123 124 /** 125 * <p> 126 * This class allows to easily create OpenGL 2D textures from images, text or raw data. <br/> 127 * The created cc.Texture2D object will always have power-of-two dimensions. <br/> 128 * Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions <br/> 129 * i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0). <br/> 130 * Be aware that the content of the generated textures will be upside-down! </p> 131 * @class 132 * @extends cc.Class 133 */ 134 cc.Texture2DWebGL = cc.Class.extend(/** @lends cc.Texture2D# */{ 135 // By default PVR images are treated as if they don't have the alpha channel premultiplied 136 _pVRHaveAlphaPremultiplied:null, 137 _pixelFormat:null, 138 _pixelsWide:null, 139 _pixelsHigh:null, 140 _name:null, 141 _contentSize:null, 142 _maxS:null, 143 _maxT:null, 144 _hasPremultipliedAlpha:null, 145 _hasMipmaps:false, 146 147 _shaderProgram:null, 148 149 _isLoaded:false, 150 _htmlElementObj:null, 151 _webTextureObj:null, 152 153 _loadedEventListeners:null, 154 155 /*public:*/ 156 ctor:function () { 157 this._pixelsWide = 0; 158 this._pixelsHigh = 0; 159 this._name = ""; 160 this._maxS = 0; 161 this._maxT = 0; 162 this._hasPremultipliedAlpha = false; 163 this._contentSize = cc.size(0, 0); 164 165 this._hasMipmaps = false; 166 this._pVRHaveAlphaPremultiplied = true; 167 this._pixelFormat = cc.Texture2D.defaultAlphaPixelFormat(); 168 169 this._shaderProgram = null; 170 this._isLoaded = false; 171 this._htmlElementObj = null; 172 this._webTextureObj = null; 173 this._loadedEventListeners = []; 174 }, 175 176 releaseTexture:function () { 177 if (this._webTextureObj) 178 cc.renderContext.deleteTexture(this._webTextureObj); 179 }, 180 181 /** 182 * pixel format of the texture 183 * @return {Number} 184 */ 185 getPixelFormat:function () { 186 return this._pixelFormat; 187 }, 188 189 /** 190 * width in pixels 191 * @return {Number} 192 */ 193 getPixelsWide:function () { 194 return this._pixelsWide; 195 }, 196 197 /** 198 * hight in pixels 199 * @return {Number} 200 */ 201 getPixelsHigh:function () { 202 return this._pixelsHigh; 203 }, 204 205 /** 206 * get WebGLTexture Object 207 * @return {WebGLTexture} 208 */ 209 getName:function () { 210 return this._webTextureObj; 211 }, 212 213 /** 214 * content size 215 * @return {cc.Size} 216 */ 217 getContentSize:function () { 218 return cc.size(this._contentSize.width / cc.CONTENT_SCALE_FACTOR(), this._contentSize.height / cc.CONTENT_SCALE_FACTOR()); 219 }, 220 221 getContentSizeInPixels:function () { 222 return this._contentSize; 223 }, 224 225 /** texture max S */ 226 getMaxS:function () { 227 return this._maxS; 228 }, 229 230 setMaxS:function (maxS) { 231 this._maxS = maxS; 232 }, 233 234 /** texture max T */ 235 getMaxT:function () { 236 return this._maxT; 237 }, 238 239 setMaxT:function (maxT) { 240 this._maxT = maxT; 241 }, 242 243 /** 244 * return shader program used by drawAtPoint and drawInRect 245 * @return {cc.GLProgram} 246 */ 247 getShaderProgram:function () { 248 return this._shaderProgram; 249 }, 250 251 /** 252 * set shader program used by drawAtPoint and drawInRect 253 * @param {cc.GLProgram} shaderProgram 254 */ 255 setShaderProgram:function (shaderProgram) { 256 this._shaderProgram = shaderProgram; 257 }, 258 259 /** 260 * whether or not the texture has their Alpha premultiplied 261 * @return {Boolean} 262 */ 263 hasPremultipliedAlpha:function () { 264 return this._hasPremultipliedAlpha; 265 }, 266 267 hasMipmaps:function () { 268 return this._hasMipmaps; 269 }, 270 271 description:function () { 272 return "<cc.Texture2D | Name = " + this._name + " | Dimensions = " + this._pixelsWide + " x " + this._pixelsHigh 273 + " | Coordinates = (" + this._maxS + ", " + this._maxT + ")>"; 274 }, 275 276 /** 277 * These functions are needed to create mutable textures 278 * @param {Array} data 279 */ 280 releaseData:function (data) { 281 data = null; 282 }, 283 284 keepData:function (data, length) { 285 //The texture data mustn't be saved becuase it isn't a mutable texture. 286 return data; 287 }, 288 289 /** 290 * Intializes with a texture2d with data 291 * @param {Array} data 292 * @param {Number} pixelFormat 293 * @param {Number} pixelsWide 294 * @param {Number} pixelsHigh 295 * @param {cc.Size} contentSize 296 * @return {Boolean} 297 */ 298 initWithData:function (data, pixelFormat, pixelsWide, pixelsHigh, contentSize) { 299 var gl = cc.renderContext; 300 301 var bitsPerPixel = 0; 302 //Hack: bitsPerPixelForFormat returns wrong number for RGB_888 textures. See function. 303 if (pixelFormat === cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888) { 304 bitsPerPixel = 24; 305 } else { 306 bitsPerPixel = this.bitsPerPixelForFormat(pixelFormat); 307 } 308 309 var bytesPerRow = pixelsWide * bitsPerPixel / 8; 310 if (bytesPerRow % 8 === 0) { 311 gl.pixelStorei(gl.UNPACK_ALIGNMENT, 8); 312 } else if (bytesPerRow % 4 === 0) { 313 gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4); 314 } else if (bytesPerRow % 2 === 0) { 315 gl.pixelStorei(gl.UNPACK_ALIGNMENT, 2); 316 } else { 317 gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1); 318 } 319 320 this._webTextureObj = gl.createTexture(); 321 gl.bindTexture(gl.TEXTURE_2D, this._webTextureObj); 322 323 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 324 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); 325 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 326 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); 327 328 // Specify OpenGL texture image 329 switch (pixelFormat) { 330 case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888: 331 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_BYTE, data); 332 break; 333 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888: 334 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, pixelsWide, pixelsHigh, 0, gl.RGB, gl.UNSIGNED_BYTE, data); 335 break; 336 case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444: 337 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_SHORT_4_4_4_4, data); 338 break; 339 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1: 340 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_SHORT_5_5_5_1, data); 341 break; 342 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565: 343 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, pixelsWide, pixelsHigh, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, data); 344 break; 345 case cc.TEXTURE_2D_PIXEL_FORMAT_AI88: 346 gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE_ALPHA, pixelsWide, pixelsHigh, 0, gl.LUMINANCE_ALPHA, gl.UNSIGNED_BYTE, data); 347 break; 348 case cc.TEXTURE_2D_PIXEL_FORMAT_A8: 349 gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, pixelsWide, pixelsHigh, 0, gl.ALPHA, gl.UNSIGNED_BYTE, data); 350 break; 351 case cc.TEXTURE_2D_PIXEL_FORMAT_I8: 352 gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, pixelsWide, pixelsHigh, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, data); 353 break; 354 default: 355 throw "NSInternalInconsistencyException"; 356 break; 357 } 358 359 360 this._contentSize = contentSize; 361 this._pixelsWide = pixelsWide; 362 this._pixelsHigh = pixelsHigh; 363 this._pixelFormat = pixelFormat; 364 this._maxS = contentSize.width / pixelsWide; 365 this._maxT = contentSize.height / pixelsHigh; 366 367 this._hasPremultipliedAlpha = false; 368 this._hasMipmaps = false; 369 this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURE)); 370 371 this._isLoaded = true; 372 373 return true; 374 }, 375 376 /** 377 Drawing extensions to make it easy to draw basic quads using a CCTexture2D object. 378 These functions require gl.TEXTURE_2D and both gl.VERTEX_ARRAY and gl.TEXTURE_COORD_ARRAY client states to be enabled. 379 */ 380 381 /** 382 * draws a texture at a given point 383 * @param {cc.Point} point 384 */ 385 drawAtPoint:function (point) { 386 var coordinates = [ 387 0.0, this._maxT, 388 this._maxS, this._maxT, 389 0.0, 0.0, 390 this._maxS, 0.0 ]; 391 392 var width = this._pixelsWide * this._maxS, 393 height = this._pixelsHigh * this._maxT; 394 395 var vertices = [ 396 point.x, point.y, 0.0, 397 width + point.x, point.y, 0.0, 398 point.x, height + point.y, 0.0, 399 width + point.x, height + point.y, 0.0 ]; 400 401 cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEXCOORDS); 402 this._shaderProgram.use(); 403 this._shaderProgram.setUniformsForBuiltins(); 404 405 cc.glBindTexture2D(this); 406 407 var gl = cc.renderContext; 408 gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, vertices); 409 gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, coordinates); 410 411 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); 412 }, 413 414 /** 415 * draws a texture inside a rect 416 * @param {cc.Rect} rect 417 */ 418 drawInRect:function (rect) { 419 var coordinates = [ 420 0.0, this._maxT, 421 this._maxS, this._maxT, 422 0.0, 0.0, 423 this._maxS, 0.0]; 424 425 var vertices = [ rect.x, rect.y, /*0.0,*/ 426 rect.x + rect.width, rect.y, /*0.0,*/ 427 rect.x, rect.y + rect.height, /*0.0,*/ 428 rect.x + rect.width, rect.y + rect.height /*0.0*/ ]; 429 430 cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEXCOORDS); 431 this._shaderProgram.use(); 432 this._shaderProgram.setUniformsForBuiltins(); 433 434 cc.glBindTexture2D(this); 435 436 var gl = cc.renderContext; 437 gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, vertices); 438 gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, coordinates); 439 440 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); 441 }, 442 443 /** 444 Extensions to make it easy to create a CCTexture2D object from an image file. 445 Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA). 446 */ 447 448 /** 449 * Initializes a texture from a UIImage object 450 * @param uiImage 451 * @return {Boolean} 452 */ 453 initWithImage:function (uiImage) { 454 if (uiImage == null) { 455 cc.log("cocos2d: cc.Texture2D. Can't create Texture. UIImage is nil"); 456 return false; 457 } 458 459 var imageWidth = uiImage.getWidth(); 460 var imageHeight = uiImage.getHeight(); 461 462 var conf = cc.Configuration.getInstance(); 463 464 var maxTextureSize = conf.getMaxTextureSize(); 465 if (imageWidth > maxTextureSize || imageHeight > maxTextureSize) { 466 cc.log("cocos2d: WARNING: Image (" + imageWidth + " x " + imageHeight + ") is bigger than the supported " + maxTextureSize + " x " + maxTextureSize); 467 return false; 468 } 469 this._isLoaded = true; 470 471 // always load premultiplied images 472 return this._initPremultipliedATextureWithImage(uiImage, imageWidth, imageHeight); 473 }, 474 475 initWithElement:function (element) { 476 if (!element) 477 return; 478 this._webTextureObj = cc.renderContext.createTexture(); 479 this._htmlElementObj = element; 480 }, 481 482 /** 483 * HTMLElement Object getter 484 * @return {HTMLElement} 485 */ 486 getHtmlElementObj:function(){ 487 return this._htmlElementObj; 488 }, 489 490 isLoaded:function () { 491 return this._isLoaded; 492 }, 493 494 handleLoadedTexture:function () { 495 this._isLoaded = true; 496 //upload image to buffer 497 var gl = cc.renderContext; 498 499 cc.glBindTexture2D(this); 500 501 gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4); 502 503 // Specify OpenGL texture image 504 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._htmlElementObj); 505 506 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 507 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); 508 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 509 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); 510 511 this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURE)); 512 gl.bindTexture(gl.TEXTURE_2D, null); 513 514 var pixelsWide = this._htmlElementObj.width; 515 var pixelsHigh = this._htmlElementObj.height; 516 517 this._contentSize = new cc.Size(pixelsWide, pixelsHigh); 518 this._pixelsWide = pixelsWide; 519 this._pixelsHigh = pixelsHigh; 520 this._pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888; 521 this._maxS = 1; 522 this._maxT = 1; 523 524 this._hasPremultipliedAlpha = false; 525 this._hasMipmaps = false; 526 527 this._callLoadedEventCallbacks(); 528 }, 529 530 /** 531 Extensions to make it easy to create a cc.Texture2D object from a string of text. 532 Note that the generated textures are of type A8 - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA). 533 */ 534 /** 535 * Initializes a texture from a string with dimensions, alignment, font name and font size (note: initWithString does not support on HTML5) 536 * @param {String} text 537 * @param {String | cc.FontDefinition} fontName or fontDefinition 538 * @param {Number} fontSize 539 * @param {cc.Size} dimensions 540 * @param {Number} hAlignment 541 * @param {Number} vAlignment 542 * @return {Boolean} 543 */ 544 initWithString:function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) { 545 if (arguments.length == 3) { 546 fontName = arguments[1]; 547 fontSize = arguments[2]; 548 dimensions = cc.size(0, 0); 549 hAlignment = cc.TEXT_ALIGNMENT_CENTER; 550 vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP; 551 } 552 553 /*if (cc.ENABLE_CACHE_TEXTURE_DATA) { 554 // cache the texture data 555 cc.VolatileTexture.addStringTexture(this, text, dimensions, alignment, fontName, fontSize); 556 }*/ 557 558 var image = new cc.Image(); 559 var eAlign; 560 561 if (cc.VERTICAL_TEXT_ALIGNMENT_TOP === vAlignment) { 562 eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_TOP 563 : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_TOP_LEFT : cc.ALIGN_TOP_RIGHT; 564 } else if (cc.VERTICAL_TEXT_ALIGNMENT_CENTER === vAlignment) { 565 eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_CENTER 566 : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_LEFT : cc.ALIGN_RIGHT; 567 } else if (cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM === vAlignment) { 568 eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_BOTTOM 569 : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_BOTTOM_LEFT : cc.ALIGN_BOTTOM_RIGHT; 570 } else { 571 cc.log("Not supported alignment format!"); 572 return false; 573 } 574 575 if (!image.initWithString(text, dimensions.width, dimensions.height, eAlign, fontName, fontSize)) 576 return false; 577 578 return this.initWithImage(image); 579 }, 580 581 /** 582 * Initializes a texture from a ETC file (note: initWithETCFile does not support on HTML5) 583 * @note Compatible to Cocos2d-x 584 * @param {String} file 585 * @return {Boolean} 586 */ 587 initWithETCFile:function (file) { 588 return false; 589 }, 590 591 /** 592 * Initializes a texture from a PVR file 593 * @param {String} file 594 * @return {Boolean} 595 */ 596 initWithPVRFile:function (file) { 597 var ret = false; 598 // nothing to do with cc.Object.init 599 600 var pvr = new cc.TexturePVR; 601 ret = pvr.initWithContentsOfFile(file); 602 603 if (ret) { 604 pvr.setRetainName(true); // don't dealloc texture on release 605 606 this._name = pvr.getName(); 607 this._maxS = 1.0; 608 this._maxT = 1.0; 609 this._pixelsWide = pvr.getWidth(); 610 this._pixelsHigh = pvr.getHeight(); 611 this._contentSize = cc.size(this._pixelsWide, this._pixelsHigh); 612 this._hasPremultipliedAlpha = cc.PVRHaveAlphaPremultiplied_; 613 this._pixelFormat = pvr.getFormat(); 614 615 this.setAntiAliasTexParameters(); 616 } else { 617 cc.log("cocos2d: Couldn't load PVR image " + file); 618 } 619 return ret; 620 }, 621 622 /** 623 Extensions to make it easy to create a cc.Texture2D object from a PVRTC file 624 Note that the generated textures don't have their alpha premultiplied - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA). 625 */ 626 /** 627 * Initializes a texture from a PVRTC buffer 628 * @note compatible to cocos2d-iphone interface. 629 * @param {Array} data 630 * @param {Number} level 631 * @param {Number} bpp 632 * @param {Boolean} hasAlpha 633 * @param {Number} length 634 * @param {Number} pixelFormat 635 * @return {Boolean} 636 */ 637 initWithPVRTCData:function (data, level, bpp, hasAlpha, length, pixelFormat) { 638 if (!(cc.Configuration.getInstance().supportsPVRTC())) { 639 cc.log("cocos2d: WARNING: PVRTC images is not supported."); 640 return false; 641 } 642 return true; 643 }, 644 645 /** 646 * sets the min filter, mag filter, wrap s and wrap t texture parameters. <br/> 647 * 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}. 648 * @param texParams 649 */ 650 setTexParameters:function (texParams) { 651 var gl = cc.renderContext; 652 653 cc.Assert((this._pixelsWide == cc.NextPOT(this._pixelsWide) && this._pixelsHigh == cc.NextPOT(this._pixelsHigh)) || 654 (texParams.wrapS == gl.CLAMP_TO_EDGE && texParams.wrapT == gl.CLAMP_TO_EDGE), 655 "WebGLRenderingContext.CLAMP_TO_EDGE should be used in NPOT textures"); 656 657 cc.glBindTexture2D(this); 658 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texParams.minFilter); 659 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texParams.magFilter); 660 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, texParams.wrapS); 661 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, texParams.wrapT); 662 663 //TODO 664 //VolatileTexture::setTexParameters(this, texParams); 665 }, 666 667 /** 668 * sets antialias texture parameters: <br/> 669 * - GL_TEXTURE_MIN_FILTER = GL_NEAREST <br/> 670 * - GL_TEXTURE_MAG_FILTER = GL_NEAREST 671 */ 672 setAntiAliasTexParameters:function () { 673 var gl = cc.renderContext; 674 675 cc.glBindTexture2D(this); 676 if (!this._hasMipmaps) 677 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 678 else 679 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST); 680 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 681 //TODO 682 /*#if CC_ENABLE_CACHE_TEXTURE_DATA 683 ccTexParams texParams = {m_bHasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR,GL_LINEAR,GL_NONE,GL_NONE}; 684 VolatileTexture::setTexParameters(this, &texParams); 685 #endif*/ 686 }, 687 688 /** 689 * sets alias texture parameters: 690 * GL_TEXTURE_MIN_FILTER = GL_NEAREST 691 * GL_TEXTURE_MAG_FILTER = GL_NEAREST 692 */ 693 setAliasTexParameters:function () { 694 var gl = cc.renderContext; 695 696 cc.glBindTexture2D(this); 697 if (!this._hasMipmaps) 698 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 699 else 700 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST); 701 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 702 703 //TODO 704 /*#if CC_ENABLE_CACHE_TEXTURE_DATA 705 ccTexParams texParams = {m_bHasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST,GL_NEAREST,GL_NONE,GL_NONE}; 706 VolatileTexture::setTexParameters(this, &texParams); 707 #endif*/ 708 }, 709 710 /** 711 * Generates mipmap images for the texture.<br/> 712 * It only works if the texture size is POT (power of 2). 713 */ 714 generateMipmap:function () { 715 cc.Assert(this._pixelsWide == cc.NextPOT(this._pixelsWide) && this._pixelsHigh == cc.NextPOT(this._pixelsHigh), "Mimpap texture only works in POT textures"); 716 717 cc.glBindTexture2D(this); 718 cc.renderContext.generateMipmap(cc.renderContext.TEXTURE_2D); 719 this._hasMipmaps = true; 720 }, 721 722 /** 723 * returns the pixel format. 724 * @return {String} 725 */ 726 stringForFormat:function () { 727 switch (this._pixelFormat) { 728 case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888: 729 return "RGBA8888"; 730 731 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888: 732 return "RGB888"; 733 734 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565: 735 return "RGB565"; 736 737 case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444: 738 return "RGBA4444"; 739 740 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1: 741 return "RGB5A1"; 742 743 case cc.TEXTURE_2D_PIXEL_FORMAT_AI88: 744 return "AI88"; 745 746 case cc.TEXTURE_2D_PIXEL_FORMAT_A8: 747 return "A8"; 748 749 case cc.TEXTURE_2D_PIXEL_FORMAT_I8: 750 return "I8"; 751 752 case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4: 753 return "PVRTC4"; 754 755 case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2: 756 return "PVRTC2"; 757 758 default: 759 cc.log("stringForFormat: " + this._pixelFormat + ", cannot give useful result, it's a unrecognized pixel format"); 760 break; 761 } 762 return ""; 763 }, 764 765 /** 766 * returns the bits-per-pixel of the in-memory OpenGL texture 767 * @return {Number} 768 */ 769 bitsPerPixelForFormat:function (format) { 770 format = format || this._pixelFormat; 771 switch (format) { 772 case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888: 773 return 32; 774 775 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888: 776 return 32; 777 778 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565: 779 return 16; 780 781 case cc.TEXTURE_2D_PIXEL_FORMAT_A8: 782 return 8; 783 784 case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444: 785 return 16; 786 787 case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1: 788 return 16; 789 790 case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4: 791 return 4; 792 793 case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2: 794 return 2; 795 796 case cc.TEXTURE_2D_PIXEL_FORMAT_I8: 797 return 8; 798 799 case cc.TEXTURE_2D_PIXEL_FORMAT_AI88: 800 return 16; 801 802 default: 803 cc.log("bitsPerPixelForFormat: " + this._pixelFormat + ", cannot give useful result, it's a illegal pixel format"); 804 return -1; 805 } 806 }, 807 808 _initPremultipliedATextureWithImage:function (uiImage, width, height) { 809 var tempData = uiImage.getData(); 810 var inPixel32 = null; 811 var inPixel8 = null; 812 var outPixel16 = null; 813 var hasAlpha = uiImage.hasAlpha(); 814 var imageSize = cc.size(uiImage.getWidth(), uiImage.getHeight()); 815 var pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT; 816 var bpp = uiImage.getBitsPerComponent(); 817 var i; 818 819 // compute pixel format 820 if (!hasAlpha) { 821 if (bpp >= 8) { 822 pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGB888; 823 } else { 824 cc.log("cocos2d: cc.Texture2D: Using RGB565 texture since image has no alpha"); 825 pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGB565; 826 } 827 } 828 829 // Repack the pixel data into the right format 830 var length = width * height; 831 832 if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB565) { 833 if (hasAlpha) { 834 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB" 835 tempData = new Uint16Array(width * height); 836 inPixel32 = uiImage.getData(); 837 838 for (i = 0; i < length; ++i) { 839 tempData[i] = 840 ((((inPixel32[i] >> 0) & 0xFF) >> 3) << 11) | // R 841 ((((inPixel32[i] >> 8) & 0xFF) >> 2) << 5) | // G 842 ((((inPixel32[i] >> 16) & 0xFF) >> 3) << 0); // B 843 } 844 } else { 845 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB" 846 tempData = new Uint16Array(width * height); 847 inPixel8 = uiImage.getData(); 848 849 for (i = 0; i < length; ++i) { 850 tempData[i] = 851 (((inPixel8[i] & 0xFF) >> 3) << 11) | // R 852 (((inPixel8[i] & 0xFF) >> 2) << 5) | // G 853 (((inPixel8[i] & 0xFF) >> 3) << 0); // B 854 } 855 } 856 } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444) { 857 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA" 858 tempData = new Uint16Array(width * height); 859 inPixel32 = uiImage.getData(); 860 861 for (i = 0; i < length; ++i) { 862 tempData[i] = 863 ((((inPixel32[i] >> 0) & 0xFF) >> 4) << 12) | // R 864 ((((inPixel32[i] >> 8) & 0xFF) >> 4) << 8) | // G 865 ((((inPixel32[i] >> 16) & 0xFF) >> 4) << 4) | // B 866 ((((inPixel32[i] >> 24) & 0xFF) >> 4) << 0); // A 867 } 868 } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1) { 869 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGBBBBBA" 870 tempData = new Uint16Array(width * height); 871 inPixel32 = uiImage.getData(); 872 873 for (i = 0; i < length; ++i) { 874 tempData[i] = 875 ((((inPixel32[i] >> 0) & 0xFF) >> 3) << 11) | // R 876 ((((inPixel32[i] >> 8) & 0xFF) >> 3) << 6) | // G 877 ((((inPixel32[i] >> 16) & 0xFF) >> 3) << 1) | // B 878 ((((inPixel32[i] >> 24) & 0xFF) >> 7) << 0); // A 879 } 880 } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_A8) { 881 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "AAAAAAAA" 882 tempData = new Uint8Array(width * height); 883 inPixel32 = uiImage.getData(); 884 885 for (i = 0; i < length; ++i) { 886 tempData[i] = (inPixel32 >> 24) & 0xFF; // A 887 } 888 } 889 890 if (hasAlpha && pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB888) { 891 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRRRRGGGGGGGGBBBBBBBB" 892 inPixel32 = uiImage.getData(); 893 tempData = new Uint8Array(width * height * 3); 894 895 for (i = 0; i < length; ++i) { 896 tempData[i * 3] = (inPixel32 >> 0) & 0xFF; // R 897 tempData[i * 3 + 1] = (inPixel32 >> 8) & 0xFF; // G 898 tempData[i * 3 + 2] = (inPixel32 >> 16) & 0xFF; // B 899 } 900 } 901 902 this.initWithData(tempData, pixelFormat, width, height, imageSize); 903 904 if (tempData != uiImage.getData()) 905 tempData = null; 906 907 this._hasPremultipliedAlpha = uiImage.isPremultipliedAlpha(); 908 return true; 909 }, 910 911 addLoadedEventListener: function (callback, target) { 912 this._loadedEventListeners.push({eventCallback: callback, eventTarget: target}); 913 }, 914 915 removeLoadedEventListener:function(target){ 916 var locListeners = this._loadedEventListeners; 917 for(var i = 0; i < locListeners.length; i++){ 918 var selCallback = locListeners[i]; 919 if(selCallback.eventTarget == target){ 920 locListeners.splice(i, 1); 921 } 922 } 923 }, 924 925 _callLoadedEventCallbacks: function () { 926 var locListeners = this._loadedEventListeners; 927 for (var i = 0, len = locListeners.length; i < len; i++) { 928 var selCallback = locListeners[i]; 929 selCallback.eventCallback.call(selCallback.eventTarget, this); 930 } 931 locListeners.length = 0; 932 } 933 }); 934 935 /** 936 * <p> 937 * This class allows to easily create Canvas 2D textures from images, text or raw data. <br/> 938 * The created cc.Texture2D object will always have power-of-two dimensions. <br/> 939 * Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions <br/> 940 * i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0). <br/> 941 * Be aware that the content of the generated textures will be upside-down! </p> 942 * @class 943 * @extends cc.Class 944 */ 945 cc.Texture2DCanvas = cc.Class.extend(/** @lends cc.Texture2D# */{ 946 _contentSize:null, 947 _isLoaded:false, 948 _htmlElementObj:null, 949 950 _loadedEventListeners:null, 951 /*public:*/ 952 ctor:function () { 953 this._contentSize = cc.size(0,0); 954 this._isLoaded = false; 955 this._htmlElementObj = null; 956 this._loadedEventListeners = []; 957 }, 958 959 /** 960 * width in pixels 961 * @return {Number} 962 */ 963 getPixelsWide:function () { 964 return this._contentSize.width; 965 }, 966 967 /** 968 * hight in pixels 969 * @return {Number} 970 */ 971 getPixelsHigh:function () { 972 return this._contentSize.height; 973 }, 974 975 /** 976 * content size 977 * @return {cc.Size} 978 */ 979 getContentSize:function () { 980 return cc.size(this._contentSize.width / cc.CONTENT_SCALE_FACTOR(), this._contentSize.height / cc.CONTENT_SCALE_FACTOR()); 981 }, 982 983 getContentSizeInPixels:function () { 984 return this._contentSize; 985 }, 986 987 initWithElement:function (element) { 988 if (!element) 989 return; 990 this._htmlElementObj = element; 991 }, 992 993 /** 994 * HTMLElement Object getter 995 * @return {HTMLElement} 996 */ 997 getHtmlElementObj:function(){ 998 return this._htmlElementObj; 999 }, 1000 1001 isLoaded:function () { 1002 return this._isLoaded; 1003 }, 1004 1005 handleLoadedTexture:function () { 1006 this._isLoaded = true; 1007 var locElement = this._htmlElementObj; 1008 this._contentSize = new cc.Size(locElement.width, locElement.height); 1009 1010 this._callLoadedEventCallbacks(); 1011 }, 1012 1013 description:function () { 1014 return "<cc.Texture2D | width = " + this._contentSize.width + " height " + this._contentSize.height+">"; 1015 }, 1016 1017 /** 1018 * Initializes with a texture2d with data 1019 * @param {Array} data 1020 * @param {Number} pixelFormat 1021 * @param {Number} pixelsWide 1022 * @param {Number} pixelsHigh 1023 * @param {cc.Size} contentSize 1024 * @return {Boolean} 1025 */ 1026 initWithData:function (data, pixelFormat, pixelsWide, pixelsHigh, contentSize) { 1027 //support only in WebGl rendering mode 1028 return false; 1029 }, 1030 1031 /** 1032 Extensions to make it easy to create a CCTexture2D object from an image file. 1033 Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA). 1034 */ 1035 /** 1036 * Initializes a texture from a UIImage object 1037 * @param uiImage 1038 * @return {Boolean} 1039 */ 1040 initWithImage:function (uiImage) { 1041 //support only in WebGl rendering mode 1042 return false; 1043 }, 1044 1045 /** 1046 Extensions to make it easy to create a cc.Texture2D object from a string of text. 1047 Note that the generated textures are of type A8 - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA). 1048 */ 1049 /** 1050 * Initializes a texture from a string with dimensions, alignment, font name and font size (note: initWithString does not support on HTML5) 1051 * @param {String} text 1052 * @param {String | cc.FontDefinition} fontName or fontDefinition 1053 * @param {Number} fontSize 1054 * @param {cc.Size} dimensions 1055 * @param {Number} hAlignment 1056 * @param {Number} vAlignment 1057 * @return {Boolean} 1058 */ 1059 initWithString:function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) { 1060 //support only in WebGl rendering mode 1061 return false; 1062 }, 1063 1064 releaseTexture:function () { 1065 //support only in WebGl rendering mode 1066 }, 1067 1068 /** 1069 * get WebGLTexture Object 1070 * @return {WebGLTexture} 1071 */ 1072 getName:function () { 1073 //support only in WebGl rendering mode 1074 return null; 1075 }, 1076 1077 /** texture max S */ 1078 getMaxS:function () { 1079 //support only in WebGl rendering mode 1080 return 1; 1081 }, 1082 1083 setMaxS:function (maxS) { 1084 //support only in WebGl rendering mode 1085 }, 1086 1087 /** texture max T */ 1088 getMaxT:function () { 1089 return 1; 1090 }, 1091 1092 setMaxT:function (maxT) { 1093 //support only in WebGl rendering mode 1094 }, 1095 1096 /** 1097 * return shader program used by drawAtPoint and drawInRect 1098 * @return {cc.GLProgram} 1099 */ 1100 getShaderProgram:function () { 1101 //support only in WebGl rendering mode 1102 return null; 1103 }, 1104 1105 /** 1106 * set shader program used by drawAtPoint and drawInRect 1107 * @param {cc.GLProgram} shaderProgram 1108 */ 1109 setShaderProgram:function (shaderProgram) { 1110 //support only in WebGl rendering mode 1111 }, 1112 1113 /** 1114 * whether or not the texture has their Alpha premultiplied 1115 * @return {Boolean} 1116 */ 1117 hasPremultipliedAlpha:function () { 1118 //support only in WebGl rendering mode 1119 return false; 1120 }, 1121 1122 hasMipmaps:function () { 1123 //support only in WebGl rendering mode 1124 return false; 1125 }, 1126 1127 /** 1128 * These functions are needed to create mutable textures 1129 * @param {Array} data 1130 */ 1131 releaseData:function (data) { 1132 //support only in WebGl rendering mode 1133 data = null; 1134 }, 1135 1136 keepData:function (data, length) { 1137 //support only in WebGl rendering mode 1138 return data; 1139 }, 1140 1141 /** 1142 Drawing extensions to make it easy to draw basic quads using a CCTexture2D object. 1143 These functions require gl.TEXTURE_2D and both gl.VERTEX_ARRAY and gl.TEXTURE_COORD_ARRAY client states to be enabled. 1144 */ 1145 1146 /** 1147 * draws a texture at a given point 1148 * @param {cc.Point} point 1149 */ 1150 drawAtPoint:function (point) { 1151 //support only in WebGl rendering mode 1152 }, 1153 1154 /** 1155 * draws a texture inside a rect 1156 * @param {cc.Rect} rect 1157 */ 1158 drawInRect:function (rect) { 1159 //support only in WebGl rendering mode 1160 }, 1161 1162 /** 1163 * Initializes a texture from a ETC file (note: initWithETCFile does not support on HTML5) 1164 * @note Compatible to Cocos2d-x 1165 * @param {String} file 1166 * @return {Boolean} 1167 */ 1168 initWithETCFile:function (file) { 1169 //support only in WebGl rendering mode 1170 return false; 1171 }, 1172 1173 /** 1174 * Initializes a texture from a PVR file 1175 * @param {String} file 1176 * @return {Boolean} 1177 */ 1178 initWithPVRFile:function (file) { 1179 //support only in WebGl rendering mode 1180 return false; 1181 }, 1182 1183 /** 1184 Extensions to make it easy to create a cc.Texture2D object from a PVRTC file 1185 Note that the generated textures don't have their alpha premultiplied - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA). 1186 */ 1187 /** 1188 * Initializes a texture from a PVRTC buffer 1189 * @note compatible to cocos2d-iphone interface. 1190 * @param {Array} data 1191 * @param {Number} level 1192 * @param {Number} bpp 1193 * @param {Boolean} hasAlpha 1194 * @param {Number} length 1195 * @param {Number} pixelFormat 1196 * @return {Boolean} 1197 */ 1198 initWithPVRTCData:function (data, level, bpp, hasAlpha, length, pixelFormat) { 1199 //support only in WebGl rendering mode 1200 return true; 1201 }, 1202 1203 /** 1204 * sets the min filter, mag filter, wrap s and wrap t texture parameters. <br/> 1205 * 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}. 1206 * @param texParams 1207 */ 1208 setTexParameters:function (texParams) { 1209 //support only in WebGl rendering mode 1210 }, 1211 1212 /** 1213 * sets antialias texture parameters: <br/> 1214 * - GL_TEXTURE_MIN_FILTER = GL_NEAREST <br/> 1215 * - GL_TEXTURE_MAG_FILTER = GL_NEAREST 1216 */ 1217 setAntiAliasTexParameters:function () { 1218 //support only in WebGl rendering mode 1219 }, 1220 1221 /** 1222 * sets alias texture parameters: 1223 * GL_TEXTURE_MIN_FILTER = GL_NEAREST 1224 * GL_TEXTURE_MAG_FILTER = GL_NEAREST 1225 */ 1226 setAliasTexParameters:function () { 1227 //support only in WebGl rendering mode 1228 }, 1229 1230 /** 1231 * Generates mipmap images for the texture.<br/> 1232 * It only works if the texture size is POT (power of 2). 1233 */ 1234 generateMipmap:function () { 1235 //support only in WebGl rendering mode 1236 }, 1237 1238 /** 1239 * returns the pixel format. 1240 * @return {String} 1241 */ 1242 stringForFormat:function () { 1243 //support only in WebGl rendering mode 1244 return ""; 1245 }, 1246 1247 /** 1248 * returns the bits-per-pixel of the in-memory OpenGL texture 1249 * @return {Number} 1250 */ 1251 bitsPerPixelForFormat:function (format) { 1252 //support only in WebGl rendering mode 1253 return -1; 1254 }, 1255 1256 addLoadedEventListener:function(callback, target){ 1257 this._loadedEventListeners.push({eventCallback:callback, eventTarget:target}); 1258 }, 1259 1260 removeLoadedEventListener:function(target){ 1261 var locListeners = this._loadedEventListeners; 1262 for(var i = 0; i < locListeners.length; i++){ 1263 var selCallback = locListeners[i]; 1264 if(selCallback.eventTarget == target){ 1265 locListeners.splice(i, 1); 1266 } 1267 } 1268 }, 1269 1270 _callLoadedEventCallbacks:function(){ 1271 var locListeners = this._loadedEventListeners; 1272 for(var i = 0, len = locListeners.length; i < len; i++){ 1273 var selCallback = locListeners[i]; 1274 selCallback.eventCallback.call(selCallback.eventTarget, this); 1275 } 1276 locListeners.length = 0; 1277 } 1278 }); 1279 1280 cc.Texture2D = cc.Browser.supportWebGL ? cc.Texture2DWebGL : cc.Texture2DCanvas; 1281 1282 /** 1283 * <p> 1284 * sets the default pixel format for UIImagescontains alpha channel. <br/> 1285 * If the UIImage contains alpha channel, then the options are: <br/> 1286 * - generate 32-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888 (default one) <br/> 1287 * - generate 24-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB888 <br/> 1288 * - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444 <br/> 1289 * - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1 <br/> 1290 * - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB565 <br/> 1291 * - generate 8-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_A8 (only use it if you use just 1 color) <br/> 1292 * <br/> 1293 * How does it work ? <br/> 1294 * - 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/> 1295 * - If the image is an RGB (without Alpha) then an RGB565 or RGB888 texture will be used (16-bit texture) <br/> 1296 * </p> 1297 * @param {Number} format 1298 */ 1299 cc.Texture2D.setDefaultAlphaPixelFormat = function (format) { 1300 cc._defaultAlphaPixelFormat = format; 1301 }; 1302 1303 /** 1304 * returns the alpha pixel format 1305 * @return {Number} 1306 */ 1307 cc.Texture2D.defaultAlphaPixelFormat = function () { 1308 return cc._defaultAlphaPixelFormat; 1309 }; 1310 1311 cc.Texture2D.getDefaultAlphaPixelFormat = function () { 1312 return cc._defaultAlphaPixelFormat; 1313 }; 1314 1315 /** 1316 * <p> 1317 * treats (or not) PVR files as if they have alpha premultiplied. <br/> 1318 * Since it is impossible to know at runtime if the PVR images have the alpha channel premultiplied, it is <br/> 1319 * possible load them as if they have (or not) the alpha channel premultiplied. <br/> 1320 * <br/> 1321 * By default it is disabled. <br/> 1322 * </p> 1323 * @param haveAlphaPremultiplied 1324 * @constructor 1325 */ 1326 cc.Texture2D.PVRImagesHavePremultipliedAlpha = function (haveAlphaPremultiplied) { 1327 cc.PVRHaveAlphaPremultiplied_ = haveAlphaPremultiplied; 1328 }; 1329