androidc++11xcode6cocos2d-x-3.0

How multi resolution support for all android devices in cocos2d-x 3.0 in landscape mode?


I am converting an existing cocos2d game in to android but i am getting resolution problem there.In the existing cocos game , i am using there two resources folder one for IPAD and one for IPHONE.I want that using these existing resources folder , i can execute my android game. please give me any sample code which also work with android resolution.

previously i am using this below code for IOS

auto director = Director::getInstance();

auto glview = director->getOpenGLView();

std::vector searchPaths;

Size frameSize = glview->getFrameSize();

if (frameSize.height > 640)
{
    UserDefault::getInstance()->setIntegerForKey(DEVICE_TYPE,IPAD);

    searchPath.push_back(largeResource.directory);

    if(frameSize.height>768)
    {
        UserDefault::getInstance()->setBoolForKey("RETINA",true);

        Director::getInstance()->setContentScaleFactor(0.5f);

        UserDefault::getInstance()->setBoolForKey("RET", true);
    }

}
else
{
    UserDefault::getInstance()->setIntegerForKey(DEVICE_TYPE,IPHONE);
    searchPath.push_back(smallResource.directory);

    if(director->getWinSize().width == 960)
    {
        UserDefault::getInstance()->setBoolForKey(IPOD_5,false);

    }
    else
    {
        UserDefault::getInstance()->setBoolForKey(IPOD_5,true);

    }
}

UserDefault::getInstance()->flush();

FileUtils::getInstance()->setSearchPaths(searchPath);


Solution

  • There’s too many different screen sizes on the android devices. For example, below is most common values for now on phones (first value - resolution, second & third - aspect ratio):

    480x800 - 3:5 or 0.6
    480x854 - 0.5621
    768x1280 - 3:5 or 0.6
    720x1280 - 9:16 or 0.5625
    1080x1920 - 9:16 or 0.5625
    1440x2560 - 9:16 or 0.5625

    And for the tablets:

    2048x1536 (2048x1440) - 3:4 or 0.75 (0.7031)
    1280x800 (1280x752) - 10:16 or 0.625 (0.5875)
    1920x1200 (1920x1104) - 10:16 or 0.625 (0.575)
    2560x1600 (2560x1504) - 10:16 or 0.625 (0.5875)
    1024x600 (1024x552) - 0.5859 (0.539)

    Keep in mind that there’s status bar on the tablets. It take some resolution, and cocos application can’t see the total device resolution, but only truncated (I showed it in the brackets). Accordingly changes aspect ratio.
    All this leads to a very large selection of resolution values. Therefore it would be better to group the values for aspect ratio devices. In this case I use this method in AppDelegate.cpp:

    static Size designResolutionSize = Size(2560, 1600); // you can choose your designResolutionSize at the top of AppDelegate
    

    In applicationDidFinishLaunching() method:

    bool AppDelegate::applicationDidFinishLaunching() {
    
        …
        auto glview = director->getOpenGLView();
        if(!glview) {
            glview = GLViewImpl::create("My Game");
            director->setOpenGLView(glview);
        }
        glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT);
    
        std::vector <std::string> searchPaths;
        singleton->setScreenRelation(glview->getFrameSize().height / glview->getFrameSize().width);
        if (singleton->getScreenRelation() > 1)
            singleton->setScreenRelation(1 / singleton->getScreenRelation()); // isn’t depend on the screen orientation
    
        if (singleton->getScreenRelation() <= 0.563) // phones with 9:16 aspect ratio
        {
            searchPaths.push_back("PhoneBacks");
        }
        if ((singleton->getScreenRelation() > 0.563)&(singleton->getScreenRelation() <= 0.625)) // phones and tablets with 10:16 and 3:5 aspect ratio
        {
            log("WideTablet, %d", singleton->getScreenRelation());
        }
        if (singleton->getScreenRelation() > 0.625) // tablets with 3:4 aspect ratio
        {
            searchPaths.push_back("TabletBacks");
        }
    
        cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths);
    

    So, you need only background with three different aspect ratios.
    Here is most important part – ResolutionPolicy::FIXED_HEIGHT (or FIXED_WIDTH) option. For landscape screen orientation you need FIXED_HEIGHT policy. It means that you have fixed designResolution.height = 1600 pixels on the all devices, and variable width. For example, on the 720x1280 phone you have background.width= 1600, and background.height = 1600/0.5625 = 2844 pixels. Analogically use FIXED_WIDTH for the portrait screen orientation.
    This method is not perfect. For similar values of aspect ratio backgrounds can be cut into several pixels, or vice versa, can be seen at the edges of black stripes is several pixels wide. Also this method isn’t suitable for moving backgrounds. But for the fixed backgrounds it gives a good picture and requires only three resolutions of backgrounds.