CCParticleBatchNode.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2009 Matt Oswald
  3. * Copyright (c) 2009-2010 Ricardo Quesada
  4. * Copyright (c) 2010-2012 cocos2d-x.org
  5. * Copyright (c) 2011 Zynga Inc.
  6. * Copyright (c) 2011 Marco Tillemans
  7. * Copyright (c) 2013-2017 Chukong Technologies Inc.
  8. *
  9. * http://www.cocos2d-x.org
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. */
  30. #ifndef __CCPARTICLEBATCHNODE_H__
  31. #define __CCPARTICLEBATCHNODE_H__
  32. #include "2d/CCNode.h"
  33. #include "base/CCProtocols.h"
  34. #include "renderer/CCBatchCommand.h"
  35. NS_CC_BEGIN
  36. class Texture2D;
  37. class TextureAtlas;
  38. class ParticleSystem;
  39. /**
  40. * @addtogroup _2d
  41. * @{
  42. */
  43. #define kParticleDefaultCapacity 500
  44. /** @class ParticleBatchNode
  45. * @brief ParticleBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call
  46. * (often known as "batch draw").
  47. *
  48. * A ParticleBatchNode can reference one and only one texture (one image file, one texture atlas).
  49. * Only the ParticleSystems that are contained in that texture can be added to the SpriteBatchNode.
  50. * All ParticleSystems added to a SpriteBatchNode are drawn in one OpenGL ES draw call.
  51. * If the ParticleSystems are not added to a ParticleBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.
  52. *
  53. *
  54. * Limitations:
  55. * - At the moment only ParticleSystemQuad is supported
  56. * - All systems need to be drawn with the same parameters, blend function, aliasing, texture
  57. *
  58. * Most efficient usage
  59. * - Initialize the ParticleBatchNode with the texture and enough capacity for all the particle systems
  60. * - Initialize all particle systems and add them as child to the batch node
  61. * @since v1.1
  62. */
  63. class CC_DLL ParticleBatchNode : public Node, public TextureProtocol
  64. {
  65. public:
  66. /** Create the particle system with Texture2D, a capacity of particles, which particle system to use.
  67. *
  68. * @param tex A given texture.
  69. * @param capacity A capacity of particles.
  70. * @return An autoreleased ParticleBatchNode object.
  71. * @js NA
  72. */
  73. static ParticleBatchNode* createWithTexture(Texture2D *tex, int capacity = kParticleDefaultCapacity);
  74. /** Create the particle system with the name of a file on disk (for a list of supported formats look at the Texture2D class), a capacity of particles.
  75. *
  76. * @param fileImage A given file name.
  77. * @param capacity A capacity of particles.
  78. * @return An autoreleased ParticleBatchNode object.
  79. */
  80. static ParticleBatchNode* create(const std::string& fileImage, int capacity = kParticleDefaultCapacity);
  81. /** Inserts a child into the ParticleBatchNode.
  82. *
  83. * @param system A given particle system.
  84. * @param index The insert index.
  85. */
  86. void insertChild(ParticleSystem* system, int index);
  87. /** Remove a child of the ParticleBatchNode.
  88. *
  89. * @param index The index of the child.
  90. * @param doCleanup True if all actions and callbacks on this node should be removed, false otherwise.
  91. */
  92. void removeChildAtIndex(int index, bool doCleanup);
  93. void removeAllChildrenWithCleanup(bool doCleanup) override;
  94. /** Disables a particle by inserting a 0'd quad into the texture atlas.
  95. *
  96. * @param particleIndex The index of the particle.
  97. */
  98. void disableParticle(int particleIndex);
  99. /** Gets the texture atlas used for drawing the quads.
  100. *
  101. * @return The texture atlas used for drawing the quads.
  102. */
  103. TextureAtlas* getTextureAtlas() const { return _textureAtlas; }
  104. /** Sets the texture atlas used for drawing the quads.
  105. *
  106. * @param atlas The texture atlas used for drawing the quads.
  107. */
  108. void setTextureAtlas(TextureAtlas* atlas) { _textureAtlas = atlas; }
  109. // Overrides
  110. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  111. using Node::addChild;
  112. virtual void addChild(Node * child, int zOrder, int tag) override;
  113. virtual void addChild(Node * child, int zOrder, const std::string &name) override;
  114. virtual void removeChild(Node* child, bool cleanup) override;
  115. virtual void reorderChild(Node * child, int zOrder) override;
  116. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  117. virtual Texture2D* getTexture(void) const override;
  118. virtual void setTexture(Texture2D *texture) override;
  119. /**
  120. * @code
  121. * When this function bound into js or lua,the parameter will be changed
  122. * In js: var setBlendFunc(var src, var dst)
  123. * @endcode
  124. * @lua NA
  125. */
  126. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  127. /**
  128. * @js NA
  129. * @lua NA
  130. */
  131. virtual const BlendFunc& getBlendFunc(void) const override;
  132. CC_CONSTRUCTOR_ACCESS:
  133. /**
  134. * @js ctor
  135. */
  136. ParticleBatchNode();
  137. /**
  138. * @js NA
  139. * @lua NA
  140. */
  141. virtual ~ParticleBatchNode();
  142. /** initializes the particle system with Texture2D, a capacity of particles */
  143. bool initWithTexture(Texture2D *tex, int capacity);
  144. /** initializes the particle system with the name of a file on disk (for a list of supported formats look at the Texture2D class), a capacity of particles */
  145. bool initWithFile(const std::string& fileImage, int capacity);
  146. private:
  147. void updateAllAtlasIndexes();
  148. void increaseAtlasCapacityTo(ssize_t quantity);
  149. int searchNewPositionInChildrenForZ(int z);
  150. void getCurrentIndex(int* oldIndex, int* newIndex, Node* child, int z);
  151. int addChildHelper(ParticleSystem* child, int z, int aTag, const std::string &name, bool setTag);
  152. void addChildByTagOrName(ParticleSystem* child, int z, int tag, const std::string &name, bool setTag);
  153. void updateBlendFunc(void);
  154. /** the texture atlas used for drawing the quads */
  155. TextureAtlas* _textureAtlas;
  156. /** the blend function used for drawing the quads */
  157. BlendFunc _blendFunc;
  158. // quad command
  159. BatchCommand _batchCommand;
  160. };
  161. // end of _2d group
  162. /// @}
  163. NS_CC_END
  164. #endif /* __CCPARTICLEBATCHNODE_H__ */