Cocos2d-x and uikit mixed programming to achieve translucent Effect

Source: Internet
Author: User
Keywords

Cocos2d-x, uikit, transparent.

Problem

The cocos2d-x uses a dedicated OpenGL view for rendering, and its rendering is separated from the uikit, So we usually use the cocos2d-x full screen display, such as Ray's tutorial (http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit) (cocos2d-x and cocos2d have no essential difference in this article)

If we insert cocos2d as a sub-view, cocos2d scene will be a black rectangle without anything, and cannot be transparent. it is useless to set the background color of OpenGL view to clearcolor. can cocos2d become transparent?

Through Bing difficult to find this self-answer, http://stackoverflow.com/questions/12846744/cocos2d-opengl-es-becoming-transparent-over-uiview, although only one like, but it is indeed the only online answer to solve the problem, must be recorded to future generations.

Solution

1. Be sure to select OpenGL view during initializationKeaglcolorformatrgba8Pixel mode to support transparency.

    [CCEAGLView viewWithFrame: CGRectMake(0, 0, winHeight, winWidth)                  pixelFormat: kEAGLColorFormatRGBA8                  depthFormat: GL_DEPTH24_STENCIL8_OES           preserveBackbuffer: NO                   sharegroup: nil                multiSampling: NO              numberOfSamples: 0];

2. To enable the transparent mode for OpenGL view, there isOpaqueAttribute. The default value is yes, indicating that the content of the covered area will be automatically blocked. This is to optimize the rendering and fast cropping. We need to set it to No.

glClearColor(0, 0, 0, 0);_cocosView.backgroundColor = [UIColor clearColor];_cocosView.opaque = NO;

3. cocos2d-xClear colorSet to (0, 0, 0, 0 ). this value is the default value that OpenGL sets for each pixel when rendering each frame. because the rendering of cocos2d-x and uikit is implemented in different OpenGL pipelines, because the cocos2d-x will replace the content of uikit rendering with the black screen.

This need to modify the source code of the cocos2d-x, inCcdirector. cppOfVoid Director: setgldefavaluvalues ().

With the above three steps, we can now transparent cocos2d-x interface, mask above the uikit.

 

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.