CCSpriteFrameCache.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2009 Jason Booth
  4. Copyright (c) 2009 Robert J Payne
  5. Copyright (c) 2010-2012 cocos2d-x.org
  6. Copyright (c) 2011 Zynga Inc.
  7. Copyright (c) 2013-2016 Chukong Technologies Inc.
  8. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  9. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. ****************************************************************************/
  26. #ifndef __SPRITE_CCSPRITE_FRAME_CACHE_H__
  27. #define __SPRITE_CCSPRITE_FRAME_CACHE_H__
  28. #include <set>
  29. #include <string>
  30. #include "2d/CCSpriteFrame.h"
  31. #include "base/CCRef.h"
  32. #include "base/CCValue.h"
  33. #include "base/CCMap.h"
  34. NS_CC_BEGIN
  35. class Sprite;
  36. class Texture2D;
  37. class PolygonInfo;
  38. /**
  39. * @addtogroup _2d
  40. * @{
  41. */
  42. /** @class SpriteFrameCache
  43. * @brief Singleton that handles the loading of the sprite frames.
  44. The SpriteFrameCache loads SpriteFrames from a .plist file.
  45. A SpriteFrame contains information about how to use a sprite
  46. located in a sprite sheet.
  47. The .plist file contains the following elements:
  48. - `frames`:
  49. Dictionary of sprites. Key is the sprite's name, value a dict containing the sprite frame data.
  50. A sprite frame consists of the following values:
  51. - `spriteOffset`: difference vector between the original sprite's center and the center of the trimmed sprite
  52. - `spriteSize`: size of the trimmed sprite
  53. - `spriteSourceSize`: size of the original sprite
  54. - `textureRect`: the position of the sprite in the sprite sheet
  55. - `textureRotated`: true if the sprite is rotated clockwise
  56. - `anchor`: anchor point in normalized coordinates (optional)
  57. Optional values when using polygon outlines
  58. - `triangles`: 3 indices per triangle, pointing to vertices and verticesUV coordinates
  59. - `vertices`: vertices in sprite coordinates, each vertex consists of a pair of x and y coordinates
  60. - `verticesUV`: vertices in the sprite sheet, each vertex consists of a pair of x and y coordinates
  61. - `metadata`:
  62. Dictionary containing additional information about the sprite sheet:
  63. - `format`: plist file format, currently 3
  64. - `size`: size of the texture (optional)
  65. - `textureFileName`: name of the texture's image file
  66. Use one of the following tools to create the .plist file and sprite sheet:
  67. - [TexturePacker](https://www.codeandweb.com/texturepacker/cocos2d)
  68. - [Zwoptex](https://zwopple.com/zwoptex/)
  69. @since v0.9
  70. @js cc.spriteFrameCache
  71. */
  72. class CC_DLL SpriteFrameCache : public Ref
  73. {
  74. public:
  75. /** Returns the shared instance of the Sprite Frame cache.
  76. *
  77. * @return The instance of the Sprite Frame Cache.
  78. * @js NA
  79. */
  80. static SpriteFrameCache* getInstance();
  81. /** @deprecated Use getInstance() instead
  82. @js NA
  83. */
  84. CC_DEPRECATED_ATTRIBUTE static SpriteFrameCache* sharedSpriteFrameCache() { return SpriteFrameCache::getInstance(); }
  85. /** Destroys the cache. It releases all the Sprite Frames and the retained instance.
  86. * @js NA
  87. */
  88. static void destroyInstance();
  89. /** @deprecated Use destroyInstance() instead
  90. * @js NA
  91. */
  92. CC_DEPRECATED_ATTRIBUTE static void purgeSharedSpriteFrameCache() { return SpriteFrameCache::destroyInstance(); }
  93. /** Destructor.
  94. * @js NA
  95. * @lua NA
  96. */
  97. virtual ~SpriteFrameCache();
  98. /** Initialize method.
  99. *
  100. * @return if success return true.
  101. */
  102. bool init();
  103. /** Adds multiple Sprite Frames from a plist file.
  104. * A texture will be loaded automatically. The texture name will composed by replacing the .plist suffix with .png.
  105. * If you want to use another texture, you should use the addSpriteFramesWithFile(const std::string& plist, const std::string& textureFileName) method.
  106. * @js addSpriteFrames
  107. * @lua addSpriteFrames
  108. *
  109. * @param plist Plist file name.
  110. */
  111. void addSpriteFramesWithFile(const std::string& plist);
  112. /** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames.
  113. @since v0.99.5
  114. * @js addSpriteFrames
  115. * @lua addSpriteFrames
  116. *
  117. * @param plist Plist file name.
  118. * @param textureFileName Texture file name.
  119. */
  120. void addSpriteFramesWithFile(const std::string& plist, const std::string& textureFileName);
  121. /** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames.
  122. * @js addSpriteFrames
  123. * @lua addSpriteFrames
  124. *
  125. * @param plist Plist file name.
  126. * @param texture Texture pointer.
  127. */
  128. void addSpriteFramesWithFile(const std::string&plist, Texture2D *texture);
  129. /** Adds multiple Sprite Frames from a plist file content. The texture will be associated with the created sprite frames.
  130. * @js NA
  131. * @lua addSpriteFrames
  132. *
  133. * @param plist_content Plist file content string.
  134. * @param texture Texture pointer.
  135. */
  136. void addSpriteFramesWithFileContent(const std::string& plist_content, Texture2D *texture);
  137. /** Adds an sprite frame with a given name.
  138. If the name already exists, then the contents of the old name will be replaced with the new one.
  139. *
  140. * @param frame A certain sprite frame.
  141. * @param frameName The name of the sprite frame.
  142. */
  143. void addSpriteFrame(SpriteFrame *frame, const std::string& frameName);
  144. /** Check if multiple Sprite Frames from a plist file have been loaded.
  145. * @js NA
  146. * @lua NA
  147. *
  148. * @param plist Plist file name.
  149. * @return True if the file is loaded.
  150. */
  151. bool isSpriteFramesWithFileLoaded(const std::string& plist) const;
  152. /** Purges the dictionary of loaded sprite frames.
  153. * Call this method if you receive the "Memory Warning".
  154. * In the short term: it will free some resources preventing your app from being killed.
  155. * In the medium term: it will allocate more resources.
  156. * In the long term: it will be the same.
  157. */
  158. void removeSpriteFrames();
  159. /** Removes unused sprite frames.
  160. * Sprite Frames that have a retain count of 1 will be deleted.
  161. * It is convenient to call this method after when starting a new Scene.
  162. * @js NA
  163. */
  164. void removeUnusedSpriteFrames();
  165. /** Deletes an sprite frame from the sprite frame cache.
  166. *
  167. * @param name The name of the sprite frame that needs to removed.
  168. */
  169. void removeSpriteFrameByName(const std::string& name);
  170. /** Removes multiple Sprite Frames from a plist file.
  171. * Sprite Frames stored in this file will be removed.
  172. * It is convenient to call this method when a specific texture needs to be removed.
  173. * @since v0.99.5
  174. *
  175. * @param plist The name of the plist that needs to removed.
  176. */
  177. void removeSpriteFramesFromFile(const std::string& plist);
  178. /** Removes multiple Sprite Frames from a plist file content.
  179. * Sprite Frames stored in this file will be removed.
  180. * It is convenient to call this method when a specific texture needs to be removed.
  181. *
  182. * @param plist_content The string of the plist content that needs to removed.
  183. * @js NA
  184. */
  185. void removeSpriteFramesFromFileContent(const std::string& plist_content);
  186. /** Removes all Sprite Frames associated with the specified textures.
  187. * It is convenient to call this method when a specific texture needs to be removed.
  188. * @since v0.995.
  189. *
  190. * @param texture The texture that needs to removed.
  191. */
  192. void removeSpriteFramesFromTexture(Texture2D* texture);
  193. /** Returns an Sprite Frame that was previously added.
  194. If the name is not found it will return nil.
  195. You should retain the returned copy if you are going to use it.
  196. * @js getSpriteFrame
  197. * @lua getSpriteFrame
  198. *
  199. * @param name A certain sprite frame name.
  200. * @return The sprite frame.
  201. */
  202. SpriteFrame* getSpriteFrameByName(const std::string& name);
  203. /** @deprecated use getSpriteFrameByName() instead */
  204. CC_DEPRECATED_ATTRIBUTE SpriteFrame* spriteFrameByName(const std::string&name) { return getSpriteFrameByName(name); }
  205. bool reloadTexture(const std::string& plist);
  206. protected:
  207. // MARMALADE: Made this protected not private, as deriving from this class is pretty useful
  208. SpriteFrameCache(){}
  209. /*Adds multiple Sprite Frames with a dictionary. The texture will be associated with the created sprite frames.
  210. */
  211. void addSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D *texture);
  212. /*Adds multiple Sprite Frames with a dictionary. The texture will be associated with the created sprite frames.
  213. */
  214. void addSpriteFramesWithDictionary(ValueMap& dictionary, const std::string &texturePath);
  215. /** Removes multiple Sprite Frames from Dictionary.
  216. * @since v0.99.5
  217. */
  218. void removeSpriteFramesFromDictionary(ValueMap& dictionary);
  219. /** Parses list of space-separated integers */
  220. void parseIntegerList(const std::string &string, std::vector<int> &res);
  221. /** Configures PolygonInfo class with the passed sizes + triangles */
  222. void initializePolygonInfo(const Size &textureSize,
  223. const Size &spriteSize,
  224. const std::vector<int> &vertices,
  225. const std::vector<int> &verticesUV,
  226. const std::vector<int> &triangleIndices,
  227. PolygonInfo &polygonInfo);
  228. void reloadSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D *texture);
  229. Map<std::string, SpriteFrame*> _spriteFrames;
  230. ValueMap _spriteFramesAliases;
  231. std::set<std::string>* _loadedFileNames;
  232. };
  233. // end of _2d group
  234. /// @}
  235. NS_CC_END
  236. #endif // __SPRITE_CCSPRITE_FRAME_CACHE_H__