您的位置:首页 > 其它

CCTextureCache(加载纹理到缓存)

2014-05-22 09:43 525 查看
1. 加载图片 方法

CCTexture2D* addImage(const char* fileimage);

同步加载一个图片,返回 一个 CCTexture2D对象。这个文件名还可以做 key. 后续可以调用

CCTexture2D* textureForKey(const char* key); 方法来获得这个文件名所对应的CCTexture2D对象,如果这个Key对应的图片不存在,那么就返回NULL.

2. 异步加载图片方法

void addImageAsync(const char *path, CCObject *target, SEL_CallFuncOselector);

加载之后会回调,一般用来程序启动的时候预加载图片资源。

例如: CCTextureCache::sharedTextureCache()->addImageAsync("Images/blocks.png", this,callfuncO_selector(TextureCacheTest::loadingCallBack));

3. 获得图片的方法

可以使用 CCTexture2D* textureForKey(const char*
key); 方法来获得这个文件名所对应的CCTexture2D对象,如果这个Key对应的图片不存在,那么就返回NULL.

也可以使用 CCTexture2D* addImage(const char* fileimage); 因为之前如果已经加载,那么会直接返回图片缓存,不会重新加载图片。

4. 图片缓存的释放

在退出界面的时候,可以调用 void removeAllTextures();

方法来将所以的缓存移除,防止资源占用过多。

void removeUnusedTextures(); 用来移除哪些暂时没用到的纹理图。 It
is convenient to call this method after when starting a new Scene . 意思是新开始一个场景界面的时候,可以调用很方便。

首先是最底层也最有效的纹理缓存CCTextureCache,这里缓存的是加载到内存中的纹理资源,也就是图片资源。其原理是对加入缓存的纹理资源进行一次引用,使其引用计数加一,保持不被清除,而Cocos2d-x的渲染机制是可以重复使用同一份纹理在不同的场合进行绘制,从而达到重复使用,降低内存和GPU运算资源的开销的目的。常用的是如下所示的3个接口:

在这3个接口中,CCTextureCache屏蔽了加载纹理的许多细节;addImage函数会返回一个纹理CCTexture2D的引用,可能是新加载到内存的,也可能是之前已经存在的;而removeUnusedTextures则会释放当前所有引用计数为1的纹理,即目前没有被使用的纹理。后面会看到,引用计数的内存管理方式为缓存的设计带来了很大的便利。

实际上,我们很少需要调用addImage这个接口,因为引擎内部所有的纹理加载都是通过这个缓存进行的,换句话说,载入的每一张图片都被缓存了,所以我们更需要关心什么时候清理缓存。引擎会在设备出现内存警告时自动清理缓存,但是这显然在很多情况下已经为时过晚了。一般情况下,我们应该在切换场景时清理缓存中的无用纹理,因为不同场景间使用的纹理是不同的。如果确实存在着共享的纹理,将其加入一个标记数组来保持其引用计数,以避免被清理了。

#ifndef __CCTEXTURE_CACHE_H__
#define __CCTEXTURE_CACHE_H__

#include "cocoa/CCObject.h"
#include "cocoa/CCDictionary.h"
#include "textures/CCTexture2D.h"
#include <string>

#if CC_ENABLE_CACHE_TEXTURE_DATA
#include "platform/CCImage.h"
#include <list>
#endif

NS_CC_BEGIN

class CCLock;
class CCImage;

/**
* @addtogroup textures
* @{
*/

/** @brief Singleton that handles the loading of textures
* Once the texture is loaded, the next time it will return
* a reference of the previously loaded texture reducing GPU & CPU memory
*/
class CC_DLL CCTextureCache : public CCObject
{
protected:
CCDictionary* m_pTextures;
//pthread_mutex_t *m_pDictLock;

private:
/// todo: void addImageWithAsyncObject(CCAsyncObject* async);
void addImageAsyncCallBack(float dt);

public:

CCTextureCache();
virtual ~CCTextureCache();

const char* description(void);

CCDictionary* snapshotTextures();

/** Returns the 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 previously loaded image.
* Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif
*/
CCTexture2D* addImage(const char* fileimage);

/* Returns a Texture2D object given a file image
* 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.
* Supported image extensions: .png, .jpg
* @since v0.8
*/

void addImageAsync(const char *path, CCObject *target, SEL_CallFuncO selector);

/* Returns a Texture2D object given an CGImageRef 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.
* @since v0.8
*/
// todo: CGImageRef CCTexture2D* addCGImage(CGImageRef image, string & key);
/** 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.
*/
CCTexture2D* addUIImage(CCImage *image, const char *key);

/** Returns an already created texture. Returns nil if the texture doesn't exist.
@since v0.99.5
*/
CCTexture2D* textureForKey(const char* key);
/** 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 convenient to call this method after when starting a new Scene
* @since v0.8
*/
void removeUnusedTextures();

/** Deletes a texture from the cache given a texture
*/
void removeTexture(CCTexture2D* texture);

/** Deletes a texture from the cache given a its key name
@since v0.99.4
*/
void removeTextureForKey(const char *textureKeyName);

/** 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
*/
void dumpCachedTextureInfo();

/** Returns a Texture2D object given an PVR 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 previously loaded image
*/
CCTexture2D* addPVRImage(const char* filename);

/** Returns a Texture2D object given an ETC 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 previously loaded image
*/
CCTexture2D* addETCImage(const char* filename);

/** Reload all textures
It's only useful when the value of CC_ENABLE_CACHE_TEXTURE_DATA is 1
*/
static void reloadAllTextures();
};

#if CC_ENABLE_CACHE_TEXTURE_DATA

class VolatileTexture
{
typedef enum {
kInvalid = 0,
kImageFile,
kImageData,
kString,
kImage,
}ccCachedImageType;

public:
VolatileTexture(CCTexture2D *t);
~VolatileTexture();

static void addImageTexture(CCTexture2D *tt, const char* imageFileName, CCImage::EImageFormat format);
static void addStringTexture(CCTexture2D *tt, const char* text, const CCSize& dimensions, CCTextAlignment alignment,
CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize);
static void addDataTexture(CCTexture2D *tt, void* data, CCTexture2DPixelFormat pixelFormat, const CCSize& contentSize);
static void addCCImage(CCTexture2D *tt, CCImage *image);

static void setTexParameters(CCTexture2D *t, ccTexParams *texParams);
static void removeTexture(CCTexture2D *t);
static void reloadAllTextures();

public:
static std::list<VolatileTexture*> textures;
static bool isReloading;

private:
// find VolatileTexture by CCTexture2D*
// if not found, create a new one
static VolatileTexture* findVolotileTexture(CCTexture2D *tt);

protected:
CCTexture2D *texture;

CCImage *uiImage;

ccCachedImageType m_eCashedImageType;

void *m_pTextureData;
CCSize m_TextureSize;
CCTexture2DPixelFormat m_PixelFormat;

std::string m_strFileName;
CCImage::EImageFormat m_FmtImage;

ccTexParams m_texParams;
CCSize m_size;
CCTextAlignment m_alignment;
CCVerticalTextAlignment m_vAlignment;
std::string m_strFontName;
std::string m_strText;
float m_fFontSize;
};

#endif

// end of textures group
/// @}

NS_CC_END

#endif //__CCTEXTURE_CACHE_H__
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: