cocos2d-x  3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TextureCache Class Reference

Singleton that handles the loading of textures. More...

#include <CCTextureCache.h>

Inheritance diagram for TextureCache:
Ref

Classes

struct  AsyncStruct
 

Public Member Functions

 TextureCache ()
virtual ~TextureCache ()
virtual std::string getDescription () const
Texture2DaddImage (const std::string &filepath)
 Returns a Texture2D object given an filename. More...
 
local addImage ( local filepath)
 Returns a Texture2D object given an filename. More...
 
virtual void addImageAsync (const std::string &filepath, const std::function< void(Texture2D *)> &callback)
 Returns a Texture2D object given a file image. More...
 
local addImageAsync ( local filepath, local callback)
 Returns a Texture2D object given a file image. More...
 
virtual void unbindImageAsync (const std::string &filename)
 Unbind a specified bound image asynchronous callback. More...
 
var unbindImageAsync ( var filename)
 Unbind a specified bound image asynchronous callback. More...
 
local unbindImageAsync ( local filename)
 Unbind a specified bound image asynchronous callback. More...
 
virtual void unbindAllImageAsync ()
 Unbind all bound image asynchronous load callbacks. More...
 
Texture2DaddImage (Image *image, const std::string &key)
 Returns a Texture2D object given an Image. More...
 
local addImage ( local image, local key)
 Returns a Texture2D object given an Image. More...
 
Texture2DaddUIImage (Image *image, const std::string &key)
Texture2DgetTextureForKey (const std::string &key) const
 Returns an already created texture. More...
 
local getTextureForKey ( local key)
 Returns an already created texture. More...
 
Texture2DtextureForKey (const std::string &key) const
bool reloadTexture (const std::string &fileName)
 Reload texture from the image file. More...
 
void removeAllTextures ()
 Purges the dictionary of loaded textures. More...
 
void removeUnusedTextures ()
 Removes unused textures. More...
 
void removeTexture (Texture2D *texture)
 Deletes a texture from the cache given a texture. More...
 
void removeTextureForKey (const std::string &key)
 Deletes a texture from the cache given a its key name. More...
 
var removeTextureForKey ( var key)
 Deletes a texture from the cache given a its key name. More...
 
local removeTextureForKey ( local key)
 Deletes a texture from the cache given a its key name. More...
 
std::string getCachedTextureInfo () const
 Output to CCLOG the current contents of this TextureCache. More...
 
void waitForQuit ()
 Called by director, please do not called outside. More...
 
- Public Member Functions inherited from Ref
void retain ()
 Retains the ownership. More...
 
void release ()
 Releases the ownership immediately. More...
 
Refautorelease ()
 Releases the ownership sometime soon automatically. More...
 
unsigned int getReferenceCount () const
 Returns the Ref's current reference count. More...
 
virtual ~Ref ()
 Destructor. More...
 

Static Public Member Functions

static TextureCachegetInstance ()
 Returns the shared instance of the cache. More...
 
var getInstance ()
 Returns the shared instance of the cache. More...
 
static TextureCachesharedTextureCache ()
var sharedTextureCache ()
local sharedTextureCache ()
static void destroyInstance ()
 Purges the cache. More...
 
static void purgeSharedTextureCache ()
local purgeSharedTextureCache ()
static void reloadAllTextures ()
 Reload all textures. More...
 

Protected Attributes

std::thread * _loadingThread
std::queue< AsyncStruct * > * _asyncStructQueue
var _asyncStructQueue
local _asyncStructQueue
std::deque< ImageInfo * > * _imageInfoQueue
std::mutex _asyncStructQueueMutex
std::mutex _imageInfoMutex
std::mutex _sleepMutex
std::condition_variable _sleepCondition
var _sleepCondition
local _sleepCondition
bool _needQuit
int _asyncRefCount
std::unordered_map
< std::string, Texture2D * > 
_textures
- Protected Attributes inherited from Ref
unsigned int _referenceCount
 count of references More...
 
local _referenceCount
 count of references More...
 

Additional Inherited Members

- Protected Member Functions inherited from Ref
 Ref ()
 Constructor. More...
 
 Ref ()
 Constructor. More...
 

Detailed Description

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.

Constructor & Destructor Documentation

var ctor ( )
local TextureCache ( )
virtual ~TextureCache ( )
virtual

Member Function Documentation

Texture2D* addImage ( const std::string &  filepath)

Returns a Texture2D object given an filename.

If the filename was not previously loaded, it will create a new Texture2D. 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.

Parameters
filepathA null terminated string.
var addImage ( var  filepath)

Returns a Texture2D object given an filename.

If the filename was not previously loaded, it will create a new Texture2D. 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.

