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