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.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 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__
|
|
|
|
|
2010-07-22 09:39:15 +08:00
|
|
|
/*
|
|
|
|
* To create sprite frames and texture atlas, use this tool:
|
|
|
|
* http://zwoptex.zwopple.com/
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrame.h"
|
|
|
|
#include "2d/CCTexture2D.h"
|
|
|
|
#include "base/CCRef.h"
|
2013-12-03 16:20:41 +08:00
|
|
|
#include "CCValue.h"
|
|
|
|
#include "CCMap.h"
|
2014-02-20 10:53:49 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
#include <set>
|
2012-06-19 13:50:11 +08:00
|
|
|
#include <string>
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class Sprite;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup sprite_nodes
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-09-28 18:27:33 +08:00
|
|
|
/** @brief Singleton that handles the loading of the sprite frames.
|
2010-07-22 09:39:15 +08:00
|
|
|
It saves in a cache the sprite frames.
|
|
|
|
@since v0.9
|
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:
|
|
|
|
/** Returns the shared instance of the Sprite Frame cache */
|
|
|
|
static SpriteFrameCache* getInstance(void);
|
|
|
|
|
|
|
|
/** @deprecated Use getInstance() instead */
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static SpriteFrameCache* sharedSpriteFrameCache() { return SpriteFrameCache::getInstance(); }
|
|
|
|
|
|
|
|
/** Destroys the cache. It releases all the Sprite Frames and the retained instance. */
|
|
|
|
static void destroyInstance();
|
|
|
|
|
|
|
|
/** @deprecated Use destroyInstance() instead */
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static void purgeSharedSpriteFrameCache() { return SpriteFrameCache::destroyInstance(); }
|
|
|
|
|
2012-10-19 23:38:58 +08:00
|
|
|
protected:
|
|
|
|
// MARMALADE: Made this protected not private, as deriving from this class is pretty useful
|
2013-12-04 10:33:27 +08:00
|
|
|
SpriteFrameCache(){}
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
public:
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
virtual ~SpriteFrameCache();
|
2012-04-19 14:35:52 +08:00
|
|
|
bool init(void);
|
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Adds multiple Sprite Frames from a plist file.
|
|
|
|
* 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
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void addSpriteFramesWithFile(const std::string&plist, 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.
|
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void addSpriteFrame(SpriteFrame *frame, const std::string& frameName);
|
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.
|
|
|
|
*/
|
2013-12-03 16:20:41 +08:00
|
|
|
void removeUnusedSpriteFrames();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Deletes an sprite frame from the sprite frame cache. */
|
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
|
|
|
|
*/
|
2013-11-06 11:02:03 +08:00
|
|
|
void removeSpriteFramesFromFile(const std::string& plist);
|
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.
|
|
|
|
*/
|
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
|
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
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
private:
|
|
|
|
/*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);
|
2013-07-12 06:24:23 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
protected:
|
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
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of sprite_nodes group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __SPRITE_CCSPRITE_FRAME_CACHE_H__
|