COCOS2DX Tips (10) Three kinds of cache class demo

Source: Internet
Author: User

In introducing the level three cache, first, we have to figure out a problem: What is a texture? Its popular explanation, texture is the picture of its meaning.

When the image is loaded into memory, it is present in the form of a texture. As can be seen from here. The texture is actually a piece of memory. This block of memory is populated with image pixel information in the specified pixel format.



The following is the point of entry:
1, Texturecache
The Texturecache texture cache is the lowest and most efficient texture cache. What's the use of it? Let me first describe a phenomenon: If there is a lot of pictures in the game, the first time you point into this interface is very slow (because you want to load many pictures). But then the second click went in. What is this for? The original COCOS2DX rendering mechanism is able to repeatedly use the same texture on different occasions to draw, so as to achieve repeated use. Reduce the consumption and overhead of memory and GPU computing resources .

Take a sample: Create an Elf

Auto SP = sprite::create ("Image.png");//Sprite picture is Image.png
The first time to use image.png this picture has two steps, one is to load the picture into the Texturecache cache, and the next step is to draw the picture to display it in the scene


The second time you use Image.png, because the previous image.png has been put into Texturecache, it is only necessary to find this image from the cache and then draw it out .
Here's a summary with a few lines of code:

The first way to load the wizard Auto SP1 = sprite::create ("Boy.png"); This->addchild (sp1,1);//Another way to load the Genie auto Sp_cache = Director:: GetInstance ()->addimage ("Boy.png"), Auto SP1 = sprite::createwithtexture (Sp_cache); This->addchild (sp1,1);

One might think that another method is more efficient, in fact it is not. Both kinds are the same !

Both of them are loaded into the Texturecache cache, and then extracted from the cache and drawn out of these two steps.

But another way to use it is more flexible, as I said above, if an interface uses a lot of resources. These image resources can be read into Texturecache first. When you really get into this interface and then use these pictures, the speed will be very fast. Often used in the place is the game's resource loading interface, can participate in this blog: http://blog.csdn.net/start530/article/details/19420317


2, Spriteframecache
Name implies. The cache here is the Spriteframe cache.

Same as the Texturecache function. It's just different from Texturecache. If the picture you want to find does not exist in the memory pool. It will prompt you not to find it, but not to load the picture locally.

Spriteframecache is generally used to process the Plist file (this file specifies the location and size of each individual sprite in this "big picture"), the file corresponding to a larger picture of the sprite, plist file can be used Texturepacker production. Auto Framecache = Spriteframecache::getinstance (); Framecache->addspriteframeswithfile ("Boy.plist", "boy.png"); /boy.png boy1.png,boy2.png These small figures auto frame_sp = sprite::createwithspriteframename ("Boy1.png");// Find Boy1.png This image from the Spriteframecache cache. This->addchild (frame_sp,2);


3, Animationcache

This should be the simplest of all, the cache of animations.

for sprite animations, you need to load the sprite frame each time you create it. The array is then added sequentially, and the animation is created by reading the array with animation. This is a very cumbersome calculation process .

The use of high-frequency animation, such as character walking, dancing, etc., can be added to the Animationcache, each use from the cache, which can effectively reduce the huge cost of creating animation.
The scale is as follows: If there are two sets of animated dance_animate,sleep_animate.

Load it into the cache animationcache::getinstance ()->addanimation (Dance_animate, "Dance");// The second parameter is the corresponding keyanimationcache::getinstance ()->addanimation (Sleep_animate, "sleep") when the animation is cached;//Read the animation in the cache auto Dance_ Animate = Animationcache::getinstance ()->animationbyname ("Dance");//Extract animation from cache by key Auto Sleep_animate = Animationcache::getinstance ()->animationbyname ("Sleep");

All right, that's it.

respect for originality. Reprint please indicate source: http://blog.csdn.net/star530/article/details/23612487

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

COCOS2DX Tips (10) Three kinds of cache class demo

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.