Cocos2dx Memory Optimization

Source: Internet
Author: User

In cocos2dx, Sprite does not consume much memory, but memory is consumed by associated material files. It is assumed that there are 10 sprite associated with the same material, there will be no 10 times of consumption.


I think there are several Optimization Methods for the material memory occupied by images:
1. For background images, there is no need to consider transparency issues. When loading materials, you can use the rgb565 format (5-bit red, 6-Bit green, 5-bit blue). The memory consumed by each pixel is 16 bit = 2 bytes. Half of the memory consumed by default rgba8888.
2. A large image can be scaled down as needed. For example, a 960x640 graph can be reduced to 768x512, and the memory consumption is reduced by half.
3. Some sprite do not need so many colors. They can be loaded in the rgba4444 format. one pixel consumes only 2 bytes and is halved. You can use a tool such as texturepacker to process the original 32 bitpng image and generate the rgba4444 format Material file.
4. merging multiple small Graphs into sprite sheet can significantly reduce memory usage and improve performance.
5. Crop a large background image into multiple small blocks. The area to be displayed is loaded into the corresponding block.ProgramIt is a lot more complicated, but it is better than the memory is insufficient to crash.

In my personal practice, the above optimization methods are quite effective.

 

 

1. OpenGL generally uses the n texture size of 2, so when creating a texture, make it as close as possible to the n value of 2, such as 128*128. If the texture size of a slice is 130*100, re-arrange the slice so that it can be controlled within 128*128, which can reduce the memory by half.
2. You can use a thumbnail to zoom in and paste the image. However, we recommend that you reduce the size by an integer when the image definition is not high, and then use nearest to zoom out the image.AlgorithmIn this way, the effect is acceptable and the efficiency is high.
3. When merging slices into textures, you must put the slices with the same time and frequency together so that the textures currently loaded into memory are the most frequently used. The unused textures should be released in time. Gamers generally can wait for the progress bar to load something.
4. In the early stage of the game, you can use a simulator for testing. In the later stage, you must use a real machine + instruments for testing. xcode has strong instruments functions and can detect leak, OpenGL optimization recommendations, and allocation.
5. The game should avoid repeated memory allocation in the logic loop or render loop, resulting in a vicious increase.
6. PNG uses PS for Web compression to reduce the file size. Although it does not reduce the memory allocation during runtime, it can reduce the I/O time of texture loading.

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.