axmol/cocos2dx/include/CCSpriteFrameCache.h

141 lines
5.5 KiB
C
Raw Normal View History

/****************************************************************************
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/
*/
#include <string>
#include "CCSpriteFrame.h"
#include "CCTexture2D.h"
#include "CCObject.h"
#include "CCMutableDictionary.h"
2010-08-04 15:46:12 +08:00
namespace cocos2d {
2010-07-23 14:24:23 +08:00
class CCSprite;
2010-09-28 18:27:33 +08:00
/** @brief Singleton that handles the loading of the sprite frames.
It saves in a cache the sprite frames.
@since v0.9
*/
class CC_DLL CCSpriteFrameCache : public CCObject
{
public:
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);
/** 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.
*/
void addSpriteFramesWithFile(const char *pszPlist);
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. */
void addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture);
/** 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.
*/
void addSpriteFrame(CCSpriteFrame *pobFrame, const char *pszFrameName);
/** 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.
*/
void removeUnusedSpriteFrames(void);
2010-09-28 16:18:05 +08:00
/** Deletes an sprite frame from the sprite frame cache. */
void removeSpriteFrameByName(const char *pszName);
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);
/** 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.
*/
CCSpriteFrame* spriteFrameByName(const char *pszName);
/** 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
*/
CCSprite* createSpriteWithFrameName(const char *pszName);
public:
2010-09-28 16:18:05 +08:00
/** Returns the shared instance of the Sprite Frame cache */
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. */
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);
protected:
2011-03-07 18:20:48 +08:00
CCDictionary<std::string, CCSpriteFrame*> *m_pSpriteFrames;
CCDictionary<std::string, CCSpriteFrame*> *m_pSpriteFramesAliases;
};
2010-08-04 15:46:12 +08:00
}//namespace cocos2d
#endif // __SPRITE_CCSPRITE_FRAME_CACHE_H__