Background Image Optimization in Cocos2d-x optimization, cocos2d-x background image

Source: Internet
Author: User

Background Image Optimization in Cocos2d-x optimization, cocos2d-x background image
As the background image is stored in the scene for a long time and there are many images, we can optimize it. We will consider optimization through the following aspects:
1. Do not use Alpha Channels
The background image is not transparent, so the texture format can be in a format without Alpha channels. Therefore, the RBG565 format is suitable for background images.
2. puzzles
The background image is different from other image texture formats. When creating a Sprite table, we cannot compare the background image in RBG565 format with other texture images (such as RGBA4444) in a Sprite table, we can place multiple backgrounds in one sprite table based on the format. However, you must note that the big image file spliced by the sprite table cannot be too large, some old devices have limits on the size of a single file. For example, iPod touch 4 cannot exceed 2048*2048 pixels for a single file.
3. Loading time to texture Cache
When can I load the background image to the texture cache? This problem mainly depends on the Use Frequency of this background image scenario. If the frequency is high, it is necessary to load it during game initialization. For background images with low frequencies, you can consider loading them when entering the scenario. During image loading, asynchronous loading can be considered because the background image is relatively large for a long time.
4. Duplicate textures for small-texture images
If the background of a scene uses a monochrome or regular image, we can use a small texture image and repeat the texture. In chapter 8, we added a x texture image (backgroundtile.png) to reduce memory consumption. The core code is as follows:
// The texture image width and height must be 2 to n power, 128x128
Auto bg = Sprite: create ("BackgroundTile.png ",
Rect (0, 0, visibleSize. width, visibleSize. height ));
// Texture parameters of the texture, horizontal and vertical
Texture2D: TexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT };
Bg-> getTexture ()-> setTexParameters (tp );
Bg-> setPosition (origin + Vec2 (visibleSize. width/2, visibleSize. height/2 ));
AddChild (bg, 0 );


5. Use a tile Map
The background can be implemented using a tile map. A tile map can be used to build a large game background with only a few small pictures. Its performance does not need to be said, but its disadvantage is that it is made of several tiles, there are many repeated areas in the background. If you don't care about this, of course, choosing a tile map to build the background is our first choice. In addition, when designing a tile map, the layer in the map should not exceed four layers.


6. Background z depth Optimization
Sometimes, in order to achieve the dynamic parallax effect, the background is divided into several images. We can place clouds, trees, lawns, and mountains in different background images, we set the z-order ).




In this case, we start the deep test. In OpenG ES, the deep test uses the deep cache algorithm to eliminate the possibility of an unfamiliar scenario. Turn on the deep test in the Cocos2d-x and you can modify the code in AppDelegate. cpp as follows:
Bool AppDelegate: applicationDidFinishLaunching (){
... ...
Director-> setDepthTest (true );


Return true;

}

Director-> setDepthTest (true) enables deep testing. director is the object pointer of the Director class. By default, deep test is disabled. Enabling deep test consumes more power.



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

Related Article

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.