Texture cache (Texture cache)

Source: Internet
Author: User

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. (The progress bar has been loaded with the arts and sciences into the cache, so the next pressure will be smaller.) )

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" );#路径存在不存在,不存在就弹出后面这句话,CCAssert的用法就是这样子的。      CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFilename);#创建一个纹理对象指针,的方法是这样子的。      if (pTexture)#创建成功了的话执行下面的内容      {          CCRect rect = CCRectZero;#创建一个矩形区域对象          rect.size = pTexture->getContentSize();#设置该区域的大小为前面的纹理对象的大小          return initWithTexture(pTexture, rect);#返回一个初始化的结果。(功能是初始化纹理,将纹理加入到缓存中去),但为什么要return我也不知道是什么意思?应该是表示是否创建成功。      }      // don‘t release here.      // when load texture failed, it‘s better to get a "transparent" sprite than a crashed program      // this->release();      return false;#这种返回false应该就是表示创建失败了。在这行的前面应该还有其他的代码。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:

123 CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile(“family.plist”, “family.png”);

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
}

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.