CCTextureAtlas.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #ifndef __CCTEXTURE_ATLAS_H__
  25. #define __CCTEXTURE_ATLAS_H__
  26. #include <string>
  27. #include "base/ccTypes.h"
  28. #include "base/CCRef.h"
  29. #include "base/ccConfig.h"
  30. NS_CC_BEGIN
  31. class Texture2D;
  32. class EventCustom;
  33. class EventListenerCustom;
  34. /**
  35. * @addtogroup _2d
  36. * @{
  37. */
  38. /** @brief A class that implements a Texture Atlas.
  39. Supported features:
  40. * The atlas file can be a PVRTC, PNG or any other format supported by Texture2D
  41. * Quads can be updated in runtime
  42. * Quads can be added in runtime
  43. * Quads can be removed in runtime
  44. * Quads can be re-ordered in runtime
  45. * The TextureAtlas capacity can be increased or decreased in runtime
  46. * OpenGL component: V3F, C4B, T2F.
  47. The quads are rendered using an OpenGL ES VBO.
  48. To render the quads using an interleaved vertex array list, you should modify the ccConfig.h file
  49. @warning If you want to use TextureAtlas, you'd better setup GL status before it's rendered.
  50. Otherwise, the effect of TextureAtlas will be affected by the GL status of other nodes.
  51. */
  52. class CC_DLL TextureAtlas : public Ref
  53. {
  54. public:
  55. /** Creates a TextureAtlas with an filename and with an initial capacity for Quads.
  56. * The TextureAtlas capacity can be increased in runtime.
  57. @param file A null terminated string contains the file path.
  58. @param capacity Capacity for Quads.
  59. */
  60. static TextureAtlas* create(const std::string& file , ssize_t capacity);
  61. /** Creates a TextureAtlas with a previously initialized Texture2D object, and
  62. * with an initial capacity for n Quads.
  63. * The TextureAtlas capacity can be increased in runtime.
  64. @param texture A texture2D object pointer.
  65. @param capacity Capacity for Quads.
  66. */
  67. static TextureAtlas* createWithTexture(Texture2D *texture, ssize_t capacity);
  68. /**
  69. * @js ctor
  70. */
  71. TextureAtlas();
  72. /**
  73. * @js NA
  74. * @lua NA
  75. */
  76. virtual ~TextureAtlas();
  77. /** Initializes a TextureAtlas with a filename and with a certain capacity for Quads.
  78. * The TextureAtlas capacity can be increased in runtime.
  79. *
  80. @attention Do not reinitialize the TextureAtlas because it will leak memory (issue #706).
  81. @param file A null terminated string contains the file path.
  82. @param capacity Capacity for Quads.
  83. */
  84. bool initWithFile(const std::string& file, ssize_t capacity);
  85. /** Initializes a TextureAtlas with a previously initialized Texture2D object, and
  86. * with an initial capacity for Quads.
  87. * The TextureAtlas capacity can be increased in runtime.
  88. @attention: Do not reinitialize the TextureAtlas because it will leak memory (issue #706).
  89. @param texture A texture2D object pointer.
  90. @param capacity Capacity for Quads.
  91. */
  92. bool initWithTexture(Texture2D *texture, ssize_t capacity);
  93. /** Updates a Quad (texture, vertex and color) at a certain index.
  94. @param quad Quad that are going to be rendered.
  95. @param index Index must be between 0 and the atlas capacity - 1.
  96. @since v0.8
  97. */
  98. void updateQuad(V3F_C4B_T2F_Quad* quad, ssize_t index);
  99. /** Inserts a Quad (texture, vertex and color) at a certain index.
  100. @param quad Quad that are going to be rendered.
  101. @param index Index must be between 0 and the atlas capacity - 1.
  102. @since v0.8
  103. */
  104. void insertQuad(V3F_C4B_T2F_Quad* quad, ssize_t index);
  105. /** Inserts a c array of quads at a given index.
  106. @param quads Quad that are going to be rendered.
  107. @param index Index must be between 0 and the atlas capacity - 1.
  108. @param amount The quads array amount.
  109. @attention This method doesn't enlarge the array when amount + index > totalQuads.
  110. @since v1.1
  111. */
  112. void insertQuads(V3F_C4B_T2F_Quad* quads, ssize_t index, ssize_t amount);
  113. /** Removes the quad that is located at a certain index and inserts it at a new index.
  114. This operation is faster than removing and inserting in a quad in 2 different steps.
  115. @since v0.7.2
  116. */
  117. void insertQuadFromIndex(ssize_t fromIndex, ssize_t newIndex);
  118. /** Removes a quad at a given index number.
  119. The capacity remains the same, but the total number of quads to be drawn is reduced in 1.
  120. @since v0.7.2
  121. */
  122. void removeQuadAtIndex(ssize_t index);
  123. /** Removes a amount of quads starting from index.
  124. @since 1.1
  125. */
  126. void removeQuadsAtIndex(ssize_t index, ssize_t amount);
  127. /** Removes all Quads.
  128. The TextureAtlas capacity remains untouched. No memory is freed.
  129. The total number of quads to be drawn will be 0.
  130. @since v0.7.2
  131. */
  132. void removeAllQuads();
  133. /** Resize the capacity of the TextureAtlas.
  134. * The new capacity can be lower or higher than the current one.
  135. * It returns true if the resize was successful.
  136. * If it fails to resize the capacity it will return false with a new capacity of 0.
  137. @param capacity Capacity for Quads.
  138. */
  139. bool resizeCapacity(ssize_t capacity);
  140. /**
  141. Used internally by ParticleBatchNode.
  142. don't use this unless you know what you're doing.
  143. @since 1.1
  144. */
  145. void increaseTotalQuadsWith(ssize_t amount);
  146. /** Moves an amount of quads from oldIndex at newIndex.
  147. @since v1.1
  148. */
  149. void moveQuadsFromIndex(ssize_t oldIndex, ssize_t amount, ssize_t newIndex);
  150. /**
  151. Moves quads from index till totalQuads to the newIndex.
  152. Used internally by ParticleBatchNode.
  153. This method doesn't enlarge the array if newIndex + quads to be moved > capacity.
  154. @since 1.1
  155. */
  156. void moveQuadsFromIndex(ssize_t index, ssize_t newIndex);
  157. /**
  158. Ensures that after a realloc quads are still empty.
  159. Used internally by ParticleBatchNode.
  160. @since 1.1
  161. */
  162. void fillWithEmptyQuadsFromIndex(ssize_t index, ssize_t amount);
  163. /** Draws n quads.
  164. * N can't be greater than the capacity of the Atlas.
  165. */
  166. void drawNumberOfQuads(ssize_t n);
  167. /** Draws n quads from an index (offset).
  168. N + start can't be greater than the capacity of the atlas.
  169. @since v1.0
  170. */
  171. void drawNumberOfQuads(ssize_t numberOfQuads, ssize_t start);
  172. /** Draws all the Atlas's Quads.
  173. */
  174. void drawQuads();
  175. /** Listen the event that renderer was recreated on Android.
  176. */
  177. void listenRendererRecreated(EventCustom* event);
  178. /** Whether or not the array buffer of the VBO needs to be updated.*/
  179. bool isDirty() { return _dirty; }
  180. /** Specify if the array buffer of the VBO needs to be updated. */
  181. void setDirty(bool bDirty) { _dirty = bDirty; }
  182. /**Get quads total amount.
  183. * @js NA
  184. * @lua NA
  185. */
  186. virtual std::string getDescription() const;
  187. /** Gets the quantity of quads that are going to be drawn. */
  188. ssize_t getTotalQuads() const;
  189. /** Gets the quantity of quads that can be stored with the current texture atlas size. */
  190. ssize_t getCapacity() const;
  191. /** Gets the texture of the texture atlas. */
  192. Texture2D* getTexture() const;
  193. /** Sets the texture for the texture atlas. */
  194. void setTexture(Texture2D* texture);
  195. /** Gets the quads that are going to be rendered. */
  196. V3F_C4B_T2F_Quad* getQuads();
  197. /** Sets the quads that are going to be rendered. */
  198. void setQuads(V3F_C4B_T2F_Quad* quads);
  199. private:
  200. void renderCommand();
  201. void setupIndices();
  202. void mapBuffers();
  203. void setupVBOandVAO();
  204. void setupVBO();
  205. protected:
  206. GLushort* _indices;
  207. GLuint _VAOname;
  208. GLuint _buffersVBO[2]; //0: vertex 1: indices
  209. bool _dirty; //indicates whether or not the array buffer of the VBO needs to be updated
  210. /** quantity of quads that are going to be drawn */
  211. ssize_t _totalQuads;
  212. /** quantity of quads that can be stored with the current texture atlas size */
  213. ssize_t _capacity;
  214. /** Texture of the texture atlas */
  215. Texture2D* _texture;
  216. /** Quads that are going to be rendered */
  217. V3F_C4B_T2F_Quad* _quads;
  218. #if CC_ENABLE_CACHE_TEXTURE_DATA
  219. EventListenerCustom* _rendererRecreatedListener;
  220. #endif
  221. };
  222. // end of textures group
  223. /// @}
  224. NS_CC_END
  225. #endif //__CCTEXTURE_ATLAS_H__