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

前端之家收集整理的这篇文章主要介绍了cocos2d-x 屏幕分辨率适配方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_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;
}
首先,在前期进行拉伸,之后,如果出现黑边,后期,在通过调整背景图片进行填充: @H_301_0@

@H_301_0@

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);
    }

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

// 组[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所设置的值:

<span class="line-number">1</span>
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 
原文链接:https://www.f2er.com/cocos2dx/341735.html

猜你在找的Cocos2d-x相关文章