首先可以参考:http://www.cocoachina.com/bbs/read.php?tid=195955
无论是真机还是模拟器,只要出现白底蓝底问题,基本上都可以通过修改glSurfaceView.setEGLConfigChooser的参数进行修正。
在默认情况下,cocos2dx会将C++中的AppDelegate::initGLContextAttrs()中设置的glContextAttrs传给java中的glSurfaceView.setEGLConfigChooser。
C++:
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs <span style="color:#ff0000;">glContextAttrs = {8, 8, 8, 8, 24, 8}</span>;
GLView::setGLContextAttrs(glContextAttrs);
}
Cocos2dxActivity.java:
<span style="font-family:helvetica neue;color: rgb(51, 51, 51);"> public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
//this line is need on some device if we specify an alpha bits
if(this.mGLContextAttrs[3] > 0) glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);</span><span style="color:#333333;">
</span><span style="color: rgb(51, 51, 51); font-family: 'helvetica neue';">...</span><span style="font-family: 'helvetica neue';"><span style="color:#333333;">
cocos2dEGLConfigChooser chooser = new cocos2dEGLConfigChooser(</span><strong><span style="color:#ff0000;">this.mGLContextAttrs</span></strong><span style="color:#333333;">);
glSurfaceView.setEGLConfigChooser(chooser);
return glSurfaceView;
}</span></span>
如果是模拟器,cocos2dx还作了特别处理:
Cocos2dxActivity.java:
public void init() {
...
// Switch to supported OpenGL (ARGB888) mode on emulator
if (isAndroidEmulator())
<span style="color:#ff0000;">this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);</span>
...
}
最后一个参数是0,禁止了模板缓冲,导致了渲染错误。
本人用的是android自带的模拟器,而不是android x86虚拟机,前者可以使用Host GPU,后者,本人安装的是4.0rc,不支持OpenGL ES(据说4.4之后才支持),根本无法运行cocos2dx程序。
解决办法就是将下列语句删除:
if (isAndroidEmulator())
<span style="color:#ff0000;">this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);</span>