2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2009 Jason Booth
|
|
|
|
Copyright (c) 2009 Robert J Payne
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __SPRITE_CCSPRITE_FRAME_CACHE_H__
|
|
|
|
#define __SPRITE_CCSPRITE_FRAME_CACHE_H__
|
|
|
|
|
2014-08-28 17:03:29 +08:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrame.h"
|
|
|
|
#include "base/CCRef.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCValue.h"
|
|
|
|
#include "base/CCMap.h"
|
2014-02-20 10:53:49 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class Sprite;
|
2014-08-28 17:03:29 +08:00
|
|
|
class Texture2D;
|
2015-09-29 21:42:04 +08:00
|
|
|
class PolygonInfo;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
2015-03-24 10:34:41 +08:00
|
|
|
* @addtogroup _2d
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-03-18 20:40:29 +08:00
|
|
|
/** @class SpriteFrameCache
|
|
|
|
* @brief Singleton that handles the loading of the sprite frames.
|
2015-10-01 16:36:18 +08:00
|
|
|
|
|
|
|
The SpriteFrameCache loads SpriteFrames from a .plist file.
|
|
|
|
A SpriteFrame contains information about how to use a sprite
|
|
|
|
located in a sprite sheet.
|
|
|
|
|
|
|
|
The .plist file contains the following elements:
|
|
|
|
|
|
|
|
- `frames`:
|
|
|
|
Dictionary of sprites. Key is the sprite's name, value a dict containing the sprite frame data.
|
|
|
|
A sprite frame consists of the following values:
|
|
|
|
- `spriteOffset`: difference vector between the original sprite's center and the center of the trimmed sprite
|
|
|
|
- `spriteSize`: size of the trimmed sprite
|
|
|
|
- `spriteSourceSize`: size of the original sprite
|
|
|
|
- `textureRect`: the position of the sprite in the sprite sheet
|
|
|
|
- `textureRotated`: true if the sprite is rotated clockwise
|
2015-12-15 17:56:10 +08:00
|
|
|
- `anchor`: anchor point in normalized coordinates (optional)
|
2015-10-01 16:36:18 +08:00
|
|
|
Optional values when using polygon outlines
|
|
|
|
- `triangles`: 3 indices per triangle, pointing to vertices and verticesUV coordinates
|
|
|
|
- `vertices`: vertices in sprite coordinates, each vertex consists of a pair of x and y coordinates
|
|
|
|
- `verticesUV`: vertices in the sprite sheet, each vertex consists of a pair of x and y coordinates
|
|
|
|
|
|
|
|
- `metadata`:
|
|
|
|
Dictionary containing additional information about the sprite sheet:
|
|
|
|
- `format`: plist file format, currently 3
|
|
|
|
- `size`: size of the texture (optional)
|
|
|
|
- `textureFileName`: name of the texture's image file
|
|
|
|
|
|
|
|
Use one of the following tools to create the .plist file and sprite sheet:
|
|
|
|
- [TexturePacker](https://www.codeandweb.com/texturepacker/cocos2d)
|
|
|
|
- [Zwoptex](https://zwopple.com/zwoptex/)
|
|
|
|
|
2010-07-22 09:39:15 +08:00
|
|
|
@since v0.9
|
2015-03-18 22:24:03 +08:00
|
|
|
@js cc.spriteFrameCache
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
class CC_DLL SpriteFrameCache : public Ref
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-18 07:56:19 +08:00
|
|
|
public:
|
2015-03-18 20:40:29 +08:00
|
|
|
/** Returns the shared instance of the Sprite Frame cache.
|
|
|
|
*
|
|
|
|
* @return The instance of the Sprite Frame Cache.
|
2015-03-19 21:44:44 +08:00
|
|
|
* @js NA
|
2015-03-18 20:40:29 +08:00
|
|
|
*/
|
2014-05-30 15:13:59 +08:00
|
|
|
static SpriteFrameCache* getInstance();
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2015-10-01 16:36:18 +08:00
|
|
|
/** @deprecated Use getInstance() instead
|
|
|
|
@js NA
|
2015-03-18 22:24:03 +08:00
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static SpriteFrameCache* sharedSpriteFrameCache() { return SpriteFrameCache::getInstance(); }
|
|
|
|
|
2015-03-18 20:40:29 +08:00
|
|
|
/** Destroys the cache. It releases all the Sprite Frames and the retained instance.
|
2015-03-18 22:24:03 +08:00
|
|
|
* @js NA
|
2015-03-18 20:40:29 +08:00
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
static void destroyInstance();
|
|
|
|
|
2015-10-01 16:36:18 +08:00
|
|
|
/** @deprecated Use destroyInstance() instead
|
2015-03-19 21:00:33 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static void purgeSharedSpriteFrameCache() { return SpriteFrameCache::destroyInstance(); }
|
|
|
|
|
2015-03-18 20:40:29 +08:00
|
|
|
/** Destructor.
|
2013-09-13 13:52:42 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
virtual ~SpriteFrameCache();
|
2015-03-18 20:40:29 +08:00
|
|
|
|
|
|
|
/** Initialize method.
|
|
|
|
*
|
|
|
|
* @return if success return true.
|
|
|
|
*/
|
2014-05-30 15:13:59 +08:00
|
|
|
bool init();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Adds multiple Sprite Frames from a plist file.
|
2015-03-18 20:40:29 +08:00
|
|
|
* A texture will be loaded automatically. The texture name will composed by replacing the .plist suffix with .png.
|
2013-11-06 11:02:03 +08:00
|
|
|
* If you want to use another texture, you should use the addSpriteFramesWithFile(const std::string& plist, const std::string& textureFileName) method.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js addSpriteFrames
|
|
|
|
* @lua addSpriteFrames
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param plist Plist file name.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void addSpriteFramesWithFile(const std::string& plist);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames.
|
2013-09-13 11:41:20 +08:00
|
|
|
@since v0.99.5
|
|
|
|
* @js addSpriteFrames
|
|
|
|
* @lua addSpriteFrames
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param plist Plist file name.
|
|
|
|
* @param textureFileName Texture file name.
|
2013-09-13 11:41:20 +08:00
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void addSpriteFramesWithFile(const std::string& plist, const std::string& textureFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames.
|
|
|
|
* @js addSpriteFrames
|
|
|
|
* @lua addSpriteFrames
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param plist Plist file name.
|
|
|
|
* @param texture Texture pointer.
|
2013-09-13 11:41:20 +08:00
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void addSpriteFramesWithFile(const std::string&plist, Texture2D *texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-07-29 16:58:33 +08:00
|
|
|
/** Adds multiple Sprite Frames from a plist file content. The texture will be associated with the created sprite frames.
|
2015-03-18 22:24:03 +08:00
|
|
|
* @js NA
|
2014-07-29 16:58:33 +08:00
|
|
|
* @lua addSpriteFrames
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param plist_content Plist file content string.
|
|
|
|
* @param texture Texture pointer.
|
2014-07-29 16:58:33 +08:00
|
|
|
*/
|
|
|
|
void addSpriteFramesWithFileContent(const std::string& plist_content, Texture2D *texture);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Adds an sprite frame with a given name.
|
|
|
|
If the name already exists, then the contents of the old name will be replaced with the new one.
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param frame A certain sprite frame.
|
|
|
|
* @param frameName The name of the sprite frame.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void addSpriteFrame(SpriteFrame *frame, const std::string& frameName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-04-23 16:50:28 +08:00
|
|
|
/** Check if multiple Sprite Frames from a plist file have been loaded.
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*
|
|
|
|
* @param plist Plist file name.
|
|
|
|
* @return True if the file is loaded.
|
|
|
|
*/
|
2015-04-24 11:40:15 +08:00
|
|
|
bool isSpriteFramesWithFileLoaded(const std::string& plist) const;
|
2015-04-23 16:50:28 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Purges the dictionary of loaded sprite frames.
|
|
|
|
* Call this method if you receive the "Memory Warning".
|
|
|
|
* In the short term: it will free some resources preventing your app from being killed.
|
|
|
|
* In the medium term: it will allocate more resources.
|
|
|
|
* In the long term: it will be the same.
|
|
|
|
*/
|
2013-12-03 16:20:41 +08:00
|
|
|
void removeSpriteFrames();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Removes unused sprite frames.
|
|
|
|
* Sprite Frames that have a retain count of 1 will be deleted.
|
|
|
|
* It is convenient to call this method after when starting a new Scene.
|
2015-03-18 22:24:03 +08:00
|
|
|
* @js NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-12-03 16:20:41 +08:00
|
|
|
void removeUnusedSpriteFrames();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-18 20:40:29 +08:00
|
|
|
/** Deletes an sprite frame from the sprite frame cache.
|
|
|
|
*
|
|
|
|
* @param name The name of the sprite frame that needs to removed.
|
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void removeSpriteFrameByName(const std::string& name);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Removes multiple Sprite Frames from a plist file.
|
|
|
|
* Sprite Frames stored in this file will be removed.
|
2012-09-17 15:02:24 +08:00
|
|
|
* It is convenient to call this method when a specific texture needs to be removed.
|
2012-04-19 14:35:52 +08:00
|
|
|
* @since v0.99.5
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param plist The name of the plist that needs to removed.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void removeSpriteFramesFromFile(const std::string& plist);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-07-29 16:58:33 +08:00
|
|
|
/** Removes multiple Sprite Frames from a plist file content.
|
|
|
|
* Sprite Frames stored in this file will be removed.
|
|
|
|
* It is convenient to call this method when a specific texture needs to be removed.
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param plist_content The string of the plist content that needs to removed.
|
2015-03-18 22:24:03 +08:00
|
|
|
* @js NA
|
2014-07-29 16:58:33 +08:00
|
|
|
*/
|
|
|
|
void removeSpriteFramesFromFileContent(const std::string& plist_content);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Removes all Sprite Frames associated with the specified textures.
|
2013-07-18 07:56:19 +08:00
|
|
|
* It is convenient to call this method when a specific texture needs to be removed.
|
|
|
|
* @since v0.995.
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param texture The texture that needs to removed.
|
2013-07-18 07:56:19 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeSpriteFramesFromTexture(Texture2D* texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Returns an Sprite Frame that was previously added.
|
|
|
|
If the name is not found it will return nil.
|
|
|
|
You should retain the returned copy if you are going to use it.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js getSpriteFrame
|
|
|
|
* @lua getSpriteFrame
|
2015-03-18 20:40:29 +08:00
|
|
|
*
|
|
|
|
* @param name A certain sprite frame name.
|
|
|
|
* @return The sprite frame.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
SpriteFrame* getSpriteFrameByName(const std::string& name);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
/** @deprecated use getSpriteFrameByName() instead */
|
2013-11-06 11:02:03 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE SpriteFrame* spriteFrameByName(const std::string&name) { return getSpriteFrameByName(name); }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-11-12 09:49:49 +08:00
|
|
|
bool reloadTexture(const std::string& plist);
|
|
|
|
|
2014-11-26 09:53:52 +08:00
|
|
|
protected:
|
|
|
|
// MARMALADE: Made this protected not private, as deriving from this class is pretty useful
|
|
|
|
SpriteFrameCache(){}
|
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
/*Adds multiple Sprite Frames with a dictionary. The texture will be associated with the created sprite frames.
|
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
void addSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D *texture);
|
2016-02-11 20:47:23 +08:00
|
|
|
|
|
|
|
/*Adds multiple Sprite Frames with a dictionary. The texture will be associated with the created sprite frames.
|
|
|
|
*/
|
|
|
|
void addSpriteFramesWithDictionary(ValueMap& dictionary, const std::string &texturePath);
|
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
/** Removes multiple Sprite Frames from Dictionary.
|
|
|
|
* @since v0.99.5
|
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
void removeSpriteFramesFromDictionary(ValueMap& dictionary);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-09-29 21:42:04 +08:00
|
|
|
/** Parses list of space-separated integers */
|
2015-10-26 16:36:01 +08:00
|
|
|
void parseIntegerList(const std::string &string, std::vector<int> &res);
|
2015-09-29 21:42:04 +08:00
|
|
|
|
|
|
|
/** Configures PolygonInfo class with the passed sizes + triangles */
|
|
|
|
void initializePolygonInfo(const Size &textureSize,
|
|
|
|
const Size &spriteSize,
|
|
|
|
const std::vector<int> &vertices,
|
|
|
|
const std::vector<int> &verticesUV,
|
|
|
|
const std::vector<int> &triangleIndices,
|
|
|
|
PolygonInfo &polygonInfo);
|
2014-11-26 09:53:52 +08:00
|
|
|
|
2015-11-12 09:49:49 +08:00
|
|
|
void reloadSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D *texture);
|
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
Map<std::string, SpriteFrame*> _spriteFrames;
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap _spriteFramesAliases;
|
2013-06-15 14:03:30 +08:00
|
|
|
std::set<std::string>* _loadedFileNames;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2015-03-24 10:34:41 +08:00
|
|
|
// end of _2d group
|
2012-06-20 18:09:11 +08:00
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __SPRITE_CCSPRITE_FRAME_CACHE_H__
|