[cocos2d-x] iphone5 填充黑边适配方案。

[cocos2d-x] iphone5 填充黑边适配方案。

看不惯黑边又不想拉伸的解决方案,很多游戏也这么处理,直接用边框填充黑边。


     //  Set RootViewController to window
     if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
                                         pixelFormat: kEAGLColorFormatRGBA8
                                         depthFormat: GL_DEPTH_COMPONENT16
                                  preserveBackbuffer: NO
                                          sharegroup:nil
                                       multiSampling:NO
                                     numberOfSamples:0];
        
        UIView* view = [[UIView alloc] initWithFrame:[window bounds]];
        [view setBackgroundColor:[UIColor blueColor]];
        
         //  Use RootViewController manage EAGLView
        viewController = [[[RootViewController alloc] initWithNibName:nil bundle:nil] autorelease];
        viewController.wantsFullScreenLayout = YES;
        viewController.view = __glView;
        
         //  warning: addSubView doesn't work on iOS6
        [window addSubview: viewController.view];
        
        s_rootView = __glView;
        s_rootViewController = viewController;
        
         //  Set RootViewController to window
        [window makeKeyAndVisible];
        [[UIApplication sharedApplication] setStatusBarHidden: YES];
        
        [window setRootViewController:viewController];
    }
     else
    {
         bool i5 =  false;
        
        CGRect r = [window bounds];
         float f = 1.0f * r.size.height / r.size.width - 960.0f / 640;
         // printf("---- %f,%f,%f", r.size.height, r.size.width, f);
         if (!(f < 0.001 && f > -0.001))
        {
            i5 =  true;
        }
        
        EAGLView *__glView = [EAGLView viewWithFrame: CGRectMake(0, i5 ? 44 : 0, 320, 480) // [window bounds]
                                         pixelFormat: kEAGLColorFormatRGBA8
                                         depthFormat: GL_DEPTH_COMPONENT16
                                  preserveBackbuffer: NO
                                          sharegroup:nil
                                       multiSampling:NO
                                     numberOfSamples:0];
        
        UIView* rootView = [[[UIView alloc] initWithFrame:[window bounds]] autorelease];
        [rootView addSubview:__glView];
        
         if (i5)
        {
             //  top
            UIImageView* topView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top.png"]] autorelease];
            [rootView addSubview:topView];
            [topView setFrame:CGRectMake(0, 0, 320, 44)];
        
             //  bottom
            UIImageView* bottomView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottom.png"]] autorelease];
            [rootView addSubview:bottomView];
            [bottomView setFrame:CGRectMake(0, 524, 320, 44)];
        
            s_sharedApplication.setKeyboardOffsetY(44 * 2);
        }
        
         //  Use RootViewController manage EAGLView
        viewController = [[[RootViewController alloc] initWithNibName:nil bundle:nil] autorelease];
        viewController.wantsFullScreenLayout = YES;
        viewController.view = rootView;

         //  use this method on ios6
        [window setRootViewController:viewController];
        
        s_rootView = rootView;
        s_rootViewController = viewController;
        
         //  Set RootViewController to window
        [window makeKeyAndVisible];
        [[UIApplication sharedApplication] setStatusBarHidden: YES];
    }

你可能感兴趣的:([cocos2d-x] iphone5 填充黑边适配方案。)