setDesignResolutionSize 到竖屏下面就缩放尺寸不对的问题

来源:互联网 发布:淘宝手机端gif图 编辑:程序博客网 时间:2024/06/15 12:30

    之前做的都是横版的游戏,然后一般使用如下的代码来适配多分辨率:

     

typedef struct tagResource{    CCSize sizeInPixel;    CCSize sizeDesign;    char directory[100];}Resource;static Resource resPhone  =  { CCSizeMake(480, 320), CCSizeMake(480, 320), "iphone" };static Resource resPhoneRetina =  { CCSizeMake(960, 640), CCSizeMake(480, 320), "iphonehd"   };static Resource resPhone5Retina =  { CCSizeMake(1136, 640), CCSizeMake(568, 320), "iphonehd"   };static Resource resTable =  { CCSizeMake(1024, 768), CCSizeMake(512, 384), "ipad"   };static Resource resTableRetina  =  { CCSizeMake(2048, 1536), CCSizeMake(512, 384), "ipadhd" };


然后在  cocos的 

bool AppDelegate::applicationDidFinishLaunching()函数中,通过如下的代码来适配:

    // initialize director    CCDirector *pDirector = CCDirector::sharedDirector();    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());    //设置素材目录    CCSize frameSize = pEGLView->getFrameSize();    Resource actualResource;    float actualHeight = fmin(frameSize.width, frameSize.height);    float actualWidth = fmax(frameSize.width, frameSize.height);        if (actualHeight > resTable.sizeInPixel.height)    {        actualResource = resTableRetina;    }    else if (actualHeight > resPhoneRetina.sizeInPixel.height)    {        actualResource = resTable;    }    else if (actualHeight > resPhone.sizeInPixel.height)    {        actualResource = resPhoneRetina;        if (actualWidth > resPhoneRetina.sizeInPixel.width)        {            actualResource = resPhone5Retina;        }    }    else    {        actualResource = resPhoneRetina;    }    CCFileUtils::sharedFileUtils()->setResourceDirectory(actualResource.directory);    pDirector->setContentScaleFactor(1.0 *actualResource.sizeInPixel.height / actualResource.sizeDesign.height);    // Set the design resolution    pEGLView->setDesignResolutionSize(actualResource.sizeDesign.width, actualResource.sizeDesign.height, kResolutionNoBorder);

然后查了好久才发现原因:在于 

setDesignResolutionSize  这个函数,先看看这个函数的实现:


    m_obDesignResolutionSize.setSize(width, height);

    

    m_fScaleX = (float)m_obScreenSize.width /m_obDesignResolutionSize.width;

    m_fScaleY = (float)m_obScreenSize.height /m_obDesignResolutionSize.height;

这个是关键的代码,这个代码是将传进去的第1,2个参数当作设计尺寸的width跟height,然后跟 screenSize的width,height分别求比值来确定缩放比例。


screenSize的 来源在哪,看到了2个函数 

const CCSize&CCEGLViewProtocol::getFrameSize() const

{

    returnm_obScreenSize;

}


void CCEGLViewProtocol::setFrameSize(float width,float height)

{

    m_obDesignResolutionSize =m_obScreenSize = CCSizeMake(width, height);

}


发现这个就是 跟 

    CCSize frameSize = pEGLView->getFrameSize();是一个size,即是这个frameSize,而frameSize是跟横竖屏有关系的。

1.  如果是 横屏,那frameSize的width跟height就是 960 * 640 ,  刚好是 对应了 design这个结构体的width(480)跟height(320)两个成员。

     所以传递给 setDesignResolutionSize 的就依次是  

actualResource.sizeDesign.width (值480)  跟actualResource.sizeDesign.height(值 320), 跟 960 * 640对应。


2. 如果是 竖屏,那么frameSize的width跟height就是 640 * 960 ,对应的就是 design的 结构体的height(320)跟width(480)两个成员。


    所以传递给 setDesignResolutionSize就应该依次是 

actualResource.sizeDesign.height(值 320) 跟actualResource.sizeDesign.width (值480),跟 640 * 960对应。



综上,如果是横屏,那么应该使用如下的代码:

    // Set the design resolution    pEGLView->setDesignResolutionSize(actualResource.sizeDesign.height, actualResource.sizeDesign.width, kResolutionNoBorder);






原创粉丝点击