Parameters
filepathA null terminated string.
local addImage ( local  filepath)

Returns a Texture2D object given an filename.

If the filename was not previously loaded, it will create a new Texture2D. 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.

Parameters
filepathA null terminated string.
Texture2D* addImage ( Image *  image,
const std::string &  key 
)

Returns a Texture2D object given an Image.

If the image was not previously loaded, it will create a new Texture2D object and it will return it. Otherwise it will return a reference of a previously loaded image.

Parameters
keyThe "key" parameter will be used as the "key" for the cache. If "key" is nil, then a new texture will be created each time.
var addImage ( var  image,
var  key 
)

Returns a Texture2D object given an Image.

If the image was not previously loaded, it will create a new Texture2D object and it will return it. Otherwise it will return a reference of a previously loaded image.

Parameters
keyThe "key" parameter will be used as the "key" for the cache. If "key" is nil, then a new texture will be created each time.
local addImage ( local  image,
local  key 
)

Returns a Texture2D object given an Image.

If the image was not previously loaded, it will create a new Texture2D object and it will return it. Otherwise it will return a reference of a previously loaded image.

Parameters
keyThe "key" parameter will be used as the "key" for the cache. If "key" is nil, then a new texture will be created each time.
virtual void addImageAsync ( const std::string &  filepath,
const std::function< void(Texture2D *)> &  callback 
)
virtual

Returns a Texture2D object given a file image.

If the file image was not previously loaded, it will create a new Texture2D 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

Parameters
filepathA null terminated string.
callbackA callback function would be inovked after the image is loaded.
Since
v0.8
var addImageAsync ( var  filepath,
var  callback 
)
virtual

Returns a Texture2D object given a file image.

If the file image was not previously loaded, it will create a new Texture2D 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

Parameters
filepathA null terminated string.
callbackA callback function would be inovked after the image is loaded.
Since
v0.8
local addImageAsync ( local  filepath,
local  callback 
)
virtual

Returns a Texture2D object given a file image.

If the file image was not previously loaded, it will create a new Texture2D 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

Parameters
filepathA null terminated string.
callbackA callback function would be inovked after the image is loaded.
Since
v0.8
Texture2D* addUIImage ( Image *  image,
const std::string &  key 
)
inline
var addUIImage ( var  image,
var  key 
)
inline
local addUIImage ( local  image,
local  key 
)
inline
static void destroyInstance ( )
static

Purges the cache.

It releases the retained instance.

Since
v0.99.0
var destroyInstance ( )
static

Purges the cache.

It releases the retained instance.

Since
v0.99.0
local destroyInstance ( )
static

Purges the cache.

It releases the retained instance.

Since
v0.99.0
std::string getCachedTextureInfo ( ) const

Output to CCLOG the current contents of this TextureCache.

This will attempt to calculate the size of each texture, and the total texture memory in use.

Since
v1.0
var getCachedTextureInfo ( )

Output to CCLOG the current contents of this TextureCache.

This will attempt to calculate the size of each texture, and the total texture memory in use.

Since
v1.0
local getCachedTextureInfo ( )

Output to CCLOG the current contents of this TextureCache.

This will attempt to calculate the size of each texture, and the total texture memory in use.

Since
v1.0
virtual std::string getDescription ( ) const
virtual
static TextureCache* getInstance ( )
static

Returns the shared instance of the cache.

var getInstance ( )
static

Returns the shared instance of the cache.

local getInstance ( )
static

Returns the shared instance of the cache.

Texture2D* getTextureForKey ( const std::string &  key) const

Returns an already created texture.

Returns nil if the texture doesn't exist.

Parameters
keyIt's the related/absolute path of the file image.
Since
v0.99.5
var getTextureForKey ( var  key)

Returns an already created texture.

Returns nil if the texture doesn't exist.

Parameters
keyIt's the related/absolute path of the file image.
Since
v0.99.5
local getTextureForKey ( local  key)

Returns an already created texture.

Returns nil if the texture doesn't exist.

Parameters
keyIt's the related/absolute path of the file image.
Since
v0.99.5
static void purgeSharedTextureCache ( )
static
var purgeSharedTextureCache ( )
static
local purgeSharedTextureCache ( )
static
static void reloadAllTextures ( )
static

Reload all textures.

Should not call it, called by frame work. Now the function do nothing, use VolatileTextureMgr::reloadAllTextures.

var reloadAllTextures ( )
static

Reload all textures.

Should not call it, called by frame work. Now the function do nothing, use VolatileTextureMgr::reloadAllTextures.

local reloadAllTextures ( )
static

Reload all textures.

Should not call it, called by frame work. Now the function do nothing, use VolatileTextureMgr::reloadAllTextures.

bool reloadTexture ( const std::string &  fileName)

