CCParticleBatchNode.h 6.9 KB

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