CCSpriteBatchNode.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /****************************************************************************
  2. Copyright (c) 2009-2010 Ricardo Quesada
  3. Copyright (c) 2009 Matt Oswald
  4. Copyright (c) 2010-2012 cocos2d-x.org
  5. Copyright (c) 2011 Zynga Inc.
  6. Copyright (c) 2013-2016 Chukong Technologies Inc.
  7. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  8. http://www.cocos2d-x.org
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. ****************************************************************************/
  25. #ifndef __CC_SPRITE_BATCH_NODE_H__
  26. #define __CC_SPRITE_BATCH_NODE_H__
  27. #include <vector>
  28. #include "2d/CCNode.h"
  29. #include "base/CCProtocols.h"
  30. #include "renderer/CCTextureAtlas.h"
  31. #include "renderer/CCBatchCommand.h"
  32. NS_CC_BEGIN
  33. /**
  34. * @addtogroup _2d
  35. * @{
  36. */
  37. class Sprite;
  38. /** SpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call
  39. * (often known as "batch draw").
  40. *
  41. * A SpriteBatchNode can reference one and only one texture (one image file, one texture atlas).
  42. * Only the Sprites that are contained in that texture can be added to the SpriteBatchNode.
  43. * All Sprites added to a SpriteBatchNode are drawn in one OpenGL ES draw call.
  44. * If the Sprites are not added to a SpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.
  45. *
  46. *
  47. * Limitations:
  48. * - The only object that is accepted as child (or grandchild, grand-grandchild, etc...) is Sprite or any subclass of Sprite. eg: particles, labels and layer can't be added to a SpriteBatchNode.
  49. * - Either all its children are Aliased or Antialiased. It can't be a mix. This is because "alias" is a property of the texture, and all the sprites share the same texture.
  50. *
  51. * @since v0.7.1
  52. */
  53. class CC_DLL SpriteBatchNode : public Node, public TextureProtocol
  54. {
  55. static const int DEFAULT_CAPACITY = 29;
  56. public:
  57. /** Creates a SpriteBatchNode with a texture2d and capacity of children.
  58. * The capacity will be increased in 33% in runtime if it runs out of space.
  59. *
  60. * @param tex A texture2d.
  61. * @param capacity The capacity of children.
  62. * @return Return an autorelease object.
  63. */
  64. static SpriteBatchNode* createWithTexture(Texture2D* tex, ssize_t capacity = DEFAULT_CAPACITY);
  65. /** Creates a SpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and capacity of children.
  66. * The capacity will be increased in 33% in runtime if it runs out of space.
  67. * The file will be loaded using the TextureMgr.
  68. *
  69. * @param fileImage A file image (.png, .jpeg, .pvr, etc).
  70. * @param capacity The capacity of children.
  71. * @return Return an autorelease object.
  72. */
  73. static SpriteBatchNode* create(const std::string& fileImage, ssize_t capacity = DEFAULT_CAPACITY);
  74. /** Returns the TextureAtlas object.
  75. *
  76. * @return The TextureAtlas object.
  77. */
  78. TextureAtlas* getTextureAtlas() { return _textureAtlas; }
  79. /** Sets the TextureAtlas object.
  80. *
  81. * @param textureAtlas The TextureAtlas object.
  82. */
  83. void setTextureAtlas(TextureAtlas* textureAtlas)
  84. {
  85. if (textureAtlas != _textureAtlas)
  86. {
  87. CC_SAFE_RETAIN(textureAtlas);
  88. CC_SAFE_RELEASE(_textureAtlas);
  89. _textureAtlas = textureAtlas;
  90. }
  91. }
  92. /** Returns an array with the descendants (children, gran children, etc.).
  93. * This is specific to BatchNode. In order to use the children, use getChildren() instead.
  94. *
  95. * @return An array with the descendants (children, gran children, etc.).
  96. */
  97. const std::vector<Sprite*>& getDescendants() const { return _descendants; }
  98. /** Increase the Atlas Capacity. */
  99. void increaseAtlasCapacity();
  100. /** Removes a child given a certain index. It will also cleanup the running actions depending on the cleanup parameter.
  101. *
  102. * @param index A certain index.
  103. * @param doCleanup Whether or not to cleanup the running actions.
  104. * @warning Removing a child from a SpriteBatchNode is very slow.
  105. */
  106. void removeChildAtIndex(ssize_t index, bool doCleanup);
  107. /** Append the child.
  108. *
  109. * @param sprite A Sprite.
  110. */
  111. void appendChild(Sprite* sprite);
  112. /** Remove a sprite from Atlas.
  113. *
  114. * @param sprite A Sprite.
  115. */
  116. void removeSpriteFromAtlas(Sprite *sprite);
  117. /** Rebuild index with a sprite all child.
  118. *
  119. * @param parent The parent sprite.
  120. * @param index The child index.
  121. * @return Index.
  122. */
  123. ssize_t rebuildIndexInOrder(Sprite *parent, ssize_t index);
  124. /** Get the Max image block index,in all child.
  125. *
  126. * @param sprite The parent sprite.
  127. * @return Index.
  128. */
  129. ssize_t highestAtlasIndexInChild(Sprite *sprite);
  130. /** Get the Min image block index,in all child.
  131. *
  132. * @param sprite The parent sprite.
  133. * @return Index.
  134. */
  135. ssize_t lowestAtlasIndexInChild(Sprite *sprite);
  136. /** Get the nearest index from the sprite in z.
  137. *
  138. * @param sprite The parent sprite.
  139. * @param z Z order for drawing priority.
  140. * @return Index.
  141. */
  142. ssize_t atlasIndexForChild(Sprite *sprite, int z);
  143. /* Sprites use this to start sortChildren, don't call this manually. */
  144. void reorderBatch(bool reorder);
  145. //
  146. // Overrides
  147. //
  148. // TextureProtocol
  149. virtual Texture2D* getTexture() const override;
  150. virtual void setTexture(Texture2D *texture) override;
  151. /**
  152. *@code
  153. * When this function bound into js or lua,the parameter will be changed.
  154. * In js: var setBlendFunc(var src, var dst).
  155. * @endcode
  156. * @lua NA
  157. */
  158. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  159. /**
  160. * @lua NA
  161. */
  162. virtual const BlendFunc& getBlendFunc() const override;
  163. /**
  164. * @js NA
  165. */
  166. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  167. using Node::addChild;
  168. virtual void addChild(Node * child, int zOrder, int tag) override;
  169. virtual void addChild(Node * child, int zOrder, const std::string &name) override;
  170. virtual void reorderChild(Node *child, int zOrder) override;
  171. virtual void removeChild(Node *child, bool cleanup) override;
  172. /**
  173. * @js NA
  174. */
  175. virtual void removeAllChildrenWithCleanup(bool cleanup) override;
  176. virtual void sortAllChildren() override;
  177. /**
  178. * @js NA
  179. */
  180. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  181. /**
  182. * @js NA
  183. */
  184. virtual std::string getDescription() const override;
  185. /** Inserts a quad at a certain index into the texture atlas. The Sprite won't be added into the children array.
  186. * This method should be called only when you are dealing with very big AtlasSprite and when most of the Sprite won't be updated.
  187. * For example: a tile map (TMXMap) or a label with lots of characters (LabelBMFont).
  188. */
  189. void insertQuadFromSprite(Sprite *sprite, ssize_t index);
  190. /* This is the opposite of "addQuadFromSprite.
  191. * It add the sprite to the children and descendants array, but it doesn't update add it to the texture atlas
  192. */
  193. SpriteBatchNode * addSpriteWithoutQuad(Sprite *child, int z, int aTag);
  194. /** reserves capacity for the batch node.
  195. If the current capacity is bigger, nothing happens.
  196. otherwise, a new capacity is allocated */
  197. void reserveCapacity(ssize_t newCapacity);
  198. CC_CONSTRUCTOR_ACCESS:
  199. /**
  200. * @js ctor
  201. */
  202. SpriteBatchNode();
  203. /**
  204. * @js NA
  205. * @lua NA
  206. */
  207. virtual ~SpriteBatchNode();
  208. /** initializes a SpriteBatchNode with a texture2d and capacity of children.
  209. The capacity will be increased in 33% in runtime if it runs out of space.
  210. */
  211. bool initWithTexture(Texture2D *tex, ssize_t capacity = DEFAULT_CAPACITY);
  212. /** initializes a SpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and a capacity of children.
  213. The capacity will be increased in 33% in runtime if it runs out of space.
  214. The file will be loaded using the TextureMgr.
  215. * @js init
  216. * @lua init
  217. */
  218. bool initWithFile(const std::string& fileImage, ssize_t capacity = DEFAULT_CAPACITY);
  219. bool init() override;
  220. protected:
  221. /** Updates a quad at a certain index into the texture atlas. The Sprite won't be added into the children array.
  222. This method should be called only when you are dealing with very big AtlasSprite and when most of the Sprite won't be updated.
  223. For example: a tile map (TMXMap) or a label with lots of characters (LabelBMFont)
  224. */
  225. void updateQuadFromSprite(Sprite *sprite, ssize_t index);
  226. void updateAtlasIndex(Sprite* sprite, ssize_t* curIndex);
  227. void swap(ssize_t oldIndex, ssize_t newIndex);
  228. void updateBlendFunc();
  229. TextureAtlas *_textureAtlas;
  230. BlendFunc _blendFunc;
  231. BatchCommand _batchCommand; // render command
  232. // all descendants: children, grand children, etc...
  233. // There is not need to retain/release these objects, since they are already retained by _children
  234. // So, using std::vector<Sprite*> is slightly faster than using cocos2d::Array for this particular case
  235. std::vector<Sprite*> _descendants;
  236. };
  237. // end of sprite_nodes group
  238. /** @} */
  239. NS_CC_END
  240. #endif // __CC_SPRITE_BATCH_NODE_H__