Cocos2d-x multiple resolution support

Found a nice article regarding multiple resolution support.
Since Cocos2d-x is cross platform, it’ll run on various Android devices.

After trying 4 patterns; aspect ration 1:1.5 x 2 patterns, 1:1.33 x 2 patterns, I noticed that it’s a pain to support different aspect ratios. I ended up having only 2 patterns to support low and high resolution below.

--- Uitl.h ---
class Util {
public:
    static void SetupContentScaleFactor();
};

--- Uitl.cpp ---
typedef struct tagResource
{
    cocos2d::CCSize size;
    char directory[64];
} Resource;

// 480 x 320 / aspect ratio 1.5 (iPhone/iPod touch) should use this
static const Resource resource15s =  { cocos2d::CCSizeMake(480, 320),   "15s" };

// 960 x 480 / aspect ratio 1.5 (iPhone/iPod touch retina) should use this
// 1024 x 600 / 1.7 (Kindle Fire) should use this
// 1024 x 768 / 1.33 (iPad), 800 x 600 (android)
// 1280 x 800 / 1.6 (Nexus 7) should use this
// 2048 x 1536 / 1.33 (iPad retina) should use this
// 2560 x 1600 / 1.6 (Nexus 10)
static const Resource resource15m =  { cocos2d::CCSizeMake(960, 640),   "15m" };

static const cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(480, 320);

void Util::SetupContentScaleFactor() {
    CCDirector *pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionShowAll);
    
    CCSize frameSize = pEGLView->getFrameSize();
    CCLog("designResolutionSize: (%lf, %lf)", designResolutionSize.width, designResolutionSize.height);
    CCLog("frameSize: (%lf, %lf)", frameSize.width, frameSize.height);
    float scaleFactor = 1.0f;
    Resource resource;
    
    if ((frameSize.height == 640 && frameSize.width == 960) ||
        (frameSize.height == 640 && frameSize.width == 1136)) {
        // iPhone/iPod touch retina
        resource = resource15m;
    }
    else if (frameSize.height > resource15s.size.height)
    {
        resource = resource15m;
    }
    else
    {
        resource = resource15s;
    }
    
    CCFileUtils::sharedFileUtils()->setResourceDirectory(resource.directory);
    scaleFactor = resource.size.height/designResolutionSize.height;
    pDirector->setContentScaleFactor(scaleFactor);
    CCLog("using '%s' resource, scalefactor:%lf", resource.directory, scaleFactor);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.