cocos2d-xcocos2d-x-3.0cocos2d-x-3.x

Why is the content


In AppDelegate.cpp , we have the following code in bool AppDelegate::applicationDidFinishLaunching():

glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::SHOW_ALL);
auto frameSize = glview->getFrameSize();

// if the frame's height is larger than the height of medium size.
if (frameSize.height > mediumResolutionSize.height)
{        
    director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
     //Add Code to use large assets
}
// if the frame's height is larger than the height of small size.
else if (frameSize.height > smallResolutionSize.height)
{        
    director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
     //Add Code to use medium assets
}
// if the frame's height is smaller than the height of medium size.
else
{        
    director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
     //Add Code to use small assets
}


 director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));

Why do we take the MIN of the ratios of the height or width ? Why don't we always take either the height or the width ? Won't this be inconsistent?

Since the assets we use are fixed in size (for example, in each block we either pick the large, medium or small assets), isn't the scaleing different for different aspect ratios, that may have the same height or width ? Wouldn't this lead to even worse problems like the physics and images on screen not matching ?


Solution

  • Because if you pick maximal scale it won't fit in height/width (depending of orientation).

    for example designed resolution 960x640

    resolution of background sprite 1024x768

    1024/960 * 1024 ≈ 1092 - max

    640/768 * 1024 ≈ 853 - min

    so if you pick maximum your bg with will be wider then designed and stripe 80px wide will be out of screen.

    otherwise, your bg will fit into designed width with some black stripes aside of it (960-853)/2 px wide.

    Related information