zoukankan      html  css  js  c++  java
  • 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后缀。

  • 相关阅读:
    机器学习十大算法之EM算法
    如何利用OpenSSL生成证书
    2018中国云原生用户大会:网易云爆料完整微服务的研发过程
    漫话中文分词
    10分钟快速构建汽车零售看板
    聊一聊整车厂的那些事——售后配件业务
    网易有数的“正确”使用方式——洞察数据中隐藏的故事
    深入浅出“跨视图粒度计算”--3、EXCLUDE表达式
    深入浅出“跨视图粒度计算”--2、INCLUDE表达式
    深入浅出“跨视图数据粒度计算”--1、理解数据的粒度
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3170160.html
Copyright © 2011-2022 走看看