COCOS2DX Texture Cache

Source: Internet
Author: User
Tags addchild

Texture Caching Overview

In the game you need to load a lot of texture pictures, these operations are very memory and resources.

When the game has an interface with a lot of pictures, the first point in the interface when the speed is very slow (because to load a lot of pictures), we can use Texturecache to load the texture ahead of time, such as loading the end, to enter the interface and then use the image speed will be very fast.

Texture2d: Texture, which is a map object that the picture loads into memory for CPU and GPU operations.
Texturecache (texture cache) for loading and managing textures. Once the texture is loaded, the next time it is used, it can be used to return the previously loaded textures, reducing the GPU and CPU memory footprint.

Common methods

When you create a genie, you generally use Sprite::create (pszFileName). If you look at the way Sprite::create (pszFileName) is implemented, you will see that it adds the image to the texture cache,

Sprite* Sprite::create(const std::string& filename){    Sprite *sprite = new Sprite();    if (sprite && sprite->initWithFile(filename))    {        sprite->autorelease();        return sprite;    }    _SAFE_DELETE(sprite);    return nullptr;}bool Sprite::initWithFile(const std::string& filename){    ASSERT(filename.size()>0, "Invalid filename for sprite");    Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);    if (texture)    {        Rect rect = Rect::ZERO;        rect.size = texture->getContentSize();        return initWithTexture(texture, rect);    }    // don‘t release here.    // when load texture failed, it‘s better to get a "transparent" sprite then a crashed program    // this->release();    return false;}

The above code shows the loading texture in the control. Once the texture is loaded, the texture reference that was loaded before will be returned at the next moment, and the memory added in the moment of loading is reduced. (See Texturecache API for detailed API)

Get Texturecache

In version 3.0, Texturecache is no longer used as a singleton mode. As a member variable for a director, gets the

Director::getInstance()->getTextureCache();
Get Textures

If the file name was not previously loaded, it creates a new Texture2d object, which returns it. It will use the file name as key otherwise it will return a reference to the previously loaded image. Texturecache masks Many details of loading textures; The AddImage function returns a reference to a texture texture2d, either newly loaded into memory, or it may have existed before;

Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);

You can also use the Gettextureforkey method to get the texture cache for this key, and if the key corresponding to the texture does not exist, then return null

Texture2D *texture = Director::getInstance()->getTextureCache()->getTextureForKey(textureKeyName);
Loading textures asynchronously

The Texturecache class also supports the ability to load resources asynchronously, using the Addimageasync method. You can add a callback method to the Addimageasync method in a way that can be notified when the texture is loaded asynchronously.

You can choose the asynchronous load mode so you can add a progress bar to the loading scene. The key code is as follows:

Texturecachetest::texturecachetest (): _numberofsprites ($), _numberofloadedsprites (0) {Auto size = Director::    GetInstance ()->getwinsize ();    _labelloading = Label::createwithttf ("Loading ...", "Fonts/arial.ttf", 15);    _labelpercent = Label::createwithttf ("%0", "Fonts/arial.ttf", 15);    _labelloading->setposition (Point (SIZE.WIDTH/2, SIZE.HEIGHT/2-20));    _labelpercent->setposition (Point (SIZE.WIDTH/2, SIZE.HEIGHT/2 + 20));    This->addchild (_labelloading);    This->addchild (_labelpercent); Load Textrues director::getinstance ()->gettexturecache ()->addimageasync ("Images/helloworld.png", _    Callback_1 (Texturecachetest::loadingcallback, this)); Director::getinstance ()->gettexturecache ()->addimageasync ("Images/grossini.png", _CALLBACK_1 (    Texturecachetest::loadingcallback, this)); Director::getinstance ()->gettexturecache ()->addimageasync ("Images/grossini_dance_01.png", _CALLBACK_1 (    Texturecachetest::loadingcallback, this)); ....} VoID texturecachetest::loadingcallback (cocos2d::texture2d *texture) {++_numberofloadedsprites;    Char tmp[10];    sprintf (tmp, "%%%d", (int) (((float) _numberofloadedsprites/_numberofsprites) * 100));    _labelpercent->setstring (TMP);        if (_numberofloadedsprites = = _numberofsprites) {this->removechild (_labelloading, true);        This->removechild (_labelpercent, true);    Addsprite (); }}
Clean up the cache

Removeunusedtextures releases all current textures that have a reference count of 1, which is not currently in use. For example, when a new scene is created, it is convenient to use this method to release unused textures.

Director::getInstance()->getTextureCache()->removeUnusedTextures();

Textures still exist between memory when no other object (such as a sprite) holds a reference to the texture. Based on this, we can immediately remove from the cache, so that when the texture is not needed, it will be released from memory immediately. As shown in the following code:

Director::getInstance()->getTextureCache()->removeTextureForKey("Images/grossinis_sister2.png");

The Removealltextures () method can be called when "Memory Warning" is received. In the short term: it will also release some resources to prevent your application from being killed; Medium: It will allocate more resources; In the long run: it will be the same.

Director::getInstance()->getTextureCache()->removeAllTextures();

Source URL: https://github.com/chukong/cocos-docs/blob/master/manual/framework/native/v3/texture-cache/zh.md

COCOS2DX Texture Cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.