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.