Cocos2d-x optimization (texture rendering optimization, resource cache, memory optimization)

Source: Internet
Author: User
The following five optimizations are provided: Engine underlying optimization, texture optimization, rendering optimization, resource cache, and Memory Optimization engine optimization. Version 2.0 is later than version 1.0 inAlgorithmAnd higher efficiency. Version 2.0 uses the OpenGL ES 2.0 graphics library and Version 1.0 uses OpenGL ES 1.0. Texture Optimization

Textures consume the most memory and reduce the rendering speed.

Power of two

When applying for memory to store textures, OpenGL applies to the power of 2, that is, corresponding to the image of 480*320, which applies for a space of 512*512. It can be seen that a considerable amount of memory will be wasted. Therefore, the image we designed is preferably a power of 2, or OpenGL will eventually apply for a power of 2 memory space.

Because not all images can be designed to the power of 2, they can be solved through the "Jigsaw Puzzle" method. This is why texturepacker is used.

Color Depth Optimization

The size of memory occupied by images is calculated using this formula: height pixel * width pixel * color depth. For example, the color-depth rgba8888 is 32 bits, each of which is a byte. Therefore, a pixel occupies 4 bytes, which is an integer character. Example. A 480*320 image size occupies 480*320*4 bytes of memory.

Generally, if the color of the image is not very high. The value of argb8888 (4 bytes) can be changed to that of argb1555 (2 bytes) (transparent channel a occupies 1 bit for image mixing) or argb4444 (if not for hybrid operations, use four digits ). Alternatively, if you do not need to use image transparency, you do not need channel A. Instead, you can use rgb888 (3 bytes) to change it to rgb565 (2 bytes ). In this way, the final memory size of the image will occupy less space.

Texture compression format

The IOS system uses the powervr display chip to hard-decode the PVR format, which can be directly read by IOS. However, this method is not suitable for Android and other platforms.

The PVR format is actually to convert argb8888 into argb1555 color depth for storage.

Skeleton Animation

The cocos2d-x 2.0.3 starts to support bone animation, and cocos2dbuilder2.1 can also be used for animation editing. Memory optimization is also a good solution for animation processing.

Texture size limit

Different devices support different texture sizes. The maximum supported size for iPhone 4 is 4096*4096, and that for iPhone 4S is 4096*2048. Some Android devices are limited to 1024*1024. Avoid making too many pictures in a puzzle. To improve game compatibility, it is best to set the image size within the range of 1024*1024 (supported by mainstream devices ).

Rendering Optimization Batch Processing

Use ccspritebatchnode to prepare the coordinates of the sprite and perform one-time rendering.

The traditional rendering process is as follows: different switching textures are loaded and rendered differently. Batch processing increases the efficiency by loading all textures at a time and then rendering them at a time.

Resource Cache Ccspriteframecache Cctexturecache Cache power consumption,Therefore, the remove function is called after use.In addition, the progress bar should be displayed during pre-loading, which may affect the user experience and should be used as needed. Memory Optimization memory occupied by resources As described in the previous sections Memory Pool Solution When the game is started, it will be allocated a large area, and the game will not be released during running until it is withdrawn and then released. Instead of allocating memory for new resources, resources are loaded into the memory pool. When used for multiple times, you do not need to release or reload it multiple times. In this section, you can learn more about the memory pool management mechanism of C ++.

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: 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.