Axmol Engine 2.4.0-258ceca
Loading...
Searching...
No Matches
TextureCache Class Reference

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

#include <TextureCache.h>

Inherits Object.

Public Member Functions

 TextureCache ()
 @js ctor
 
virtual ~TextureCache ()
 @js NA @lua NA
 
virtual std::string getDescription () const
 @js NA @lua NA
 
Texture2DgetWhiteTexture ()
 Gets a 2x2 white texture.
 
Texture2DgetWhiteTexture (std::string_view key, uint8_t luma)
 Gets a 2x2 texture whith specify luma and key.
 
Texture2DgetDummyTexture ()
 Gets 1x1 dummy texture with alpha=0.
 
Texture2DaddImage (std::string_view filepath)
 Returns a Texture2D object given an filename.
 
virtual void addImageAsync (std::string_view filepath, const std::function< void(Texture2D *)> &callback)
 Returns a Texture2D object given a file image.
 
virtual void unbindImageAsync (std::string_view filename)
 Unbind a specified bound image asynchronous callback.
 
virtual void unbindAllImageAsync ()
 Unbind all bound image asynchronous load callbacks.
 
Texture2DaddImage (Image *image, std::string_view key)
 Returns a Texture2D object given an Image.
 
Texture2DaddImage (const Data &imageData, std::string_view key)
 Returns a Texture2D object given an Image.
 
Texture2DgetTextureForKey (std::string_view key) const
 Returns an already created texture.
 
bool reloadTexture (std::string_view fileName)
 Reload texture from the image file.
 
void removeAllTextures ()
 Purges the dictionary of loaded textures.
 
void removeUnusedTextures ()
 Removes unused textures.
 
void removeTexture (Texture2D *texture)
 Deletes a texture from the cache given a texture.
 
void removeTextureForKey (std::string_view key)
 Deletes a texture from the cache given a its key name.
 
std::string getCachedTextureInfo () const
 Output to AXLOGD the current contents of this TextureCache.
 
void waitForQuit ()
 Called by director, please do not called outside.
 
std::string getTextureFilePath (Texture2D *texture) const
 Get the file path of the texture.
 
void renameTextureWithKey (std::string_view srcName, std::string_view dstName)
 Reload texture from a new file.
 
- Public Member Functions inherited from Object
void retain ()
 Retains the ownership.
 
void release ()
 Releases the ownership immediately.
 
Objectautorelease ()
 Releases the ownership sometime soon automatically.
 
unsigned int getReferenceCount () const
 Returns the Object's current reference count.
 
virtual ~Object ()
 Destructor.
 

Additional Inherited Members

- Public Attributes inherited from Object
unsigned int _ID
 object id, ScriptSupport need public _ID
 
int _luaID
 Lua reference id.
 

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.

Member Function Documentation

◆ addImage() [1/3]

Texture2D * addImage ( std::string_view 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, .jpeg, .pvr.

Parameters
filepathThe file path.

◆ addImageAsync()

virtual void addImageAsync ( std::string_view 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 offthread, 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
filepathThe file path.
callbackA callback function would be invoked after the image is loaded.
Since
v0.8
Remarks
Please don't invoke Texture2D::setDefaultAlphaPixelFormat in main GL thread before invoke this API.

◆ unbindImageAsync()

virtual void unbindImageAsync ( std::string_view 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

◆ unbindAllImageAsync()

virtual void unbindAllImageAsync ( )
virtual

Unbind all bound image asynchronous load callbacks.

Since
v3.1

◆ addImage() [2/3]

Texture2D * addImage ( Image * image,
std::string_view 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.

◆ addImage() [3/3]

Texture2D * addImage ( const Data & imageData,
std::string_view 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
imageDataThe Data of the image to use
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.

◆ getTextureForKey()

Texture2D * getTextureForKey ( std::string_view 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

◆ reloadTexture()

bool reloadTexture ( std::string_view 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.

◆ removeAllTextures()

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.

◆ removeUnusedTextures()

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

◆ removeTextureForKey()

void removeTextureForKey ( std::string_view 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

◆ getCachedTextureInfo()

std::string getCachedTextureInfo ( ) const

Output to AXLOGD 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

◆ getTextureFilePath()

std::string getTextureFilePath ( Texture2D * texture) const

Get the file path of the texture.

Parameters
textureA Texture2D object pointer.
Returns
The full path of the file.

◆ renameTextureWithKey()

void renameTextureWithKey ( std::string_view srcName,
std::string_view dstName )

Reload texture from a new file.

This function is mainly for editor, won't suggest use it in game for performance reason.

Parameters
srcNameOriginal texture file name.
dstNameNew texture file name.
Since
v3.10

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