我有一个Cocos2d项目,我希望整个应用程序有一个恒定的背景。在其委托的applicationDidFinishLaunching
方法中,我替换了以下行:
我已经将glView的pixelFormat从kEAGLColorFormatRGB565
更改为kEAGLColorFormatRGBA8
。当我进行该更改时,glView 变得透明,我可以透过它看到它,但 fps 急剧下降。如果我不进行该更改,视图不会变得透明,但我看不到 fps 的巨大下降。我说的是 fps 的显着下降,从 59.0-60.0 下降到大约 35.0-42.0。
我在上面的 addSubview 行下方使用此代码以使视图透明:
director.openGLView.opaque = NO;
整个应用程序DidFinishLaunch方法如下所示:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
//
// Create the EAGLView manually
// 1. Create a RGB565 format. Alternative: RGBA8
// 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
//
//
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
// attach the openglView to the director
[director setOpenGLView:glView];
glView.opaque = NO;
// // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");
//
// VERY IMPORTANT:
// If the rotation is going to be controlled by a UIViewController
// then the device orientation should be "Portrait".
//
// IMPORTANT:
// By default, this template only supports Landscape orientations.
// Edit the RootViewController.m file to edit the supported orientations.
//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
// make the OpenGLView a child of the view controller
[viewController setView:glView];
//Required in iOS6, recommended in 4 and 5
[window setRootViewController:viewController];
// make the View Controller a child of the main window, needed for iOS 4 and 5
[window addSubview: viewController.view];
[window makeKeyAndVisible];
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// Removes the startup flicker
[self removeStartupFlicker];
// Run the intro Scene
[[CCDirector sharedDirector] runWithScene: [Intro scene]];
}
关于为什么会发生这种情况的任何想法?如果需要,我可以提供更多代码。
我忘了提到我所做的另一个代码更改。在CCDirector.m setGLDefaultValues
中,我从这里更改了这一行:
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
对此:
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
每当你添加一个透明的alpha通道时,OpenGL引擎将不得不执行某种形式的(尽管是默认的)alpha混合。 这是每个像素的数学运算,在非RGBA(即RGB 565)情况下不必这样做。 任何时候你加上引擎所需的数学,帧速率都会下降。
意料之中的。RGBA8888帧缓冲使用两倍的内存,GPU 需要执行更多工作才能渲染到此帧缓冲中。这就是为什么RGB565是默认格式的原因。