CCTextureAtlas.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. /**
  23. * <p>A class that implements a Texture Atlas. <br />
  24. * Supported features: <br />
  25. * The atlas file can be a PNG, JPG. <br />
  26. * Quads can be updated in runtime <br />
  27. * Quads can be added in runtime <br />
  28. * Quads can be removed in runtime <br />
  29. * Quads can be re-ordered in runtime <br />
  30. * The TextureAtlas capacity can be increased or decreased in runtime.</p>
  31. * @class
  32. * @extends cc.Class
  33. */
  34. cc.TextureAtlas = cc.Class.extend(/** @lends cc.TextureAtlas# */{
  35. _indices:null,
  36. //0: vertex 1: indices
  37. _buffersVBO:null,
  38. //indicates whether or not the array buffer of the VBO needs to be updated
  39. _dirty:false,
  40. _capacity:0,
  41. _texture:null,
  42. _quads:null,
  43. _quadsArrayBuffer:null,
  44. _quadsWebBuffer:null,
  45. _quadsReader:null,
  46. ctor:function () {
  47. this._buffersVBO = [];
  48. },
  49. /**
  50. * Quantity of quads that are going to be drawn.
  51. * @return {Number}
  52. */
  53. getTotalQuads:function () {
  54. //return this._quads.length;
  55. return this._totalQuads;
  56. },
  57. /**
  58. * Quantity of quads that can be stored with the current texture atlas size
  59. * @return {Number}
  60. */
  61. getCapacity:function () {
  62. return this._capacity;
  63. },
  64. /**
  65. * Texture of the texture atlas
  66. * @return {Image}
  67. */
  68. getTexture:function () {
  69. return this._texture;
  70. },
  71. /**
  72. * @param {Image} texture
  73. */
  74. setTexture:function (texture) {
  75. this._texture = texture;
  76. },
  77. /**
  78. * specify if the array buffer of the VBO needs to be updated
  79. * @param {Boolean} dirty
  80. */
  81. setDirty:function (dirty) {
  82. this._dirty = dirty;
  83. },
  84. /**
  85. * whether or not the array buffer of the VBO needs to be updated
  86. * @returns {boolean}
  87. */
  88. isDirty:function () {
  89. return this._dirty;
  90. },
  91. /**
  92. * Quads that are going to be rendered
  93. * @return {Array}
  94. */
  95. getQuads:function () {
  96. return this._quads;
  97. },
  98. /**
  99. * @param {Array} quads
  100. */
  101. setQuads:function (quads) {
  102. this._quads = quads;
  103. //TODO need re-binding
  104. },
  105. _copyQuadsToTextureAtlas:function(quads, index){
  106. if(!quads)
  107. return;
  108. for(var i = 0; i < quads.length ; i++)
  109. this._setQuadToArray(quads[i], index + i);
  110. },
  111. _setQuadToArray: function (quad, index) {
  112. var locQuads = this._quads;
  113. if (!locQuads[index]) {
  114. locQuads[index] = new cc.V3F_C4B_T2F_Quad(quad.tl, quad.bl, quad.tr, quad.br, this._quadsArrayBuffer, index * cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT);
  115. return;
  116. }
  117. locQuads[index].bl = quad.bl;
  118. locQuads[index].br = quad.br;
  119. locQuads[index].tl = quad.tl;
  120. locQuads[index].tr = quad.tr;
  121. },
  122. /**
  123. * Description
  124. * @return {String}
  125. */
  126. description:function () {
  127. return '<cc.TextureAtlas | totalQuads =' + this._totalQuads + '>';
  128. },
  129. _setupIndices:function () {
  130. if (this._capacity === 0)
  131. return;
  132. var locIndices = this._indices, locCapacity = this._capacity;
  133. for (var i = 0; i < locCapacity; i++) {
  134. if (cc.TEXTURE_ATLAS_USE_TRIANGLE_STRIP) {
  135. locIndices[i * 6 + 0] = i * 4 + 0;
  136. locIndices[i * 6 + 1] = i * 4 + 0;
  137. locIndices[i * 6 + 2] = i * 4 + 2;
  138. locIndices[i * 6 + 3] = i * 4 + 1;
  139. locIndices[i * 6 + 4] = i * 4 + 3;
  140. locIndices[i * 6 + 5] = i * 4 + 3;
  141. } else {
  142. locIndices[i * 6 + 0] = i * 4 + 0;
  143. locIndices[i * 6 + 1] = i * 4 + 1;
  144. locIndices[i * 6 + 2] = i * 4 + 2;
  145. // inverted index. issue #179
  146. locIndices[i * 6 + 3] = i * 4 + 3;
  147. locIndices[i * 6 + 4] = i * 4 + 2;
  148. locIndices[i * 6 + 5] = i * 4 + 1;
  149. }
  150. }
  151. },
  152. _setupVBO:function () {
  153. var gl = cc.renderContext;
  154. //create WebGLBuffer
  155. this._buffersVBO[0] = gl.createBuffer();
  156. this._buffersVBO[1] = gl.createBuffer();
  157. this._quadsWebBuffer = gl.createBuffer();
  158. this._mapBuffers();
  159. },
  160. _mapBuffers:function () {
  161. var gl = cc.renderContext;
  162. gl.bindBuffer(gl.ARRAY_BUFFER, this._quadsWebBuffer);
  163. gl.bufferData(gl.ARRAY_BUFFER, this._quadsArrayBuffer, gl.DYNAMIC_DRAW);
  164. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._buffersVBO[1]);
  165. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW);
  166. //cc.CHECK_GL_ERROR_DEBUG();
  167. },
  168. /**
  169. * <p>Initializes a TextureAtlas with a filename and with a certain capacity for Quads.<br />
  170. * The TextureAtlas capacity can be increased in runtime.<br />
  171. * WARNING: Do not reinitialize the TextureAtlas because it will leak memory. </p>
  172. * @param {String} file
  173. * @param {Number} capacity
  174. * @return {Boolean}
  175. * @example
  176. * //example
  177. * var textureAtlas = new cc.TextureAtlas();
  178. * textureAtlas.initWithTexture("hello.png", 3);
  179. */
  180. initWithFile:function (file, capacity) {
  181. // retained in property
  182. var texture = cc.TextureCache.getInstance().addImage(file);
  183. if (texture)
  184. return this.initWithTexture(texture, capacity);
  185. else {
  186. cc.log("cocos2d: Could not open file: " + file);
  187. return false;
  188. }
  189. },
  190. /**
  191. * <p>Initializes a TextureAtlas with a previously initialized Texture2D object, and<br />
  192. * with an initial capacity for Quads.<br />
  193. * The TextureAtlas capacity can be increased in runtime.<br />
  194. * WARNING: Do not reinitialize the TextureAtlas because it will leak memory</p>
  195. * @param {Image} texture
  196. * @param {Number} capacity
  197. * @return {Boolean}
  198. * @example
  199. * //example
  200. * var texture = cc.TextureCache.getInstance().addImage("hello.png");
  201. * var textureAtlas = new cc.TextureAtlas();
  202. * textureAtlas.initWithTexture(texture, 3);
  203. */
  204. initWithTexture:function (texture, capacity) {
  205. if(!texture)
  206. throw "cc.TextureAtlas.initWithTexture():texture should be non-null";
  207. capacity = 0 | (capacity);
  208. this._capacity = capacity;
  209. this._totalQuads = 0;
  210. // retained in property
  211. this._texture = texture;
  212. // Re-initialization is not allowed
  213. this._quads = [];
  214. this._indices = new Uint16Array(capacity * 6);
  215. var quadSize = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  216. this._quadsArrayBuffer = new ArrayBuffer(quadSize * capacity);
  217. this._quadsReader = new Uint8Array(this._quadsArrayBuffer);
  218. if (!( this._quads && this._indices) && capacity > 0)
  219. return false;
  220. var locQuads = this._quads;
  221. for(var i = 0; i< capacity; i++)
  222. locQuads[i] =new cc.V3F_C4B_T2F_Quad(null, null, null, null, this._quadsArrayBuffer, i * quadSize);
  223. this._setupIndices();
  224. this._setupVBO();
  225. this._dirty = true;
  226. return true;
  227. },
  228. /**
  229. * <p>Updates a Quad (texture, vertex and color) at a certain index <br />
  230. * index must be between 0 and the atlas capacity - 1 </p>
  231. * @param {cc.V2F_C4B_T2F_Quad} quad
  232. * @param {Number} index
  233. */
  234. updateQuad:function (quad, index) {
  235. if(!quad)
  236. throw "cc.TextureAtlas.updateQuad(): quad should be non-null";
  237. if((index < 0) || (index >= this._capacity))
  238. throw "cc.TextureAtlas.updateQuad(): Invalid index";
  239. this._totalQuads = Math.max(index + 1, this._totalQuads);
  240. this._setQuadToArray(quad, index);
  241. this._dirty = true;
  242. },
  243. /**
  244. * <p>Inserts a Quad (texture, vertex and color) at a certain index<br />
  245. * index must be between 0 and the atlas capacity - 1 </p>
  246. * @param {cc.V2F_C4B_T2F_Quad} quad
  247. * @param {Number} index
  248. */
  249. insertQuad:function (quad, index) {
  250. if(index >= this._capacity)
  251. throw "cc.TextureAtlas.insertQuad(): Invalid index";
  252. this._totalQuads++;
  253. if(this._totalQuads > this._capacity) {
  254. cc.log("cc.TextureAtlas.insertQuad(): invalid totalQuads");
  255. return;
  256. }
  257. var quadSize = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  258. // issue #575. index can be > totalQuads
  259. var remaining = (this._totalQuads-1) - index;
  260. var startOffset = index * quadSize;
  261. var moveLength = remaining * quadSize;
  262. this._quads[this._totalQuads -1] = new cc.V3F_C4B_T2F_Quad(null, null, null, null, this._quadsArrayBuffer, (this._totalQuads -1) * quadSize);
  263. this._quadsReader.set(this._quadsReader.subarray(startOffset, startOffset + moveLength), startOffset + quadSize);
  264. this._setQuadToArray(quad, index);
  265. this._dirty = true;
  266. },
  267. /**
  268. * <p>
  269. * Inserts a c array of quads at a given index <br />
  270. * index must be between 0 and the atlas capacity - 1 <br />
  271. * this method doesn't enlarge the array when amount + index > totalQuads <br />
  272. * </p>
  273. * @param {Array} quads
  274. * @param {Number} index
  275. * @param {Number} amount
  276. */
  277. insertQuads:function (quads, index, amount) {
  278. amount = amount || quads.length;
  279. if((index + amount) > this._capacity)
  280. throw "cc.TextureAtlas.insertQuad(): Invalid index + amount";
  281. var quadSize = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  282. this._totalQuads += amount;
  283. if(this._totalQuads > this._capacity) {
  284. cc.log("cc.TextureAtlas.insertQuad(): invalid totalQuads");
  285. return;
  286. }
  287. // issue #575. index can be > totalQuads
  288. var remaining = (this._totalQuads-1) - index - amount;
  289. var startOffset = index * quadSize;
  290. var moveLength = remaining * quadSize;
  291. var lastIndex = (this._totalQuads-1) - amount;
  292. var i;
  293. for(i = 0; i < amount;i++)
  294. this._quads[lastIndex + i] = new cc.V3F_C4B_T2F_Quad(null, null, null, null, this._quadsArrayBuffer, (this._totalQuads -1) * quadSize);
  295. this._quadsReader.set(this._quadsReader.subarray(startOffset, startOffset + moveLength), startOffset + quadSize * amount);
  296. for(i = 0; i < amount; i++)
  297. this._setQuadToArray(quads[i], index + i);
  298. this._dirty = true;
  299. },
  300. /**
  301. * <p>Removes the quad that is located at a certain index and inserts it at a new index <br />
  302. * This operation is faster than removing and inserting in a quad in 2 different steps</p>
  303. * @param {Number} fromIndex
  304. * @param {Number} newIndex
  305. */
  306. insertQuadFromIndex:function (fromIndex, newIndex) {
  307. if (fromIndex === newIndex)
  308. return;
  309. if(newIndex < 0 && newIndex >= this._totalQuads)
  310. throw "cc.TextureAtlas.insertQuadFromIndex(): Invalid newIndex";
  311. if(fromIndex < 0 && fromIndex >= this._totalQuads)
  312. throw "cc.TextureAtlas.insertQuadFromIndex(): Invalid fromIndex";
  313. var quadSize = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  314. var locQuadsReader = this._quadsReader;
  315. var sourceArr = locQuadsReader.subarray(fromIndex * quadSize,quadSize);
  316. var startOffset, moveLength;
  317. if(fromIndex > newIndex){
  318. startOffset = newIndex * quadSize;
  319. moveLength = (fromIndex - newIndex) * quadSize;
  320. locQuadsReader.set(locQuadsReader.subarray(startOffset, startOffset + moveLength),startOffset + quadSize);
  321. locQuadsReader.set(sourceArr,startOffset);
  322. }else{
  323. startOffset = (fromIndex + 1) * quadSize;
  324. moveLength = (newIndex - fromIndex) * quadSize;
  325. locQuadsReader.set(locQuadsReader.subarray(startOffset, startOffset + moveLength),startOffset - quadSize);
  326. locQuadsReader.set(sourceArr, newIndex * quadSize);
  327. }
  328. this._dirty = true;
  329. },
  330. /**
  331. * <p>Removes a quad at a given index number.<br />
  332. * The capacity remains the same, but the total number of quads to be drawn is reduced in 1 </p>
  333. * @param {Number} index
  334. */
  335. removeQuadAtIndex:function (index) {
  336. if(index >= this._totalQuads)
  337. throw "cc.TextureAtlas.removeQuadAtIndex(): Invalid index";
  338. var quadSize = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  339. this._totalQuads--;
  340. this._quads.length = this._totalQuads;
  341. if(index !== this._totalQuads){
  342. //move data
  343. var startOffset = (index + 1) * quadSize;
  344. var moveLength = (this._totalQuads - index) * quadSize;
  345. this._quadsReader.set(this._quadsReader.subarray(startOffset, startOffset + moveLength), startOffset - quadSize);
  346. }
  347. this._dirty = true;
  348. },
  349. removeQuadsAtIndex:function (index, amount) {
  350. if(index + amount > this._totalQuads)
  351. throw "cc.TextureAtlas.removeQuadsAtIndex(): index + amount out of bounds";
  352. this._totalQuads -= amount;
  353. if(index !== this._totalQuads){
  354. //move data
  355. var quadSize = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  356. var srcOffset = (index + amount) * quadSize;
  357. var moveLength = (this._totalQuads - index) * quadSize;
  358. var dstOffset = index * quadSize;
  359. this._quadsReader.set(this._quadsReader.subarray(srcOffset,srcOffset + moveLength),dstOffset);
  360. }
  361. this._dirty = true;
  362. },
  363. /**
  364. * <p>Removes all Quads. <br />
  365. * The TextureAtlas capacity remains untouched. No memory is freed.<br />
  366. * The total number of quads to be drawn will be 0</p>
  367. */
  368. removeAllQuads:function () {
  369. this._quads.length = 0;
  370. this._totalQuads = 0;
  371. },
  372. _setDirty:function(dirty){
  373. this._dirty = dirty;
  374. },
  375. /**
  376. * <p>Resize the capacity of the CCTextureAtlas.<br />
  377. * The new capacity can be lower or higher than the current one<br />
  378. * It returns YES if the resize was successful. <br />
  379. * If it fails to resize the capacity it will return NO with a new capacity of 0. <br />
  380. * no used for js</p>
  381. * @param {Number} newCapacity
  382. * @return {Boolean}
  383. */
  384. resizeCapacity:function (newCapacity) {
  385. if (newCapacity == this._capacity)
  386. return true;
  387. var quadSize = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  388. var oldCapacity = this._capacity;
  389. // update capacity and totolQuads
  390. this._totalQuads = Math.min(this._totalQuads, newCapacity);
  391. this._capacity = 0 | newCapacity;
  392. var i, capacity = this._capacity, locTotalQuads = this._totalQuads;
  393. if (this._quads == null) {
  394. this._quads = [];
  395. this._quadsArrayBuffer = new ArrayBuffer(quadSize * capacity);
  396. this._quadsReader = new Uint8Array(this._quadsArrayBuffer);
  397. for(i = 0; i< capacity; i++)
  398. this._quads = new cc.V3F_C4B_T2F_Quad(null, null, null, null, this._quadsArrayBuffer,i * quadSize);
  399. } else {
  400. var newQuads, newArrayBuffer, quads = this._quads;
  401. if (capacity > oldCapacity) {
  402. newQuads = [];
  403. newArrayBuffer = new ArrayBuffer(quadSize * capacity);
  404. for(i = 0; i < locTotalQuads;i++){
  405. newQuads[i] = new cc.V3F_C4B_T2F_Quad(quads[i].tl,quads[i].bl,quads[i].tr,quads[i].br,
  406. newArrayBuffer,i * quadSize);
  407. }
  408. for(;i<capacity; i ++)
  409. newQuads[i] = new cc.V3F_C4B_T2F_Quad(null,null,null,null, newArrayBuffer,i * quadSize);
  410. this._quadsReader = new Uint8Array(newArrayBuffer);
  411. this._quads = newQuads;
  412. this._quadsArrayBuffer = newArrayBuffer;
  413. } else {
  414. var count = Math.max(locTotalQuads, capacity);
  415. newQuads = [];
  416. newArrayBuffer = new ArrayBuffer(quadSize * capacity);
  417. for(i = 0; i < count;i++){
  418. newQuads[i] = new cc.V3F_C4B_T2F_Quad(quads[i].tl,quads[i].bl,quads[i].tr,quads[i].br,
  419. newArrayBuffer,i * quadSize);
  420. }
  421. this._quadsReader = new Uint8Array(newArrayBuffer);
  422. this._quads = newQuads;
  423. this._quadsArrayBuffer = newArrayBuffer;
  424. }
  425. }
  426. if (this._indices == null) {
  427. this._indices = new Uint16Array(capacity * 6);
  428. } else {
  429. if (capacity > oldCapacity) {
  430. var tempIndices = new Uint16Array(capacity * 6);
  431. tempIndices.set(this._indices, 0);
  432. this._indices = tempIndices;
  433. } else {
  434. this._indices = this._indices.subarray(0, capacity * 6);
  435. }
  436. }
  437. this._setupIndices();
  438. this._mapBuffers();
  439. this._dirty = true;
  440. return true;
  441. },
  442. /**
  443. * Used internally by CCParticleBatchNode <br/>
  444. * don't use this unless you know what you're doing
  445. * @param {Number} amount
  446. */
  447. increaseTotalQuadsWith:function (amount) {
  448. this._totalQuads += amount;
  449. },
  450. /**
  451. * Moves an amount of quads from oldIndex at newIndex
  452. * @param {Number} oldIndex
  453. * @param {Number} amount
  454. * @param {Number} newIndex
  455. */
  456. moveQuadsFromIndex: function (oldIndex, amount, newIndex) {
  457. if (newIndex === undefined) {
  458. newIndex = amount;
  459. amount = this._totalQuads - oldIndex;
  460. if((newIndex + (this._totalQuads - oldIndex)) > this._capacity)
  461. throw "cc.TextureAtlas.moveQuadsFromIndex(): move is out of bounds";
  462. if(amount === 0)
  463. return;
  464. }else{
  465. if((newIndex + amount) > this._totalQuads)
  466. throw "cc.TextureAtlas.moveQuadsFromIndex(): Invalid newIndex";
  467. if(oldIndex >= this._totalQuads)
  468. throw "cc.TextureAtlas.moveQuadsFromIndex(): Invalid oldIndex";
  469. if (oldIndex == newIndex)
  470. return;
  471. }
  472. var quadSize = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  473. var srcOffset = oldIndex * quadSize;
  474. var srcLength = amount * quadSize;
  475. var locQuadsReader = this._quadsReader;
  476. var sourceArr = locQuadsReader.subarray(srcOffset, srcOffset + srcLength);
  477. var dstOffset = newIndex * quadSize;
  478. var moveLength, moveStart;
  479. if (newIndex < oldIndex) {
  480. moveLength = (oldIndex - newIndex) * quadSize;
  481. moveStart = newIndex * quadSize;
  482. locQuadsReader.set(locQuadsReader.subarray(moveStart, moveStart + moveLength), moveStart + srcLength)
  483. } else {
  484. moveLength = (newIndex - oldIndex) * quadSize;
  485. moveStart = (oldIndex + amount) * quadSize;
  486. locQuadsReader.set(locQuadsReader.subarray(moveStart, moveStart + moveLength), srcOffset);
  487. }
  488. locQuadsReader.set(sourceArr, dstOffset);
  489. this._dirty = true;
  490. },
  491. /**
  492. * Ensures that after a realloc quads are still empty <br/>
  493. * Used internally by CCParticleBatchNode
  494. * @param {Number} index
  495. * @param {Number} amount
  496. */
  497. fillWithEmptyQuadsFromIndex:function (index, amount) {
  498. var count = amount * cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
  499. var clearReader = new Uint8Array(this._quadsArrayBuffer, index * cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT, count);
  500. for (var i = 0; i < count; i++)
  501. clearReader[i] = 0;
  502. },
  503. // TextureAtlas - Drawing
  504. /**
  505. * <p>Draws n quads from an index (offset). <br />
  506. * n + start can't be greater than the capacity of the atlas</p>
  507. * @param {Number} n
  508. * @param {Number} start
  509. */
  510. drawNumberOfQuads:function (n, start) {
  511. start = start || 0;
  512. if (0 === n || !this._texture || !this._texture.isLoaded())
  513. return;
  514. var gl = cc.renderContext;
  515. cc.glBindTexture2D(this._texture);
  516. //
  517. // Using VBO without VAO
  518. //
  519. //vertices
  520. //gl.bindBuffer(gl.ARRAY_BUFFER, this._buffersVBO[0]);
  521. // XXX: update is done in draw... perhaps it should be done in a timer
  522. cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  523. gl.bindBuffer(gl.ARRAY_BUFFER, this._quadsWebBuffer);
  524. if (this._dirty)
  525. gl.bufferData(gl.ARRAY_BUFFER, this._quadsArrayBuffer, gl.DYNAMIC_DRAW);
  526. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 24, 0); // vertices
  527. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, 12); // colors
  528. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 24, 16); // tex coords
  529. if (this._dirty)
  530. this._dirty = false;
  531. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._buffersVBO[1]);
  532. if (cc.TEXTURE_ATLAS_USE_TRIANGLE_STRIP)
  533. gl.drawElements(gl.TRIANGLE_STRIP, n * 6, gl.UNSIGNED_SHORT, start * 6 * this._indices.BYTES_PER_ELEMENT);
  534. else
  535. gl.drawElements(gl.TRIANGLES, n * 6, gl.UNSIGNED_SHORT, start * 6 * this._indices.BYTES_PER_ELEMENT);
  536. cc.g_NumberOfDraws++;
  537. //cc.CHECK_GL_ERROR_DEBUG();
  538. },
  539. /**
  540. * Draws all the Atlas's Quads
  541. */
  542. drawQuads:function () {
  543. this.drawNumberOfQuads(this._totalQuads, 0);
  544. },
  545. _releaseBuffer: function () {
  546. var gl = cc.renderContext;
  547. if (this._buffersVBO) {
  548. if (this._buffersVBO[0])
  549. gl.deleteBuffer(this._buffersVBO[0]);
  550. if (this._buffersVBO[1])
  551. gl.deleteBuffer(this._buffersVBO[1])
  552. }
  553. if (this._quadsWebBuffer)
  554. gl.deleteBuffer(this._quadsWebBuffer);
  555. }
  556. });
  557. /**
  558. * <p>Creates a TextureAtlas with an filename and with an initial capacity for Quads. <br />
  559. * The TextureAtlas capacity can be increased in runtime. </p>
  560. * @param {String} file
  561. * @param {Number} capacity
  562. * @return {cc.TextureAtlas|Null}
  563. * @example
  564. * //example
  565. * var textureAtlas = cc.TextureAtlas.create("hello.png", 3);
  566. */
  567. cc.TextureAtlas.create = function (file, capacity) {
  568. var textureAtlas = new cc.TextureAtlas();
  569. if (textureAtlas && textureAtlas.initWithFile(file, capacity))
  570. return textureAtlas;
  571. return null;
  572. };
  573. /**
  574. * <p>Creates a TextureAtlas with a previously initialized Texture2D object, and with an initial capacity for n Quads.
  575. * The TextureAtlas capacity can be increased in runtime.</p>
  576. * @param {Image|cc.Texture2D} texture
  577. * @param {Number} capacity
  578. * @return {cc.TextureAtlas}
  579. * @example
  580. * //example
  581. * var texture = cc.TextureCache.getInstance().addImage("hello.png");
  582. * var textureAtlas = cc.TextureAtlas.createWithTexture(texture, 3);
  583. */
  584. cc.TextureAtlas.createWithTexture = function (texture, capacity) {
  585. var textureAtlas = new cc.TextureAtlas();
  586. if (textureAtlas && textureAtlas.initWithTexture(texture, capacity))
  587. return textureAtlas;
  588. return null;
  589. };