Reload texture from the image file.

If the file image hasn't loaded before, load it. Otherwise the texture will be reloaded from the file image.

Parameters
fileNameIt's the related/absolute path of the file image.
Returns
True if the reloading is succeed, otherwise return false.
var reloadTexture ( var  fileName)

Reload texture from the image file.

If the file image hasn't loaded before, load it. Otherwise the texture will be reloaded from the file image.

Parameters
fileNameIt's the related/absolute path of the file image.
Returns
True if the reloading is succeed, otherwise return false.
local reloadTexture ( local  fileName)

Reload texture from the image file.

If the file image hasn't loaded before, load it. Otherwise the texture will be reloaded from the file image.

Parameters
fileNameIt's the related/absolute path of the file image.
Returns
True if the reloading is succeed, otherwise return false.
void removeAllTextures ( )

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.

var removeAllTextures ( )

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.

local removeAllTextures ( )

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 removeTexture ( Texture2D texture)

Deletes a texture from the cache given a texture.

var removeTexture ( var  texture)

Deletes a texture from the cache given a texture.

local removeTexture ( local  texture)

Deletes a texture from the cache given a texture.

void removeTextureForKey ( const std::string &  key)

Deletes a texture from the cache given a its key name.

Parameters
keyIt's the related/absolute path of the file image.
Since
v0.99.4
var removeTextureForKey ( var  key)

Deletes a texture from the cache given a its key name.

Parameters
keyIt's the related/absolute path of the file image.
Since
v0.99.4
local removeTextureForKey ( local  key)

Deletes a texture from the cache given a its key name.

Parameters
keyIt's the related/absolute path of the file image.
Since
v0.99.4
void removeUnusedTextures ( )

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
var removeUnusedTextures ( )

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
local removeUnusedTextures ( )

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
static TextureCache*
sharedTextureCache
( )
static
var sharedTextureCache ( )
static
local sharedTextureCache ( )
static
Texture2D* textureForKey ( const std::string &  key) const
inline
var textureForKey ( var  key)
inline
local textureForKey ( local  key)
inline
virtual void unbindAllImageAsync ( )
virtual

Unbind all bound image asynchronous load callbacks.

Since
v3.1
var unbindAllImageAsync ( )
virtual

Unbind all bound image asynchronous load callbacks.

Since
v3.1
local unbindAllImageAsync ( )
virtual

Unbind all bound image asynchronous load callbacks.

Since
v3.1
virtual void unbindImageAsync ( const std::string &  filename)
virtual

Unbind a specified bound image asynchronous callback.

In the case an object who was bound to an image asynchronous callback was destroyed before the callback is invoked, the object always need to unbind this callback manually.

Parameters
filenameIt's the related/absolute path of the file image.
Since
v3.1
var unbindImageAsync ( var  filename)
virtual

Unbind a specified bound image asynchronous callback.

In the case an object who was bound to an image asynchronous callback was destroyed before the callback is invoked, the object always need to unbind this callback manually.

Parameters
filenameIt's the related/absolute path of the file image.
Since
v3.1
local unbindImageAsync ( local  filename)
virtual

Unbind a specified bound image asynchronous callback.

In the case an object who was bound to an image asynchronous callback was destroyed before the callback is invoked, the object always need to unbind this callback manually.

Parameters
filenameIt's the related/absolute path of the file image.
Since
v3.1
void waitForQuit ( )

Called by director, please do not called outside.

var waitForQuit ( )

Called by director, please do not called outside.

local waitForQuit ( )

Called by director, please do not called outside.

Member Data Documentation

int _asyncRefCount
protected
var _asyncRefCount
protected
local _asyncRefCount
protected
std::queue<AsyncStruct*>
* _asyncStructQueue
protected
var _asyncStructQueue
protected
local _asyncStructQueue
protected
std::mutex _asyncStructQueueMutex
protected
var _asyncStructQueueMutex
protected
local _asyncStructQueueMutex
protected
std::mutex _imageInfoMutex
protected
var _imageInfoMutex
protected
local _imageInfoMutex
protected
std::deque<ImageInfo*>
* _imageInfoQueue
protected
var _imageInfoQueue
protected
local _imageInfoQueue
protected
std::thread* _loadingThread
protected
var _loadingThread
protected
local _loadingThread
protected
bool _needQuit
protected
var _needQuit
protected
local _needQuit
protected
std::condition_variable
_sleepCondition
protected
var _sleepCondition
protected
local _sleepCondition
protected
std::mutex _sleepMutex
protected
var _sleepMutex
protected
local _sleepMutex
protected
std::unordered_map<std::string,
Texture2D*> _textures
protected
var _textures
protected
local _textures
protected

The documentation for this class was generated from the following file: