文章出處

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


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()