Cocos2d-x learning notes-texture Optimization

Source: Internet
Author: User

Reprinted from: Red Baby game programming path csdn blog address: http://blog.csdn.net/honghaier

In the current mobile platform game development process, many friends may encounter the problem that the developed demo occupies too much memory, resulting in low rendering efficiency. The reason is caused by two factors: hardware and software logic. On hardware, mobile devices usually have less memory than PCs, so they can use less memory. In terms of software logic, a large number of large images may lead to excessive memory usage, and excessive Single images may lead to multiple switching textures during rendering, which also greatly reduces rendering efficiency.
First of all, we have no way to overcome the Hardware Memory limit. Therefore, when you develop a game, you must be clear about the memory demand of your program during running, it must be smaller than the available memory, and the smaller the memory, the better. When we develop 2D games, the most used resource is texture. A texture with a width and height of 1024 is in the format of argb8888. It occupies at least 4 MB of memory. Therefore, you must optimize the texture.

There are many schemes for texture optimization. Let's list them one by one.
(1) color depth Optimization
Let's take a look at the comparison chart below the timeline:

The first figure is in the argb888 format, and the second figure is the PVR after I converted it to a1r5g5b5. I can't see any difference from a general gamer's perspective. However, each pixel occupies 4 bytes in the first image, while each pixel occupies 2 bytes in the second image. This means that the image looks almost the same, and the memory usage can be reduced by half.
This solution does not apply to all situations. You need to consider each type of image. If the alpha channel of your image is not used for hollowing out, it is used to render the Alpha mixture. Then, you cannot convert the texture of the alpha channel that occupies only one position. You can try argb4444. For images without Alpha channels, if rgb888 is used, you can try to convert them to rgb565. In addition, after the conversion, the artist must observe and determine the actual situation. If the converted image has obvious distortion, forget it.

(2) puzzle Optimization
First, there are a bunch of small pictures:

There are a total of 20 small images, all of which are in PNG format rgba8888. The size of 15 images is 85x121, one is 52X139, the other is 52X200, and the other is 37x37.
The following is a spliced PNG Image in the same format as a thumbnail.

The size of this image is 512 × 512. We can see that there is still a large blank space for you to splice some small images.
Well, let's compare the memory they occupy.
The size of the second image is 512x512. Therefore, 1 MB of memory is required to create the rgba8888 format texture to store pixel data.
What about the above small images?
The size of the 15 images is 85x121. We know that OpenGL ES expands to the power of 2 when generating textures. The actual texture size of the 85x121 image is 128x128. A 128x128 rgba8888 texture requires 64 K memory to store pixel data. 15x64 K = 960 K.
One 52X139 image, the actual texture size is 64x256, and 64 K is required.
The two images are 52X200. The actual texture size is 64x256, which requires 64 K and 128 K.
Two other images are 37 × 37. The actual texture size is 64 × 64, and the size of the two textures is 32 KB.
The total memory size required for storing pixel data is 1184 kb. It does not include the amount of memory occupied by other information about the texture generated, and there is a lot of blank space for us to splice more small images.
This will also lead to a more obvious efficiency problem, because in rendering, the use of scatter images means to increase the overhead of texture switching. If there are many images, frequent texture switching will greatly reduce the FPs of your game. However, if you use a tiled image, you can render a large number of images with only a few texture settings, greatly reducing the overhead of texture switching.

(3) bone Animation
Yes just knowing that the Cocos2d-x2.0.3 version already supports the skeleton animation system. You can also edit the corresponding skeleton animation in cocosbuilder2.1. This is a good solution for optimizing the memory.
In the past, we used to draw a frame-by-frame animation for each frame. Using a skeleton animation can greatly reduce the number of image frames. It can achieve almost the same animation effect with only a small amount of images. However, you need to do more complex edits to edit the skeleton points of each key frame.
The creation process of bone animation is generally:
1. Set the basic skeleton points and build a skeleton relationship tree. Divide a person into various body parts based on the skeleton tree.
2. Make pictures of characters or animals based on the defined body parts. Of course, the whole figure or animal picture is usually obtained by cutting the graph into a name block.
3. Bind the body part graph block to the corresponding skeleton.
4. Set the key frame, edit the positions of all bones on the key frame, and adjust and rotate the changes.
5. Play the animation so that the skeleton can perform interpolation between two key frames based on time. The interpolation result is used to affect the vertex position when the body part is drawn.
The key to the algorithm of bone animation is the mastery of the bone animation editor. I hope you can study and use it more.
For more information, see the blog post of doubi's brother:
Http://www.cnblogs.com/lovegame/archive/2012/10/15/2722626.html

(4) Batch Processing
Batch, that is, batch, is also an important optimization indicator in the game engine. It refers to a rendering. Obviously, the smaller the number of rendering operations, the higher the efficiency of the game.
Cocos2d-x is obviously understand this truth, give the most obvious Chestnut. There is a class ccspritebatchnode in the "deep analysis of graph word principles in Cocos2d-x" we talked about earlier, which is a class dedicated to managing genie to reduce rendering batches. Let's just say, Let's render a passage: "Have you eaten ?", There are five words in it, so we need to create five ccsprite maps to draw these five blocks, but when we draw them one by one, it is a terrible thing. If you write a lot of words in your game, you can give up doing other things, because rendering calls are too many times, leading to low efficiency. To improve rendering efficiency, a dedicated class ccspritebatchnode will appear! He drew all the ccsprite in a rendering. So, I 'd like to say, if you want to improve your rendering efficiency, analyze how to use ccspritebatchnode, and place a large number of image genie in your game in ccspritebatchnode for rendering as much as possible, it can certainly increase FPS.
Cocos2d-x 2.0 after added in the lower left corner of the rendering batch statistics, huh, it seems to learn from the Ogre statistical method, why do we need to statistical rendering batch? It is to let us have an observed indicator during optimization. :

(5) Use a compressed texture
The iOS device uses the powervr display chip, which can directly read the PVR format. The source code of the PVR format texture has been analyzed in the previous blog, you can learn from the various compression formats supported by PVR. When developing IOS games, we should also try to use images in the appropriate compression format. For example, "PVR. CCZ ".
For more information about this part, see:
Http://blog.sina.com.cn/s/blog_6fbe210701015j7z.html
After some of the above optimization strategies, we should be able to greatly reduce memory usage and improve rendering efficiency. If you have any better solutions, you are welcome to submit them at any time.

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.