cocos2d-x caching mechanism: preload and reuse
Caching is a very common optimization method in hardware and software design, and it is mostly used in the design of high performance hardware and software. Simply put, the cache is the use of memory speed level differences, low-speed storage in the use of high-frequency content loaded into high-speed storage, which can effectively improve access speed. For example, the common image resources are read from disk to memory, the common program segments from memory to the CPU cache.
Mobile devices are more commonly used for external memory to memory caches. Although most phones use flash memory as external storage, they are much faster than disks on a PC, but they are two to three orders of magnitude worse than memory. It takes nearly 0.1 seconds to read an image from the flash memory, so repeated loading consumes a lot of system resources. The caching mechanism can preload the content we need into memory and reuse it in subsequent operations.
Cache space is limited after all, each cache should implement a reasonable swap-out mechanism to ensure that the content in the cache is really the most need to be reused. At the same time, the cache should be as transparent as possible, that is, the cache should take effect if it is not actively invoked. Fortunately, we don't need to implement the cache ourselves, because Cocos2d-x has provided us with a strong enough implementation. There are 3 cache classes in the engine, all of which are global singleton patterns.
1 Cctexturecache
The first is the lowest and most efficient texture cache cctexturecache, which caches the texture resources loaded into memory, which is the picture resource. The principle is to add a cache of texture resources to a reference, so that its reference count plus one, keep not cleared, and the cocos2d-x rendering mechanism can be repeated using the same texture on different occasions to draw, so as to achieve reuse, reduce the cost of memory and GPU computing resources. Commonly used are the 3 shown below
1 static cctexturecache* Sharedtexturecache (); Returns a global singleton of the texture cache
2 cctexture2d* addimage (const char* fileimage);//Add a texture picture to the cache
3 void removeunusedtextures (); To clear unused textures
In these 3 interfaces, Cctexturecache masks Many details of loading textures; The AddImage function returns a reference to a texture cctexture2d, either newly loaded into memory or a previously existing While Removeunusedtextures releases all current textures with a reference count of 1, which is not currently used. As you'll see later, the memory management of the reference count provides a great convenience for the cache design.
In these 3 interfaces, Cctexturecache masks Many details of loading textures; The AddImage function returns a reference to a texture cctexture2d, either newly loaded into memory or a previously existing While Removeunusedtextures releases all current textures with a reference count of 1, which is not currently used. As you'll see later, the memory management of the reference count provides a great convenience for the cache design.
2 Ccspriteframecache
The second one is the sprite frame cache. As the name implies, this cache is the sprite frame ccspriteframe, which mainly serve the multiple pieces of the image of the merged texture. This texture in a large map contains a number of small images, directly through the Cctexturecache reference will be a lot of inconvenience, and thus derived from the processing of the wizard frame, that is, the captured texture information stored in a sprite frame, the wizard by toggling different frame to show the different patterns.
Ccspriteframecache Common interface and Cctexturecache similar, no longer repeat, the only thing to note is to add the sprite frame of the companion file-a plist file and a large texture map. Here's a list of common methods for Ccspriteframe cache:
1 static ccspriteframecache* sharedspriteframecache (void); Global Shared Cache Example
2 void addspriteframeswithfile (const char *pszplist);//Add a set of sprite frames by plist config file
3 void Removeunusedspriteframes (void); Clean up useless caches
3 Ccanimationcache
The last one is the cache of Ccanimationcache animations. In general, for a sprite animation, each time you create the need to load the sprite frame, sequentially added to the array, and then create the corresponding action class, this is a very cumbersome calculation process. For high-frequency animations, such as fish swimming, adding them to the cache can effectively reduce the huge cost of each creation. Because the purpose of this class and the cache content are very simple and straightforward, the interface is the simplest and most straightforward, as follows:
1 static ccanimationcache* sharedanimationcache (void);//Global Shared cache example
2 void addanimation (Ccanimation *animation, const char * name);//Add an animation to cache
3 void removeanimationbyname (const char* name);//Remove a specified animation
4 ccanimation* Animationbyname (const char* name);//Get pre-deposited animations
The only difference is that the animation cache requires that we manually maintain all the animation information. Also because the load frame animation is completely code-aware, there is no configuration file guidance, so it cannot be as transparent as the other two caches. In fact, you can clean up the entire animation cache directly, taking into account that the animations used between the two scenes are basically not duplicated.
Therefore, we should add the following cleanup cache operation when the scene switches:
1 void Releasecaches ()
2 {
3 ccanimationcache::p urgesharedanimationcache ();
4 Ccspriteframecache::sharedspriteframecache ()->removeunusedspriteframes ();
5 Cctexturecache::sharedtexturecache ()->removeunusedtextures ();
6}
It is important to note that the order of cleanup, you should clean up the animation cache, and then clean up the sprite frame, and finally the texture. Follow the reference hierarchy from high to low to ensure that the release reference is valid.