zoukankan      html  css  js  c++  java
  • [转]在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

  • 相关阅读:
    nyoj 115------城市平乱( dijkstra // bellman )
    nyoj-----284坦克大战(带权值的图搜索)
    nyoj-----42一笔画问题
    hdu-------1081To The Max
    nyoj------170网络的可靠性
    HDUOJ-------1052Tian Ji -- The Horse Racing(田忌赛马)
    初学Java之Pattern与Matcher类
    初学java之StringBuffer类的常用方法
    初学java之大数处理
    hdu---1024Max Sum Plus Plus(动态规划)
  • 原文地址:https://www.cnblogs.com/qilinzi/p/3599211.html
Copyright © 2011-2022 走看看