Cocos2d-x 3.0 experience (01)-image loading and hybrid mode

Source: Internet
Author: User

Recently I started to use cocos2dx 3.0 to make things. I have a slight heart (cao) to get (dian). Let's record it a little.

Compared with v2.2, v3.0 is most noticeable in terms of the optimization of the touch level and the introduction of lambda callback functions (well, I don't think I have changed so many classes of names. In other words, every time cocos2dx is updated, there will always be a bunch of class name letters ). These features should have been studied by many people, so today we will talk about something related to images.


V3.0 has also made great changes in image loading, but it is only hidden under water, so it is not easy to find out when there is a problem.

In short, in v2.2 and earlier versions, on iOS, cocos2dx uses native UIImage to load images, such as png and jpg. General steps: first load the image with UIImage, then create a rendering environment and provide a buffer as the rendering cache, and then draw the UIImage (including CGImage) into this environment, at this time, the buffer contains the pixel data of the image, which can be used to create the gl texture.

V3.0 no longer uses the iOS native library to load images, but uses third-party libraries such as libpng and libjpeg, which can be found under cocos2d/external. In the new method, you do not need to build a rendering environment in the middle. Instead, you can directly parse the image file to the buffer and then create the gl texture.

This modification is a good thing, both in terms of efficiency and code consistency. However, it also brings some unaccustomed things.

When we use v3.0 as a particle, we will find that, with the same settings and images, the effects of previous good ends have become a bunch of color blocks under the new version. After checking for a while, we finally changed the particle's mixed mode (BlendFunc) from src = GL_ONE, dst = GL_ONE to src = GL_SRC_ALPHA, dst = GL_ONE, and the result returned to normal.

The reason is not complex. First, the image we use is a pure white but translucent image, that is, the RGB value of the image is 255, but Alpha is 0 ~ 255, depending on the transparency of each pixel. In any image-watching software, this image is white in the middle and gradually transparent around, because there is an alpha channel for semi-transparent mixing. However, in the program, src = GL_ONE indicates that the image's alpha is ignored and the RGB value is directly used to mix with the target. Because the RGB value of the image is 255, therefore, the rendering result becomes a white color block. To src = GL_SRC_ALPHA, the image alpha is used for RGB modulation to obtain the correct result.

Well, why is there no problem before? In the previous step of drawing the image from CGImage to the buffer, the image alpha has actually been modulated by RGB, so the RGB in the buffer is no longer the original RGB image, and the results will certainly be different. By the way, if you use v2.2 or a previous version and find that some images or particles are displayed normally on iOS, and the color block is displayed on Android, it may be related to this. Try to change the hybrid mode.

There is no way to keep the src = GL_ONE settings. Premultiply the image by alpha (Premultiplied Alpha), that is, the image is first modulated by alpha, and then saved back to RGB, you can continue using src = GL_ONE. Well, TexturePacker is a tool that can handle this kind of processing.


Now, let's talk about the mixed mode. Finally, let's talk about cocos2dx and cocosbuilder.

1. In cocosbuilder, image loading should use a native library, so cocos2dx v3.0 has no WYSIWYG effect on builder.

2. In cocosbuilder, if the hybrid mode of CCSprite is set to src = one and dst = one-src alpha, the hybrid mode will be ignored when exporting ccbi! How many bytes are saved for half-cent use?

3. cocos2dx 3.0, when parsing particles from cocosbuilder, when parsing the Texture, it will secretly get rid of the mixed mode. In fact, when setting Texture for the genie and particles, you can see the figure of modifying the hybrid mode. Needless to say, this challenge is to solve the mixed problem like above. But for those who want to control their own hybrid methods ...... I found out after I complained to me about the effect of modifying the particle Mixing Mode n times.

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.