CCTexture2D.js 44 KB

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