(21) Texture cache (Texture cache)

Source: Internet
Author: User

Brief introduction

Texture caching is a drawing work that caches textures easily. The size, color, and area range of each cached image can be modified. This information is stored in memory and is not sent to the GPU every time it is drawn.

Cctexturecache

Cocos2d caches the sprite's texture by calling Cctexturecache or Ccspriteframecache.

When the genie calls the Cctexturecache or Ccspriteframecache method, COCOS2DX uses the texture cache to create a ccsprite. So you can pre-load the textures into the cache, which is handy when you use them in the scene. How to load these textures depends on your own thoughts. For example, you can choose the asynchronous load mode so that you can add a progress bar to the loading scene.

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

bool CCSprite::initWithFile(const char *pszFilename){    CCAssert(pszFilename != NULL, "Invalid filename for sprite");    CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFilename);    if (pTexture)    {        CCRect rect = CCRectZero;        rect.size = pTexture->getContentSize();        return initWithTexture(pTexture, rect);    }    // don‘t release here.    // when load texture failed, it‘s better to get a "transparent" sprite than a crashed program    // this->release();     returnfalse;}

The above code shows a single example of loading textures in 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 Cctexturecache API for detailed API)

Ccspriteframecache

The Ccspriteframecache Singleton is the cache for all sprite frames. Using Spritesheet and the associated XML file, we can load a lot of sprite frames into the cache, then we can create the sprite object from this cache.

The texture set associated with this XML is generally a large picture that contains a lot of small textures. Here is an example of a texture set:

There are three ways to load the texture set into Ccspriteframecache:

    • Loading an XML (plist) file
    • Load an XML (plist) file and a texture set
    • By Ccspriteframe and the name of a sprite frame

For a complete API, see the Ccspriteframecache API.

Examples:

The reason to use the cache is to reduce memory, because when you use a picture to create a sprite, if the picture is not in the cache, then it will be loaded into the cache, when you need to use the same image to create a new wizard, you can directly from the cache, instead of a new allocation of memory space.

Ccspriteframecache vs. Ccspritebatchnode
    • It is best to use Spritesheets (ccspritebatchnodes) whenever possible. The way to do this is to reduce the number of draw calls. The call to draw is very time-consuming. Each batchnode is called once draw to draw all the nodes above, instead of each node's draw is called separately.
    • Ccspritebatchnode renders all of the child nodes only once, only one call to draw is required. That's why you need to load the wizard with batch node because it can be rendered uniformly. But only the texture used by this genie is included in batch node to be added to batch node, because batch node renders only the same texture set at a time.
    • If you add sprites to other nodes. Then each wizard will call its own draw function, and batch node will not work.
    • Ccspritebatchnode is also a common node. You can remove it from the scene like any other node. Both the texture set and the sprite frame are cached in the Cctexturecache and Ccspriteframecache Singleton. If you want to remove texture sets and Sprite frames from memory, you have to do this by caching classes.
Maximum texture size allowed for each platform hardware

As a matter of principle, cocos2d-x can display any size texture, but the texture size is actually limited due to hardware and operating system reasons.

Here we provide a table of texture size limits on different platform simulators

Platform maxsize in pixels
Win32 2048*2048
Android 4096*4096
IPhone3 1024*1024
Iphone3gs 2048*2048
IPhone4 2048*2048

On the real machine, there are some different restrictions, here are some test results: G31024*1024, iPhone4 2048*2048

So for developers, if you want to cross-platform and the game runs smoothly, you'd better keep your texture size smaller than 1024*1024, which is the limit for most machines.

(21) Texture cache (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.