Cocos2d-x Optimization in image optimization, cocos2d-x optimization pictures

Source: Internet
Author: User

Cocos2d-x Optimization in image optimization, cocos2d-x optimization pictures

In 2D games, images are undoubtedly the most important resource files. They are loaded into the memory and converted to textures, which are rendered by the GPU on top of the genie. It can be optimized in many aspects, including: image format, puzzle and texture format. Below we will introduce the optimization of one piece and texture from these aspects.
1. Select the image format
To answer this question, we need to first understand the image file formats currently used on mobile platforms, and whether these image format Cocos2d-x support. There are many image formats, but the PNG and JPG formats recommended for mobile platforms can also be considered. It is best to convert other file formats into PNG formats. Let's take a look at their features first.
1. PNG file
The PNG file format is designed to replace GIF and TIFF file formats. It is a bitmap storage format. PNG adopts lossless compression and supports transparent Alpha channel data, but does not support animation. PNG can save high-fidelity and complex images, but the files are relatively large. The PNG format is divided into: PNG8 and PNG24. The following numbers represent the maximum value of color that can be indexed and stored in this PNG format.
2. JPG
The full name of JPG is JPEG. JPG images store a single bitmap image in 24-bit color. JPG is a platform-independent format that supports the highest level of compression. The compression ratio can be as high as. This compression is at the cost of image quality, in exchange for smaller file sizes. JPG does not support transparency. JPG supports photographic images or realistic images because they are rich in color. JPG is not suitable for simple images with very few colors, areas with similar color blocks, or with very obvious brightness differences.


So should we choose JPG or PNG? Many people think that JPG files are relatively small. PNG files are relatively large and loaded to memory textures. JPG occupies less memory. This is wrong! Texture and image are two different concepts. If the texture is a camping tent, the image format is to store the collapsed tent bag and the size of the bag containing the tent, it cannot be the size of the tent. Especially in the Cocos2d-x platform after JPG loading is converted to PNG format, and then converted to texture, stored in the memory, so virtually increased the JPG file decoding time, therefore, no matter how good JPG is on other platforms, it cannot be compared with PNG on mobile platforms.









2. puzzles
I wonder if you have had such a question. Why do we need to splice all the small and medium-sized images into a large image? When using the genie table, let's briefly introduce the cause of this problem.
If you splice multiple small images into a large image (texture map or Sprite table), you can reduce the I/O operation. In addition, each time you use a scatter chart, you need to create a Sprite and add it to the texture cache. If you frequently create a large number of images, the overhead of CPU and memory is high. Using a large image, you can create a Sprite frame cache and add their textures to the texture cache at a time, which significantly improves the efficiency.
The smaller the size of an image, the better, if the image can be spliced to meet the user's fidelity. We can through TexturePacker and other texture puzzle tools, set texture support NPOT, the use of these tools can refer to our (Cocos2d-x practice (Volume IV): Detailed description of the tool).
So what is NPOT? NPOT is short for "non power of two", meaning N power of not 2. In OpenGL ES1.1, the texture image must be 2 to N power (that is, POT); otherwise, the texture cannot be created. POT requires that you use a texture tool to splice a large image into a large image, and there may be many blank areas. As shown in, there are some blank areas in the lower right corner, which may cause waste and increase the size of the image. The size of the image shown is 2048KB.

POT puzzle OpenGL ES2.0 and later support NPOT, we do not need to worry about whether the image is 2's N power, is to use NPOT puzzle, the entire picture basically has no big blank area, make full use of the image space. The image size shown in 20-24 is kb, saving kb.

NPOT puzzle

More content please pay attention to the first domestic Cocos2d-x 3.2 version of the book "Cocos2d-x practice: C ++ volume" book exchange discussion site: http://www.cOcoagame.net
For more exciting video courses, please follow the Cocos course in Zhijie class: http: // v.51wOrk6.com
Welcome to the Cocos2d-x Technology Discussion Group: 257760386 welcome to the knowledge of the iOS classroom public platform

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.