Search code examples
cocos2d-xscreen-resolution

Multiple Resolution Support in Cocos2d-x v2.2.5 in Portrait mode


I am working on a game in cocos2d-x which is in portrait mode.

Now, for a long time now, I've been working on how to properly achieve multi resolution in cocos2d-x but failed. I followed this great tutorial on forum, but it wasn't enough, I also searched a lot but couldn't find a solution.

I also tried with different-different policies which are available with cocos-x.

I went through all the following links & tutorials

Using these links I could achieve for all ios resolutions but not for all android screens.

I even tried with newer version of cocos2d-x, but they also not providing anything which can support both ios and android screens.


Solution

  • It's working for me this way for Portrait mode:

    Below the header :

    typedef struct tagResource
    {
    
         cocos2d::CCSize size;
         char directory[100];
    
    } Resource;
    
    static Resource smallResource   =  { cocos2d::CCSizeMake(640, 960),"iPhone" };
    
    static Resource mediumResource  =  { cocos2d::CCSizeMake(768, 1024),"iPad"};
    
    static Resource largeResource   =  { cocos2d::CCSizeMake(1536, 2048),"iPadhd" };
    
    static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(768, 1024);
    

    in applicationDidFinishLaunching() method :

     // initialize director
    
        CCDirector* pDirector = CCDirector::sharedDirector();
        CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
    
        pDirector->setOpenGLView(pEGLView);
    
    
        CCSize frameSize = pEGLView->getFrameSize();
    
        std::vector<std::string> searchPaths;
    
        // Set the design resolution
    
    
        pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionExactFit);
    
        if (frameSize.height <= smallResource.size.height)
        {
            searchPaths.push_back(mediumResource.directory);      
            CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);    
    
        pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);
    
        }
        else if (frameSize.height <= mediumResource.size.height)
        {
            searchPaths.push_back(mediumResource.directory);
            CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
        pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);
        }
        else
        {
            searchPaths.push_back(largeResource.directory);
            CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
        pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height);
    
       }