[转]在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率

http://cankeyyin.blog.163.com/blog/static/12336178320124149391202/

原理:将iphone的hd图片给ipad用,即:

  • 使用原iphone版HD资源(960*640),不用适配到1024*768,四周可留黑边
  • 不改动原有逻辑代码

 

经测试,这个适配方法可以让一份代码同时运行与iphone、iphone retina、ipad、ipad retina四种分辨率。

 

 

1.让ipad能够读入HD图片

在cocos2d-x源代码CCFileUtils_ios.mm中,将所有

cocos2d::CC_CONTENT_SCALE_FACTOR() == 2

改成

cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 || (UI_USER_INTERFACE_IDIOM() ==UIUserInterfaceIdiomPad 

 

2.让ipad显示640*960游戏区域,并居中

在iOS/AppController.mm中,将

EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]

                               pixelFormat: kEAGLColorFormatRGBA8

                               depthFormat: GL_DEPTH_COMPONENT16_OES

                        preserveBackbuffer: NO

                                sharegroup: nil

                             multiSampling: NO

                           numberOfSamples: 0 ];

改成(此处为竖屏分辨率,横屏调换可x,y坐标):

    EAGLView *__glView = nil;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        __glView = [EAGLView viewWithFrame: CGRectMake(0, 0, 320, 480)

                               pixelFormat: kEAGLColorFormatRGBA8

                               depthFormat: GL_DEPTH_COMPONENT16_OES

                        preserveBackbuffer: NO

                                sharegroup: nil

                             multiSampling: NO

                           numberOfSamples: 0 ];

        __glView.transform = CGAffineTransformMakeScale(1.0, 1.0);

        __glView.frame = CGRectMake((768-640)/2,(1024-960)/2, 640,960);//屏幕窗口

    }

    else {

        __glView = [EAGLView viewWithFrame: [window bounds]

                               pixelFormat: kEAGLColorFormatRGBA8

                               depthFormat: GL_DEPTH_COMPONENT16_OES

                        preserveBackbuffer: NO

                                sharegroup: nil

                             multiSampling: NO

                           numberOfSamples: 0 ];

    }

 

3.开启Retina分辨率

classes/AppDelegate中:

pDirector->enableRetinaDisplay(true);

 

4.坐标调整 某些情况下坐标坐标会扩展到640,960 所以需要用百分比的形式表示坐标

int screenWidth=CCDirector::sharedDirector()->getWinSize().width;

int screenHeitht=CCDirector::sharedDirector()->getWinSize().height;

 

如果原来有用坐标跟常数运算,比如ccp(x+常数,y-常数)这一类,常数要变换一下

坐标常数=坐标常数* screenWidth/320;

 

 

5.以上步骤完成后,可以在iphone、iphone retina、ipad上正常运行,但不能在ipad retina上运行,要在pad retina运行,需要在源代码CCEGLView_ios.mm加入以下代码,

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return false;

bool CCEGLView::canSetContentScaleFactor()

    {

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return false;

        return [[EAGLView sharedEGLView] respondsToSelector:@selector(setContentScaleFactor:)]

        && [[UIScreen mainScreen] scale] != 1.0;

    }

 

 

6.将程序设置为universal形式

Build Settings->Deployment->Targeted Device Family设置成iphone/ipad

posted @ 2014-03-13 19:39  麒麟子MrKylin  阅读(838)  评论(0编辑  收藏  举报