Cocos2dx tips (10) Introduction to three cache classes

Source: Internet
Author: User

When introducing the three caches, we need to clarify one problem first:What is texture?In plain terms, texture is what an image means. When an image is loaded into the memory, it exists as a texture. We can see from here that,Texture is actually a piece of memory! This memory stores the image pixel information filled in according to the specified pixel format.

Enter the subject below:
1. TextureCache
TextureCache texture cache is the bottom layer and the most effective texture cache. What is its use? Let me describe a phenomenon first:Assume that the game has a very large number of images used on an interface. The first time you click this interface, the speed is very slow (because you have to load and draw many images), you can click it for the second time but it will go in at once. Why? The rendering mechanism of Cocos2dx is that the same texture can be repeatedly drawn in different scenarios to achieve reuse and reduce the consumption and overhead of memory and GPU computing resources.. For example, create an genie

Auto sp = Sprite: create ("image.png"); // The image is image.png.
There are two steps to use image.png for the first time, One is to first load the image into the TextureCache cache, and the next step is to draw the image to display it in the scene.
When image.png is used for the second time, because image.png has been put into TextureCache You only need to find the image from the cache and draw it out..
Below is a summary of several lines of code:
// The first method for loading the genie: auto sp1 = Sprite: create ("boy.png"); this-> addChild (sp1, 1 ); // method 2 for loading the genie: auto sp_cache = ctor: getInstance ()-> addImage ("boy.png"); auto sp1 = Sprite: createWithTexture (sp_cache ); this-> addChild (sp1, 1 );

Some people may think that the second method is more efficient,The two are the same.! They are all loaded to the TextureCache cache, and then extracted from the cache to draw the texture. However, the second method is more flexible to use. As I mentioned above, if a page uses a lot of resources, you can read these image resources to TextureCache first, it will be very fast to use the images on this page. The common place is the game resource loading interface, can refer to this blog: http://blog.csdn.net/start530/article/details/19420317


2. SpriteFrameCache
As the name suggests, the cache here is the cache of SpriteFrame. Similar to TextureCache Unlike TextureCache, if there is no image in the memory pool, it will prompt that it cannot be found, rather than loading the image locally.
SpriteFrameCache is generally used to process plist files (this file specifies the location and size of each independent genie in this "big image"). This file corresponds to a large image containing multiple genie, plist files can be created using TexturePacker. Auto frameCache = SpriteFrameCache: getInstance (); frameCache-> addSpriteFramesWithFile ("boy. plist "," boy.png "); // adjust the image auto frame_sp = Sprite: createWithSpriteFrameName (" boy1.png "); // find the image boy1.png from the spriteframecachesaved. this-> addChild (frame_sp, 2 );


3. AnimationCache

This should be the simplest, It is the animation cache. For a Sprite Animation, You need to load the sprite frame each time you create the Animation, add it to the array in sequence, and then read the array to create the Animation. This is a very cumbersome computing process. For animations with high usage frequency, such as walking and dancing of a role, you can add them to the AnimationCache and call them from the cache each time, this can effectively reduce the huge consumption of animation creation.
Example: Assume that there are two sets of animation dance_animate and sleep_animate.
// Load the animation into the cache: getInstance ()-> addAnimation (dance_animate, "dance"); // The second parameter is the corresponding keyAnimationCache when the animation is saved to the cache :: getInstance ()-> addAnimation (sleep_animate, "sleep"); // read the animation auto dance_animate = AnimationCache: getInstance ()-> animationByName ("dance "); // extract the animation auto sleep_animate = AnimationCache: getInstance ()-> animationByName ("sleep") from the cache based on the key ");

Okay, that's it.

Respect Original, reprinted please indicate Source: http://blog.csdn.net/star530/article/details/23612487

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.