2010-07-22 09:39:15 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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__
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To create sprite frames and texture atlas, use this tool:
|
|
|
|
* http://zwoptex.zwopple.com/
|
|
|
|
*/
|
|
|
|
|
2010-09-10 14:08:25 +08:00
|
|
|
#include <string>
|
2010-07-22 09:39:15 +08:00
|
|
|
#include "CCSpriteFrame.h"
|
|
|
|
#include "CCTexture2D.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCMutableDictionary.h"
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-08-04 15:46:12 +08:00
|
|
|
namespace cocos2d {
|
2010-07-23 14:24:23 +08:00
|
|
|
class CCSprite;
|
2010-07-22 09:39:15 +08:00
|
|
|
|
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
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCSpriteFrameCache : public CCObject
|
2010-07-22 09:39:15 +08:00
|
|
|
{
|
|
|
|
public:
|
2010-09-04 12:02:52 +08:00
|
|
|
bool init(void);
|
2010-07-23 13:42:29 +08:00
|
|
|
~CCSpriteFrameCache(void);
|
|
|
|
|
|
|
|
/*Adds multiple Sprite Frames with a dictionary. The texture will be associated with the created sprite frames.
|
|
|
|
*/
|
2011-03-07 18:20:48 +08:00
|
|
|
void addSpriteFramesWithDictionary(CCDictionary<std::string, CCObject*> *pobDictionary, CCTexture2D *pobTexture);
|
2010-07-22 09:39:15 +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
|
|
|
|
* If you want to use another texture, you should use the addSpriteFramesWithFile:texture method.
|
|
|
|
*/
|
2010-07-22 15:08:30 +08:00
|
|
|
void addSpriteFramesWithFile(const char *pszPlist);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-12-27 10:26:56 +08:00
|
|
|
/** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames.
|
|
|
|
@since v0.99.5
|
|
|
|
*/
|
|
|
|
void addSpriteFramesWithFile(const char* plist, const char* textureFileName);
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames. */
|
2010-07-22 15:08:30 +08:00
|
|
|
void addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture);
|
2010-07-22 09:39:15 +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.
|
|
|
|
*/
|
2010-07-22 15:08:30 +08:00
|
|
|
void addSpriteFrame(CCSpriteFrame *pobFrame, const char *pszFrameName);
|
2010-07-22 09:39:15 +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.
|
|
|
|
*/
|
|
|
|
void removeSpriteFrames(void);
|
|
|
|
|
|
|
|
/** Removes unused sprite frames.
|
|
|
|
* Sprite Frames that have a retain count of 1 will be deleted.
|
2010-09-29 17:39:45 +08:00
|
|
|
* It is convenient to call this method after when starting a new Scene.
|
2010-07-22 09:39:15 +08:00
|
|
|
*/
|
|
|
|
void removeUnusedSpriteFrames(void);
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Deletes an sprite frame from the sprite frame cache. */
|
2010-07-22 15:08:30 +08:00
|
|
|
void removeSpriteFrameByName(const char *pszName);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-12-27 10:26:56 +08:00
|
|
|
/** Removes multiple Sprite Frames from a plist file.
|
|
|
|
* Sprite Frames stored in this file will be removed.
|
|
|
|
* It is convinient to call this method when a specific texture needs to be removed.
|
|
|
|
* @since v0.99.5
|
|
|
|
*/
|
|
|
|
void removeSpriteFramesFromFile(const char* plist);
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
/** Removes multiple Sprite Frames from CCDictionary.
|
2010-12-27 10:26:56 +08:00
|
|
|
* @since v0.99.5
|
|
|
|
*/
|
2011-03-07 18:20:48 +08:00
|
|
|
void removeSpriteFramesFromDictionary(CCDictionary<std::string, CCSpriteFrame*> *dictionary);
|
2010-12-27 10:26:56 +08:00
|
|
|
|
|
|
|
/** Removes all Sprite Frames associated with the specified textures.
|
|
|
|
* It is convinient to call this method when a specific texture needs to be removed.
|
|
|
|
* @since v0.995.
|
|
|
|
*/
|
|
|
|
void removeSpriteFramesFromTexture(CCTexture2D* texture);
|
|
|
|
|
2010-07-22 09:39:15 +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.
|
|
|
|
*/
|
2010-07-22 15:08:30 +08:00
|
|
|
CCSpriteFrame* spriteFrameByName(const char *pszName);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
/** Creates an sprite with the name of an sprite frame.
|
|
|
|
The created sprite will contain the texture, rect and offset of the sprite frame.
|
|
|
|
It returns an autorelease object.
|
2010-09-29 17:39:45 +08:00
|
|
|
@deprecated use CCSprite::spriteWithSpriteFrameName(name). This method will be removed on final v0.9
|
2010-07-22 09:39:15 +08:00
|
|
|
*/
|
2010-07-22 15:08:30 +08:00
|
|
|
CCSprite* createSpriteWithFrameName(const char *pszName);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
public:
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Returns the shared instance of the Sprite Frame cache */
|
2010-07-22 09:39:15 +08:00
|
|
|
static CCSpriteFrameCache* sharedSpriteFrameCache(void);
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Purges the cache. It releases all the Sprite Frames and the retained instance. */
|
2010-07-22 09:39:15 +08:00
|
|
|
static void purgeSharedSpriteFrameCache(void);
|
|
|
|
|
2010-07-23 13:42:29 +08:00
|
|
|
private:
|
|
|
|
CCSpriteFrameCache(void) {}
|
2011-03-07 18:20:48 +08:00
|
|
|
const char * valueForKey(const char *key, CCDictionary<std::string, CCObject*> *dict);
|
2010-09-10 14:08:25 +08:00
|
|
|
|
2010-07-22 09:39:15 +08:00
|
|
|
protected:
|
2011-03-07 18:20:48 +08:00
|
|
|
CCDictionary<std::string, CCSpriteFrame*> *m_pSpriteFrames;
|
|
|
|
CCDictionary<std::string, CCSpriteFrame*> *m_pSpriteFramesAliases;
|
2010-07-22 09:39:15 +08:00
|
|
|
};
|
2010-08-04 15:46:12 +08:00
|
|
|
}//namespace cocos2d
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
#endif // __SPRITE_CCSPRITE_FRAME_CACHE_H__
|