Design Resolution in cocos2d-x 3.x for iPhone & iPad

This is how to set design resolution of cocos2d-x project for the iPhone & iPad. (Landscape orientation)

Apply following changes into AppDelegate.cpp:

  1. Set HDR size to 2208×1536 and HD size to 1334×928.
    1. 2208 came from the “Rendered Pixels” width of 5.5″ devices.
    2. 1536 came from the “Rendered Pixels” height of retina iPads. (= 768 x 2)
    3. 1334 came from the “Rendered Pixels” width of 4.7″ devices.
    4. 928 came from following formula: 1536 x 1334 / 2208.
  2. Set “Design Resolution Size” to HDR size temporarily.
  3. Get “Device Frame Size” of the device.
  4. Determine the “Resolution Policy” of cocos2d-x.
    1. ResolutionPolicy::FIXED_WIDTH  or ResolutionPolicy::FIXED_HEIGHT
    2. By checking width/height ratio of the “Device Frame Size” and “Design Resolution Size”.
  5. Set “Design Resolution Size” with the “Resolution Policy” from step 4.
  6. Get adjusted “Design Resolution Size”.
  7. Set “Content Scale Factor” of cocos2d::Director .
  8. Set “Search Paths” of cocos2d::FileUtils  accordingly.
#include "AppDelegate.h"
...

USING_NS_CC;

static Size hdSize= Size(1334, 928);
static Size hdrSize= Size(2208, 1536);
static float resourceRatio= hdrSize.width/hdrSize.height;

bool AppDelegate::applicationDidFinishLaunching() {
    ...
    auto frameSize= glview->getFrameSize();
    bool isFixedWidth= frameSize.width/frameSize.height > resourceRatio;
    glview->setDesignResolutionSize(hdrSize.width, hdrSize.height,
                                    isFixedWidth ?
                                    ResolutionPolicy::FIXED_WIDTH :
                                    ResolutionPolicy::FIXED_HEIGHT);
    auto designResolutionSize= glview->getDesignResolutionSize();
    std::vector<std::string> resDirOrders;
    if ((isFixedWidth && frameSize.width > hdSize.width) ||
        (!isFixedWidth && frameSize.height > hdSize.height)) {
        director->setContentScaleFactor(isFixedWidth ?
                                        hdrSize.width/designResolutionSize.width :
                                        hdrSize.height/designResolutionSize.height);
        resDirOrders.push_back("res/hdr");
        resDirOrders.push_back("res/hd");
    } else {
        director->setContentScaleFactor(isFixedWidth ?
                                        hdSize.width/designResolutionSize.width :
                                        hdSize.height/designResolutionSize.height);
        resDirOrders.push_back("res/hd");
        resDirOrders.push_back("res/hdr");
    }
    FileUtils::getInstance()->setSearchPaths(resDirOrders);
    ...
}