开发者

Cocos2d mixed with UIKit views - huge FPS drop

开发者 https://www.devze.com 2023-04-06 21:29 出处:网络
I have a game written in Cocos2d. On top of it I present some UIKit objects. Then I added a UIViewController to it handling a separate mini-game written using UIKit objects and UIView animations. Th

I have a game written in Cocos2d. On top of it I present some UIKit objects.

Then I added a UIViewController to it handling a separate mini-game written using UIKit objects and UIView animations. The code that I use to add this to my Cocos2D scene is the following:

gameVC = [[[UGameViewController alloc] 
                                      initWithNibName:@"UGameViewController" bundle:nil] 
                                     retain];

[[[CCDirector sharedDirector] openGLView] addSubview:gameVC.view];
gameVC.parentClass = self;
[gameVC viewDidAppear:YES];

The initialization of the uikit-game takes place in the viewDidAppear: method, that's why I call it from the controller.

When havi开发者_如何学Pythonng the view added this way I noticed that the animations I use there (zoom + translation + view flipping) are very, very choppy. Running the app on my iPad and looking at CoreAnimation intruments shows me a dramatic FPS drop - from 40-60 fps in the cocos2d scenes to 4-8 fps on the UIKit part.

Is there anything I can do in order to fix it somehow?


Edit: my CCDirector initialization is like this:

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    window = [[ShakeEnabledUIWindow 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];

    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:kEAGLColorFormatRGB565
                                   depthFormat:0                        
                            preserveBackbuffer:NO];

    [director setOpenGLView:glView];

#if GAME_AUTOROTATION == kGameAutorotationUIViewController
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif

    [director setAnimationInterval:1.0/60];     

    // make the OpenGLView a child of the view controller
    [viewController setView:glView];
    // make the View Controller a child of the main window
    [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];

    // Run the intro Scene
    [[CCDirector sharedDirector] runWithScene: [Main scene]];       
}


I take it you're using CoreAnimation to animate your UIKit views. That may be the problem. Cocos2D will not leave much processing time to your UIKit view animations, unless you pause Cocos2D or stop it entirely (CCDirector stopAnimation/startAnimation).

What you can do is to use a CCNode as a wrapper for each UIKit object that you're using. The CCNode contains the UIKit view and updates its position to be in synch with its own position. Then you can use the Cocos2D animation actions (ie. MoveTo with easing) to animate the node and thus the UIKit views.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号