cocos2d-x适配多分辨率

现在用的2d-x版本是2.1.1。现在的项目要求是iphone ,iphone Retina,ipad和ipad Retina都有各自的路径来存放各自需要的资源。在AppDelegate的

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)

    {

        searchPath.push_back(iPadHDResource.directory);

        searchPath.push_back(iPhoneHDResource.directory);

        

        designSize = designIpadResolutionSize;

        pEGLView->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);

        

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

}


这样设置路径之后,进入游戏时会在 SearchPaths里寻找所需要的资源。需要注意的是各个路径下的资源文件相同就行了,不用根据是否未Retina而添加-HD后缀。

原文地址:https://www.cnblogs.com/dyllove98/p/3170160.html