applicationDidFinishLaunching()函数中根据屏幕分辨率来设置
static Resource iPhoneResource = {CCSizeMake(480,320), "iPhone" };
static Resource iPhoneHDResource = {CCSizeMake(480*2,320*2), "iPhoneHD" };
static Resource iPhoneTallerResource = {CCSizeMake(1136,640), "iPhoneTaller"};
static Resource iPadResource = {CCSizeMake(1024,768), "iPad" };
static Resource iPadHDResource = {CCSizeMake(2048,1536),"iPadHD" };
static CCSize designResolutionSize =CCSizeMake(480,320);
static CCSize designTallerResolutionSize =CCSizeMake(568,320);
static CCSize designIpadResolutionSize =CCSizeMake(512,384);
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
std::vector<std::string> searchPath =CCFileUtils::sharedFileUtils()->getSearchPaths();
// Set the design resolution
pEGLView->setDesignResolutionSize(designResolutionSize.width,designResolutionSize.height,kResolutionNoBorder);
CCSize designSize =designResolutionSize;
CCSize frameSize = pEGLView->getFrameSize();
_isLongiPhone =false;
_isIpadHD = false;
_isIpadNormal =false;
// In this demo, we select resource according to the frame's height.
// If the resource size is different from design resolution size, you need to set contentScaleFactor.
// We use the ratio of resource's height to the height of design resolution,
// this can make sure that the resource's height could fit for the height of design resolution.
// if the frame's height is larger than the height of medium resource size, select large resource.
if (frameSize.height ==iPadHDResource.size.height && frameSize.width ==iPadHDResource.size.width)
{
//iPhoneHDResource.directory是共用路径,这个路径下有共用资源。
searchPath.push_back(iPadHDResource.directory);
searchPath.push_back(iPhoneHDResource.directory);
//设计屏幕大小是512x384
designSize = designIpadResolutionSize;
pEGLView->setDesignResolutionSize(designSize.width, designSize.height,kResolutionNoBorder);
//实际屏幕大小是2048x1536,所以设置ContentScaleFactor为4.
pDirector->setContentScaleFactor(iPadHDResource.size.height/designIpadResolutionSize.height);
_isIpadHD = true;
}
// if the frame's height is larger than the height of small resource size, select medium resource.
elseif (frameSize.height ==iPadResource.size.height && frameSize.width ==iPadResource.size.width)
{
searchPath.push_back(iPadResource.directory);
searchPath.push_back(iPhoneHDResource.directory);
designSize = designIpadResolutionSize;
pEGLView->setDesignResolutionSize(designSize.width, designSize.height,kResolutionNoBorder);
pDirector->setContentScaleFactor(iPadResource.size.height/designIpadResolutionSize.height);
_isIpadNormal =true;
}
// if the frame's height is smaller than the height of medium resource size, select small resource.
elseif (frameSize.height ==iPhoneResource.size.height && frameSize.width ==iPhoneResource.size.width)
{
searchPath.push_back(iPhoneResource.directory);
searchPath.push_back(iPhoneHDResource.directory);
pDirector->setContentScaleFactor(iPhoneResource.size.height/designResolutionSize.height);
}
elseif (frameSize.height ==iPhoneHDResource.size.height && frameSize.width ==iPhoneHDResource.size.width){
searchPath.push_back(iPhoneHDResource.directory);
pDirector->setContentScaleFactor(iPhoneHDResource.size.height/designResolutionSize.height);
designSize = designResolutionSize;
pEGLView->setDesignResolutionSize(designSize.width, designSize.height,kResolutionNoBorder);
}
elseif (frameSize.height ==iPhoneTallerResource.size.height && frameSize.width ==iPhoneTallerResource.size.width){
//push taller resources" directory first,so look for resources in taller resources" directory first
searchPath.push_back(iPhoneTallerResource.directory);
searchPath.push_back(iPhoneHDResource.directory);
designSize = designTallerResolutionSize;
pEGLView->setDesignResolutionSize(designSize.width, designSize.height,kResolutionNoBorder);
pDirector->setContentScaleFactor(iPhoneTallerResource.size.height/designResolutionSize.height);
_isLongiPhone =true;
}
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
}