CCParticleBatchNode.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /**
  2. * Copyright (c) 2010-2012 cocos2d-x.org
  3. * Copyright (C) 2009 Matt Oswald
  4. * Copyright (c) 2009-2010 Ricardo Quesada
  5. * Copyright (c) 2011 Zynga Inc.
  6. * Copyright (c) 2011 Marco Tillemans
  7. *
  8. * http://www.cocos2d-x.org
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. *
  28. */
  29. /**
  30. * paticle default capacity
  31. * @constant
  32. * @type Number
  33. */
  34. cc.PARTICLE_DEFAULT_CAPACITY = 500;
  35. /**
  36. * <p>
  37. * cc.ParticleBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call <br/>
  38. * (often known as "batch draw"). </br>
  39. *
  40. * A cc.ParticleBatchNode can reference one and only one texture (one image file, one texture atlas).<br/>
  41. * Only the cc.ParticleSystems that are contained in that texture can be added to the cc.SpriteBatchNode.<br/>
  42. * All cc.ParticleSystems added to a cc.SpriteBatchNode are drawn in one OpenGL ES draw call.<br/>
  43. * If the cc.ParticleSystems are not added to a cc.ParticleBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.</br>
  44. *
  45. * Limitations:<br/>
  46. * - At the moment only cc.ParticleSystem is supported<br/>
  47. * - All systems need to be drawn with the same parameters, blend function, aliasing, texture<br/>
  48. *
  49. * Most efficient usage<br/>
  50. * - Initialize the ParticleBatchNode with the texture and enough capacity for all the particle systems<br/>
  51. * - Initialize all particle systems and add them as child to the batch node<br/>
  52. * </p>
  53. * @class
  54. * @extends cc.ParticleSystem
  55. */
  56. cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
  57. TextureProtocol:true,
  58. //the blend function used for drawing the quads
  59. _blendFunc:null,
  60. _textureAtlas:null,
  61. ctor:function () {
  62. cc.Node.prototype.ctor.call(this);
  63. this._blendFunc = {src:cc.BLEND_SRC, dst:cc.BLEND_DST};
  64. },
  65. /**
  66. * initializes the particle system with cc.Texture2D, a capacity of particles
  67. * @param {cc.Texture2D|HTMLImageElement|HTMLCanvasElement} texture
  68. * @param {Number} capacity
  69. * @return {Boolean}
  70. */
  71. initWithTexture:function (texture, capacity) {
  72. this._textureAtlas = new cc.TextureAtlas();
  73. this._textureAtlas.initWithTexture(texture, capacity);
  74. // no lazy alloc in this node
  75. this._children.length = 0;
  76. if (cc.renderContextType === cc.WEBGL)
  77. this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURECOLOR));
  78. return true;
  79. },
  80. /**
  81. * initializes the particle system with the name of a file on disk (for a list of supported formats look at the cc.Texture2D class), a capacity of particles
  82. * @param {String} fileImage
  83. * @param {Number} capacity
  84. * @return {Boolean}
  85. */
  86. initWithFile:function (fileImage, capacity) {
  87. var tex = cc.TextureCache.getInstance().addImage(fileImage);
  88. return this.initWithTexture(tex, capacity);
  89. },
  90. /**
  91. * initializes the particle system with the name of a file on disk (for a list of supported formats look at the cc.Texture2D class), a capacity of particles
  92. * @param {String} fileImage
  93. * @param {Number} capacity
  94. * @return {Boolean}
  95. */
  96. init:function (fileImage, capacity) {
  97. var tex = cc.TextureCache.getInstance().addImage(fileImage);
  98. return this.initWithTexture(tex, capacity);
  99. },
  100. /**
  101. * Add a child into the cc.ParticleBatchNode
  102. * @param {cc.ParticleSystem} child
  103. * @param {Number} zOrder
  104. * @param {Number} tag
  105. */
  106. addChild:function (child, zOrder, tag) {
  107. if(!child)
  108. throw "cc.ParticleBatchNode.addChild() : child should be non-null";
  109. if(!(child instanceof cc.ParticleSystem))
  110. throw "cc.ParticleBatchNode.addChild() : only supports cc.ParticleSystem as children";
  111. zOrder = (zOrder == null) ? child.getZOrder() : zOrder;
  112. tag = (tag == null) ? child.getTag() : tag;
  113. if(child.getTexture() != this._textureAtlas.getTexture())
  114. throw "cc.ParticleSystem.addChild() : the child is not using the same texture id";
  115. // If this is the 1st children, then copy blending function
  116. var childBlendFunc = child.getBlendFunc();
  117. if (this._children.length === 0)
  118. this.setBlendFunc(childBlendFunc);
  119. else{
  120. if((childBlendFunc.src != this._blendFunc.src) || (childBlendFunc.dst != this._blendFunc.dst)){
  121. cc.log("cc.ParticleSystem.addChild() : Can't add a ParticleSystem that uses a different blending function");
  122. return;
  123. }
  124. }
  125. //no lazy sorting, so don't call super addChild, call helper instead
  126. var pos = this._addChildHelper(child, zOrder, tag);
  127. //get new atlasIndex
  128. var atlasIndex = 0;
  129. if (pos != 0) {
  130. var p = this._children[pos - 1];
  131. atlasIndex = p.getAtlasIndex() + p.getTotalParticles();
  132. } else
  133. atlasIndex = 0;
  134. this.insertChild(child, atlasIndex);
  135. // update quad info
  136. child.setBatchNode(this);
  137. },
  138. /**
  139. * Inserts a child into the cc.ParticleBatchNode
  140. * @param {cc.ParticleSystem} pSystem
  141. * @param {Number} index
  142. */
  143. insertChild:function (pSystem, index) {
  144. var totalParticles = pSystem.getTotalParticles();
  145. var locTextureAtlas = this._textureAtlas;
  146. var totalQuads = locTextureAtlas.getTotalQuads();
  147. pSystem.setAtlasIndex(index);
  148. if (totalQuads + totalParticles > locTextureAtlas.getCapacity()) {
  149. this._increaseAtlasCapacityTo(totalQuads + totalParticles);
  150. // after a realloc empty quads of textureAtlas can be filled with gibberish (realloc doesn't perform calloc), insert empty quads to prevent it
  151. locTextureAtlas.fillWithEmptyQuadsFromIndex(locTextureAtlas.getCapacity() - totalParticles, totalParticles);
  152. }
  153. // make room for quads, not necessary for last child
  154. if (pSystem.getAtlasIndex() + totalParticles != totalQuads)
  155. locTextureAtlas.moveQuadsFromIndex(index, index + totalParticles);
  156. // increase totalParticles here for new particles, update method of particlesystem will fill the quads
  157. locTextureAtlas.increaseTotalQuadsWith(totalParticles);
  158. this._updateAllAtlasIndexes();
  159. },
  160. /**
  161. * @param {cc.ParticleSystem} child
  162. * @param {Boolean} cleanup
  163. */
  164. removeChild:function (child, cleanup) {
  165. // explicit nil handling
  166. if (child == null)
  167. return;
  168. if(!(child instanceof cc.ParticleSystem))
  169. throw "cc.ParticleBatchNode.removeChild(): only supports cc.ParticleSystem as children";
  170. if(this._children.indexOf(child) == -1){
  171. cc.log("cc.ParticleBatchNode.removeChild(): doesn't contain the sprite. Can't remove it");
  172. return;
  173. }
  174. cc.Node.prototype.removeChild.call(this, child, cleanup);
  175. var locTextureAtlas = this._textureAtlas;
  176. // remove child helper
  177. locTextureAtlas.removeQuadsAtIndex(child.getAtlasIndex(), child.getTotalParticles());
  178. // after memmove of data, empty the quads at the end of array
  179. locTextureAtlas.fillWithEmptyQuadsFromIndex(locTextureAtlas.getTotalQuads(), child.getTotalParticles());
  180. // paticle could be reused for self rendering
  181. child.setBatchNode(null);
  182. this._updateAllAtlasIndexes();
  183. },
  184. /**
  185. * Reorder will be done in this function, no "lazy" reorder to particles
  186. * @param {cc.ParticleSystem} child
  187. * @param {Number} zOrder
  188. */
  189. reorderChild:function (child, zOrder) {
  190. if(!child)
  191. throw "cc.ParticleBatchNode.reorderChild(): child should be non-null";
  192. if(!(child instanceof cc.ParticleSystem))
  193. throw "cc.ParticleBatchNode.reorderChild(): only supports cc.QuadParticleSystems as children";
  194. if(this._children.indexOf(child) === -1){
  195. cc.log("cc.ParticleBatchNode.reorderChild(): Child doesn't belong to batch");
  196. return;
  197. }
  198. if (zOrder == child.getZOrder())
  199. return;
  200. // no reordering if only 1 child
  201. if (this._children.length > 1) {
  202. var getIndexes = this._getCurrentIndex(child, zOrder);
  203. if (getIndexes.oldIndex != getIndexes.newIndex) {
  204. // reorder m_pChildren.array
  205. cc.ArrayRemoveObjectAtIndex(this._children, getIndexes.oldIndex);
  206. this._children = cc.ArrayAppendObjectToIndex(this._children, child, getIndexes.newIndex);
  207. // save old altasIndex
  208. var oldAtlasIndex = child.getAtlasIndex();
  209. // update atlas index
  210. this._updateAllAtlasIndexes();
  211. // Find new AtlasIndex
  212. var newAtlasIndex = 0;
  213. var locChildren = this._children;
  214. for (var i = 0; i < locChildren.length; i++) {
  215. var pNode = locChildren[i];
  216. if (pNode == child) {
  217. newAtlasIndex = child.getAtlasIndex();
  218. break;
  219. }
  220. }
  221. // reorder textureAtlas quads
  222. this._textureAtlas.moveQuadsFromIndex(oldAtlasIndex, child.getTotalParticles(), newAtlasIndex);
  223. child.updateWithNoTime();
  224. }
  225. }
  226. child._setZOrder(zOrder);
  227. },
  228. /**
  229. * @param {Number} index
  230. * @param {Boolean} doCleanup
  231. */
  232. removeChildAtIndex:function (index, doCleanup) {
  233. this.removeChild(this._children[i], doCleanup);
  234. },
  235. /**
  236. * @param {Boolean} doCleanup
  237. */
  238. removeAllChildren:function (doCleanup) {
  239. var locChildren = this._children;
  240. for (var i = 0; i < locChildren.length; i++) {
  241. locChildren[i].setBatchNode(null);
  242. }
  243. cc.Node.prototype.removeAllChildren.call(this, doCleanup);
  244. this._textureAtlas.removeAllQuads();
  245. },
  246. /**
  247. * disables a particle by inserting a 0'd quad into the texture atlas
  248. * @param {Number} particleIndex
  249. */
  250. disableParticle:function (particleIndex) {
  251. var quad = ((this._textureAtlas.getQuads())[particleIndex]);
  252. quad.br.vertices.x = quad.br.vertices.y = quad.tr.vertices.x = quad.tr.vertices.y =
  253. quad.tl.vertices.x = quad.tl.vertices.y = quad.bl.vertices.x = quad.bl.vertices.y = 0.0;
  254. this._textureAtlas._setDirty(true);
  255. },
  256. /**
  257. * @override
  258. * @param {CanvasContext} ctx
  259. */
  260. draw:function (ctx) {
  261. //cc.PROFILER_STOP("CCParticleBatchNode - draw");
  262. if (cc.renderContextType === cc.CANVAS)
  263. return;
  264. if (this._textureAtlas.getTotalQuads() == 0)
  265. return;
  266. cc.NODE_DRAW_SETUP(this);
  267. cc.glBlendFuncForParticle(this._blendFunc.src, this._blendFunc.dst);
  268. this._textureAtlas.drawQuads();
  269. //cc.PROFILER_STOP("CCParticleBatchNode - draw");
  270. },
  271. /**
  272. * returns the used texture
  273. * @return {cc.Texture2D|HTMLImageElement|HTMLCanvasElement}
  274. */
  275. getTexture:function () {
  276. return this._textureAtlas.getTexture();
  277. },
  278. /**
  279. * sets a new texture. it will be retained
  280. * @param {cc.Texture2D|HTMLImageElement|HTMLCanvasElement} texture
  281. */
  282. setTexture:function (texture) {
  283. this._textureAtlas.setTexture(texture);
  284. // If the new texture has No premultiplied alpha, AND the blendFunc hasn't been changed, then update it
  285. var locBlendFunc = this._blendFunc;
  286. if (texture && !texture.hasPremultipliedAlpha() && ( locBlendFunc.src == gl.BLEND_SRC && locBlendFunc.dst == gl.BLEND_DST )) {
  287. locBlendFunc.src = gl.SRC_ALPHA;
  288. locBlendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
  289. }
  290. },
  291. /**
  292. * set the blending function used for the texture
  293. * @param {Number} src
  294. * @param {Number} dst
  295. */
  296. setBlendFunc:function (src, dst) {
  297. if (dst === undefined){
  298. this._blendFunc.src = src.src;
  299. this._blendFunc.dst = src.dst;
  300. } else{
  301. this._blendFunc.src = src;
  302. this._blendFunc.src = dst;
  303. }
  304. },
  305. /**
  306. * returns the blending function used for the texture
  307. * @return {cc.BlendFunc}
  308. */
  309. getBlendFunc:function () {
  310. return {src:this._blendFunc.src, dst:this._blendFunc.dst};
  311. },
  312. // override visit.
  313. // Don't call visit on it's children
  314. visit:function (ctx) {
  315. if (cc.renderContextType === cc.CANVAS)
  316. return;
  317. // CAREFUL:
  318. // This visit is almost identical to cc.Node#visit
  319. // with the exception that it doesn't call visit on it's children
  320. //
  321. // The alternative is to have a void cc.Sprite#visit, but
  322. // although this is less mantainable, is faster
  323. //
  324. if (!this._visible)
  325. return;
  326. cc.kmGLPushMatrix();
  327. if (this._grid && this._grid.isActive()) {
  328. this._grid.beforeDraw();
  329. this.transformAncestors();
  330. }
  331. this.transform(ctx);
  332. this.draw(ctx);
  333. if (this._grid && this._grid.isActive())
  334. this._grid.afterDraw(this);
  335. cc.kmGLPopMatrix();
  336. },
  337. _updateAllAtlasIndexes:function () {
  338. var index = 0;
  339. var locChildren = this._children;
  340. for (var i = 0; i < locChildren.length; i++) {
  341. var child = locChildren[i];
  342. child.setAtlasIndex(index);
  343. index += child.getTotalParticles();
  344. }
  345. },
  346. _increaseAtlasCapacityTo:function (quantity) {
  347. cc.log("cocos2d: cc.ParticleBatchNode: resizing TextureAtlas capacity from [" + this._textureAtlas.getCapacity()
  348. + "] to [" + quantity + "].");
  349. if (!this._textureAtlas.resizeCapacity(quantity)) {
  350. // serious problems
  351. cc.log("cc.ParticleBatchNode._increaseAtlasCapacityTo() : WARNING: Not enough memory to resize the atlas");
  352. }
  353. },
  354. _searchNewPositionInChildrenForZ:function (z) {
  355. var locChildren = this._children;
  356. var count = locChildren.length;
  357. for (var i = 0; i < count; i++) {
  358. if (locChildren[i].getZOrder() > z)
  359. return i;
  360. }
  361. return count;
  362. },
  363. _getCurrentIndex:function (child, z) {
  364. var foundCurrentIdx = false;
  365. var foundNewIdx = false;
  366. var newIndex = 0;
  367. var oldIndex = 0;
  368. var minusOne = 0, locChildren = this._children;
  369. var count = locChildren.length;
  370. for (var i = 0; i < count; i++) {
  371. var pNode = locChildren[i];
  372. // new index
  373. if (pNode.getZOrder() > z && !foundNewIdx) {
  374. newIndex = i;
  375. foundNewIdx = true;
  376. if (foundCurrentIdx && foundNewIdx)
  377. break;
  378. }
  379. // current index
  380. if (child == pNode) {
  381. oldIndex = i;
  382. foundCurrentIdx = true;
  383. if (!foundNewIdx)
  384. minusOne = -1;
  385. if (foundCurrentIdx && foundNewIdx)
  386. break;
  387. }
  388. }
  389. if (!foundNewIdx)
  390. newIndex = count;
  391. newIndex += minusOne;
  392. return {newIndex:newIndex, oldIndex:oldIndex};
  393. },
  394. /**
  395. * <p>
  396. * don't use lazy sorting, reordering the particle systems quads afterwards would be too complex <br/>
  397. * XXX research whether lazy sorting + freeing current quads and calloc a new block with size of capacity would be faster <br/>
  398. * XXX or possibly using vertexZ for reordering, that would be fastest <br/>
  399. * this helper is almost equivalent to CCNode's addChild, but doesn't make use of the lazy sorting <br/>
  400. * </p>
  401. * @param {cc.ParticleSystem} child
  402. * @param {Number} z
  403. * @param {Number} aTag
  404. * @return {Number}
  405. * @private
  406. */
  407. _addChildHelper:function (child, z, aTag) {
  408. if(!child)
  409. throw "cc.ParticleBatchNode._addChildHelper(): child should be non-null";
  410. if(child.getParent()){
  411. cc.log("cc.ParticleBatchNode._addChildHelper(): child already added. It can't be added again");
  412. return null;
  413. }
  414. if (!this._children)
  415. this._children = [];
  416. //don't use a lazy insert
  417. var pos = this._searchNewPositionInChildrenForZ(z);
  418. this._children = cc.ArrayAppendObjectToIndex(this._children, child, pos);
  419. child.setTag(aTag);
  420. child._setZOrder(z);
  421. child.setParent(this);
  422. if (this._running) {
  423. child.onEnter();
  424. child.onEnterTransitionDidFinish();
  425. }
  426. return pos;
  427. },
  428. _updateBlendFunc:function () {
  429. if (!this._textureAtlas.getTexture().hasPremultipliedAlpha()) {
  430. this._blendFunc.src = gl.SRC_ALPHA;
  431. this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
  432. }
  433. },
  434. /**
  435. * return the texture atlas used for drawing the quads
  436. * @return {cc.TextureAtlas}
  437. */
  438. getTextureAtlas:function () {
  439. return this._textureAtlas;
  440. },
  441. /**
  442. * set the texture atlas used for drawing the quads
  443. * @param {cc.TextureAtlas} textureAtlas
  444. */
  445. setTextureAtlas:function (textureAtlas) {
  446. this._textureAtlas = textureAtlas;
  447. }
  448. });
  449. /**
  450. * initializes the particle system with cc.Texture2D, a capacity of particles, which particle system to use
  451. * @param {cc.Texture2D|HTMLImageElement|HTMLCanvasElement} texture
  452. * @param {Number} capacity
  453. * @return {cc.ParticleBatchNode}
  454. */
  455. cc.ParticleBatchNode.createWithTexture = function (texture, capacity) {
  456. var ret = new cc.ParticleBatchNode();
  457. if (ret && ret.initWithTexture(texture, capacity))
  458. return ret;
  459. return null;
  460. };
  461. /**
  462. * initializes the particle system with the name of a file on disk (for a list of supported formats look at the cc.Texture2D class), a capacity of particles
  463. * @param {String} fileImage
  464. * @param capacity
  465. * @return {cc.ParticleBatchNode}
  466. */
  467. cc.ParticleBatchNode.create = function (fileImage, capacity) {
  468. var ret = new cc.ParticleBatchNode();
  469. if (ret && ret.init(fileImage, capacity))
  470. return ret;
  471. return null;
  472. };