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:
- Set HDR size to 2208×1536 and HD size to 1334×928.
- 2208 came from the “Rendered Pixels” width of 5.5″ devices.
- 1536 came from the “Rendered Pixels” height of retina iPads. (= 768 x 2)
- 1334 came from the “Rendered Pixels” width of 4.7″ devices.
- 928 came from following formula: 1536 x 1334 / 2208.
- Set “Design Resolution Size” to HDR size temporarily.
- Get “Device Frame Size” of the device.
- Determine the “Resolution Policy” of cocos2d-x.
- ResolutionPolicy::FIXED_WIDTH or ResolutionPolicy::FIXED_HEIGHT
- By checking width/height ratio of the “Device Frame Size” and “Design Resolution Size”.
- Set “Design Resolution Size” with the “Resolution Policy” from step 4.
- Get adjusted “Design Resolution Size”.
- Set “Content Scale Factor” of cocos2d::Director .
- 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);
...
}