2011-03-19 10:59:01 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-05 10:47:25 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2011-03-19 10:59:01 +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 __CCTEXTURE_CACHE_H__
|
|
|
|
#define __CCTEXTURE_CACHE_H__
|
|
|
|
|
|
|
|
#include <string>
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCMutableDictionary.h"
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2011-04-06 16:29:58 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTTURE_DATA
|
|
|
|
#include "CCImage.h"
|
|
|
|
#include <list>
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
namespace cocos2d {
|
2010-07-16 14:15:06 +08:00
|
|
|
class CCTexture2D;
|
2010-07-16 16:28:11 +08:00
|
|
|
class CCAsyncObject;
|
2011-03-09 14:40:05 +08:00
|
|
|
class CCLock;
|
2011-03-07 17:11:57 +08:00
|
|
|
class CCImage;
|
2010-08-04 15:46:12 +08:00
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
typedef void (*fpAsyncCallback)(CCTexture2D*, void*);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief Singleton that handles the loading of textures
|
2010-07-16 14:15:06 +08:00
|
|
|
* Once the texture is loaded, the next time it will return
|
|
|
|
* a reference of the previously loaded texture reducing GPU & CPU memory
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCTextureCache : public CCObject
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
2010-07-16 16:28:11 +08:00
|
|
|
protected:
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableDictionary<std::string, CCTexture2D*> * m_pTextures;
|
2011-03-09 14:40:05 +08:00
|
|
|
CCLock *m_pDictLock;
|
|
|
|
CCLock *m_pContextLock;
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
private:
|
2010-09-28 12:14:12 +08:00
|
|
|
// @todo void addImageWithAsyncObject(CCAsyncObject* async);
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
CCTextureCache();
|
2010-08-06 16:05:19 +08:00
|
|
|
virtual ~CCTextureCache();
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2010-08-06 16:05:19 +08:00
|
|
|
char * description(void);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
|
|
|
/** Retruns ths shared instance of the cache */
|
|
|
|
static CCTextureCache * sharedTextureCache();
|
|
|
|
|
|
|
|
/** purges the cache. It releases the retained instance.
|
|
|
|
@since v0.99.0
|
|
|
|
*/
|
|
|
|
static void purgeSharedTextureCache();
|
|
|
|
|
|
|
|
/** Returns a Texture2D object given an file image
|
|
|
|
* If the file image was not previously loaded, it will create a new CCTexture2D
|
|
|
|
* object and it will return it. It will use the filename as a key.
|
|
|
|
* Otherwise it will return a reference of a previosly loaded image.
|
|
|
|
* Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif
|
|
|
|
*/
|
2010-09-08 09:28:28 +08:00
|
|
|
CCTexture2D* addImage(const char* fileimage);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-09-28 12:14:12 +08:00
|
|
|
/* Returns a Texture2D object given a file image
|
2010-07-16 14:15:06 +08:00
|
|
|
* If the file image was not previously loaded, it will create a new CCTexture2D object and it will return it.
|
|
|
|
* Otherwise it will load a texture in a new thread, and when the image is loaded, the callback will be called with the Texture2D as a parameter.
|
|
|
|
* The callback will be called from the main thread, so it is safe to create any cocos2d object from the callback.
|
2010-09-28 12:14:12 +08:00
|
|
|
* Supported image extensions: .png, .jpg
|
2010-07-16 14:15:06 +08:00
|
|
|
* @since v0.8
|
|
|
|
*/
|
2010-09-28 12:14:12 +08:00
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
// @todo void addImageAsync(const char* filename, CCObject*target, fpAsyncCallback func);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
2010-09-28 12:14:12 +08:00
|
|
|
/* Returns a Texture2D object given an CGImageRef image
|
2010-07-16 14:15:06 +08:00
|
|
|
* If the image was not previously loaded, it will create a new CCTexture2D object and it will return it.
|
|
|
|
* Otherwise it will return a reference of a previously loaded image
|
|
|
|
* The "key" parameter will be used as the "key" for the cache.
|
|
|
|
* If "key" is nil, then a new texture will be created each time.
|
|
|
|
* @since v0.8
|
|
|
|
*/
|
2010-09-28 12:14:12 +08:00
|
|
|
// @todo CGImageRef CCTexture2D* addCGImage(CGImageRef image, string & key);
|
2010-08-26 14:06:41 +08:00
|
|
|
/** Returns a Texture2D object given an UIImage image
|
|
|
|
* If the image was not previously loaded, it will create a new CCTexture2D object and it will return it.
|
|
|
|
* Otherwise it will return a reference of a previously loaded image
|
|
|
|
* The "key" parameter will be used as the "key" for the cache.
|
|
|
|
* If "key" is nil, then a new texture will be created each time.
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
CCTexture2D* addUIImage(CCImage *image, const char *key);
|
2010-12-30 17:30:11 +08:00
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
/** Returns an already created texture. Returns nil if the texture doesn't exist.
|
|
|
|
@since v0.99.5
|
2010-12-30 17:30:11 +08:00
|
|
|
*/
|
|
|
|
CCTexture2D* textureForKey(const char* key);
|
2010-07-16 14:15:06 +08:00
|
|
|
/** Purges the dictionary of loaded textures.
|
|
|
|
* 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 removeAllTextures();
|
|
|
|
|
|
|
|
/** Removes unused textures
|
|
|
|
* Textures that have a retain count of 1 will be deleted
|
|
|
|
* It is convinient to call this method after when starting a new Scene
|
|
|
|
* @since v0.8
|
|
|
|
*/
|
|
|
|
void removeUnusedTextures();
|
|
|
|
|
|
|
|
/** Deletes a texture from the cache given a texture
|
|
|
|
*/
|
2010-08-06 14:32:32 +08:00
|
|
|
void removeTexture(CCTexture2D* texture);
|
2010-07-16 14:15:06 +08:00
|
|
|
|
|
|
|
/** Deletes a texture from the cache given a its key name
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2010-12-30 17:30:11 +08:00
|
|
|
void removeTextureForKey(const char *textureKeyName);
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
/** Output to CCLOG the current contents of this CCTextureCache
|
|
|
|
* This will attempt to calculate the size of each texture, and the total texture memory in use
|
|
|
|
*
|
|
|
|
* @since v1.0
|
2011-07-05 10:47:25 +08:00
|
|
|
*/
|
|
|
|
void dumpCachedTextureInfo();
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
#ifdef CC_SUPPORT_PVRTC
|
2010-12-30 17:30:11 +08:00
|
|
|
/** Returns a Texture2D object given an PVRTC RAW filename
|
|
|
|
* If the file image was not previously loaded, it will create a new CCTexture2D
|
|
|
|
* object and it will return it. Otherwise it will return a reference of a previosly loaded image
|
|
|
|
*
|
|
|
|
* It can only load square images: width == height, and it must be a power of 2 (128,256,512...)
|
|
|
|
* bpp can only be 2 or 4. 2 means more compression but lower quality.
|
|
|
|
* hasAlpha: whether or not the image contains alpha channel
|
|
|
|
*/
|
|
|
|
CCTexture2D* addPVRTCImage(const char* fileimage, int bpp, bool hasAlpha, int width);
|
2011-07-19 15:14:59 +08:00
|
|
|
#endif // CC_SUPPORT_PVRTC
|
|
|
|
|
|
|
|
/** Returns a Texture2D object given an PVR filename
|
2010-12-30 17:30:11 +08:00
|
|
|
* If the file image was not previously loaded, it will create a new CCTexture2D
|
|
|
|
* object and it will return it. Otherwise it will return a reference of a previosly loaded image
|
|
|
|
*/
|
2011-07-19 15:14:59 +08:00
|
|
|
CCTexture2D* addPVRImage(const char* filename);
|
2011-04-06 16:29:58 +08:00
|
|
|
|
|
|
|
/** Reload all textures
|
|
|
|
It's only useful when the value of CC_ENABLE_CACHE_TEXTTURE_DATA is 1
|
|
|
|
*/
|
|
|
|
static void reloadAllTextures();
|
|
|
|
};
|
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTTURE_DATA
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
class VolatileTexture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VolatileTexture(CCTexture2D *t);
|
|
|
|
~VolatileTexture();
|
|
|
|
|
|
|
|
static void addImageTexture(CCTexture2D *tt, const char* imageFileName, CCImage::EImageFormat format);
|
|
|
|
static void addStringTexture(CCTexture2D *tt, const char* text, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
|
|
|
|
|
|
|
|
static void removeTexture(CCTexture2D *t);
|
|
|
|
static void reloadAllTextures();
|
|
|
|
|
|
|
|
public:
|
|
|
|
static std::list<VolatileTexture*> textures;
|
|
|
|
static bool isReloading;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
CCTexture2D *texture;
|
2011-04-06 16:29:58 +08:00
|
|
|
|
|
|
|
bool m_bIsString;
|
|
|
|
|
|
|
|
std::string m_strFileName;
|
|
|
|
CCImage::EImageFormat m_FmtImage;
|
|
|
|
|
|
|
|
CCSize m_size;
|
|
|
|
CCTextAlignment m_alignment;
|
|
|
|
std::string m_strFontName;
|
|
|
|
std::string m_strText;
|
|
|
|
float m_fFontSize;
|
2011-03-19 10:59:01 +08:00
|
|
|
};
|
2011-04-06 16:29:58 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
}//namespace cocos2d
|
|
|
|
|
|
|
|
#endif //__CCTEXTURE_CACHE_H__
|
|
|
|
|