智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

cocos2d-x 屏幕分辨率适配方法

Posted on 2015-05-12 17:17  Bill Yuan  阅读(638)  评论(0编辑  收藏  举报

转自:http://blog.csdn.net/somestill/article/details/9950403

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
    
    


    CCSize frameSize = CCEGLView::sharedOpenGLView()->getFrameSize();
    CCSize lsSize = CCSizeMake(960, 640);
    float scaleX = (float)frameSize.width/lsSize.width;
    float scaleY = (float)frameSize.height/lsSize.height;
    
    float scale = 0.0f; // MAX(scaleX, scaleY);
    if (scaleX > scaleY) {
        // 如果是 X 方向偏大,那么 scaleX 需要除以一个放大系数,放大系数可以由枞方向获取,
        // 因为此时 FrameSize 和 LsSize 的上下边是重叠的
        scale = scaleX / (frameSize.height / (float) lsSize.height);
    } else {
        scale = scaleY / (frameSize.width / (float) lsSize.width);
    }
    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(lsSize.width * scale, lsSize.height * scale,  kResolutionNoBorder);

    
    

    // turn on display FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}

首先,在前期进行拉伸,之后,如果出现黑边,后期,在通过调整背景图片进行填充:

CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    
    if ((int)(visibleSize.width + 0.5) != 960 ) {
        float max_width = MAX(visibleSize.width, 960);
        float min_width = MIN(visibleSize.width, 960);
        m_bg->setScaleX(max_width/min_width);
    }
    else{
        float max_width = MAX(visibleSize.height, 640);
        float min_width = MIN(visibleSize.height, 640);
        m_bg->setScaleY(max_width/min_width);
    }

这样,就能既不失真,又不会出现黑边。 推荐看这里,讲解的非常详细。

// 组[1] :
FrameSize:            width = 720, height = 420
WinSize:          width = 720, height = 480
VisibleSize:      width = 720, height = 420
VisibleOrigin:        x = 0, y = 30

// 组[2] :相比 组 [1] FrameSize 不变 VisibleSize 和 VisibleOrigin 随着 WinSize 的变小而变小
FrameSize:            width = 720, height = 420
WinSize:          width = 480, height = 320
VisibleSize:      width = 480, height = 280
VisibleOrigin:        x = 0, y = 20

// 组[3] : 相比组 [1] WinSize 不变,VisibleSize 随着 FrameSize 的比例改变而改变
FrameSize:            width = 720, height = 540
WinSize:          width = 720, height = 480
VisibleSize:      width = 640, height = 480
VisibleOrigin:        x = 40, y = 0

// WinSize VisibleSize VisibleOrigin 与都设计的分辨率相关,满足如下关系
WinSize.width = (VisibleOrigin.x * 2) + VisibleSize.width
WinSize.height = (VisibleOrigin.y * 2) + VisibleSize.height

其中,frameSize为屏幕分辨率,winSize为设计分辨率,visibleSize为可用分辨率(可用区域)

 

FrameSize 是实际的屏幕分辨率,而 VisibleSize 是在 WinSize 之内,保持 FrameSize 的宽高比所能占用的最大区域,实际屏幕分辨率可以大于 WinSize ,但VisibleSize 一定会小于或者等于 WinSize,这两者相同的是宽高比。VisibleSize 有着 WinSize 大小(随WinSize 的大小改变而改变),还有着 FrameSize 的宽高比,它标示 在设计分辨率(WinSize)下,在屏幕中的可见区域大小。

我们可以通过如下方法获取到 setDesignResolutionSize 所设置的值:

CCSize winSize = CCDirector::sharedDirector()->getWinSize();