Optimization of background image in Cocos2d-x optimization

Source: Internet
Author: User

Because the background picture is saved in the scene for a long time, and there are many pictures, we can optimize it. We consider the following aspects of optimization:

1. Do not alpha channel

The feature of the background image is not transparent, so the texture format can be used without the alpha channel format, so the RBG565 format is more suitable for background images.

2. Puzzles

The background picture differs from other picture texture format, when we create the sprite table, there is no way to make the background picture of the RBG565 format and its texture picture (such as: RGBA4444) in an Elf table, so based on format considerations we can put multiple backgrounds in an elf table, However, note that this wizard is not large enough to fit into a large image file, some older devices are limited to a single file size, such as: IPod touch 4 is a single file can not exceed 2048 * 2048 pixel size.

3. The time to load into the texture cache

When do I load a background image to the texture cache? This problem mainly depends on the background image of the scene use frequency, if the frequency is high in the game when the initialization of the load. The frequency is lower than the scene background picture, which can be considered when loading into the scene. When the picture is loaded, it is possible to consider asynchronous loading because the background picture is relatively large loading time.

4, small texture picture, duplicate map

If the background of the scene is a monochrome or regular graphic, we can use a small texture image and repeat the map implementation. We used a 128x128 texture picture (backgroundtile.png) on a repeated map in the 8th chapter to reduce memory consumption. The core code is as follows:

12345678 //贴图的纹理图片宽高必须是2的n次幂,128x128auto bg = Sprite::create("BackgroundTile.png",                             Rect(0, 0, visibleSize.width, visibleSize.height));  //贴图的纹理参数,水平重复平铺,垂直重复平铺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. Consider using tile maps

The background can be considered using tile map implementation. Tile map Because only a few small pictures can be built a large game background, its performance from needless to say, but its shortcomings are due to the use of a few tile mosaic, the background has a lot of duplication of areas, if users do not care about these, of course, the choice of tile map construction background is our preferred way. Also, do not exceed 4 layers in the map when designing the tile map.

6. Optimization of background Z depth

Sometimes in order to achieve the dynamic parallax effect, the background is divided into several pictures, we can put the clouds, trees, meadows and mountains are placed in different background images, we set the z-axis order (Z-order).

In this case we turn on the depth test. The depth test in OpenG es is to use the deep buffer algorithm to eliminate the non-meeting in the scene. To open the depth test in cocos2d-x, you can modify the code in AppDelegate.cpp as follows:

123456 boolAppDelegate::applicationDidFinishLaunching() {    … …     director->setDepthTest(true);    returntrue;}director->setDepthTest(true)开启深度测试,director是Director类是的对象指针。深度测试默认是关闭,如果开启深度测试会消耗更多的电量。

Source URL: http://blog.csdn.net/tonny_guan/article/details/41016985

Optimization of background image in Cocos2d-x optimization

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.