CCTextureAtlas.js 24 KB

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