The size of setDesignResolutionSize to the vertical screen is incorrect.

Source: Internet
Author: User

Previously, I used a horizontal game, and then generally used the following code to adapt to multiple resolutions:

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


Then in the cocos

In the bool AppDelegate: applicationDidFinishLaunching () function, use the following code to adapt:

// Initialize director CCDirector * pDirector = CCDirector: sharedDirector (); CCEGLView * pEGLView = CCEGLView: sharedOpenGLView (); pDirector-> setOpenGLView (CCEGLView: sharedOpenGLView ()); // set the clip directory 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 );

Then I checked it for a long time to find out the cause:

SetDesignResolutionSize:

M_obDesignResolutionSize.setSize (width, height );

M_fScaleX = (float) m_obScreenSize.width/
M_obDesignResolutionSize.width;

M_fScaleY = (float) m_obScreenSize.height/
M_obDesignResolutionSize.height;

This is the key code. The Code regards the 1 or 2 parameters passed in as the width and height of the design size, and then calculates the ratio with the width and height of the screenSize to determine the scaling ratio.

Where is the screenSize source? two functions are displayed.

Const CCSize &
CCEGLViewProtocol: getFrameSize () const

{

Return
M_obScreenSize;

}

Void CCEGLViewProtocol: setFrameSize (float width,
Float height)

{

M_obDesignResolutionSize =
M_obScreenSize = CCSizeMake (width, height );

}

I found that

CCSize frameSize = pEGLView-> getFrameSize (); it is a size, that is, this frameSize, and frameSize is related to the landscape.

1. If it is a horizontal screen, the width and height of frameSize are 960*640, which exactly corresponds to the width (480) and height (320) members of the design struct.

So the values passed to setDesignResolutionSize are

ActualResource. sizeDesign. width (value: 480) corresponds to actualResource. sizeDesign. height (value: 320), and 960*640.


2. If it is a portrait screen, the width and height of frameSize are 640*960, which correspond to the height (320) and width (480) members of the design struct.


Therefore, the size passed to setDesignResolutionSize should be

ActualResource. sizeDesign. height (value: 320) and actualResource. sizeDesign. width (value: 480 ),
640*960.



In summary, the following code should be used if the screen is landscape:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.