2
votes

As per my knowledge, to make a application in cocos2dx, I need three sets of resources ipad, ipadhd and iphone

From one example of an application, I saw that in order to support devices of all resolutions, the asset sizes (background image) that my friend had used were:

ipadhd: 2272 X 1536

ipad: 1136 X 768

iphone: 568 X 384

These dimensions work fine for a landscape mode game. Here are my questions: 1. Why do we need these sizes in particular?? 2. I am trying to make a portrait mode game for which I took the same asset size (but since I am working in portrait mode, I took image sizes as 1536 X 2272, 768 X 1136 and 384 X 568). But, for some reason, the BG image is enlarged when it shows up on simulator/device. I am attaching screen shots here.

Original Image: enter image description here

Image showing up in simulator:

enter image description here

For reference, here is the code to set device resolution size and content scale factor:

#define TARGET_DESIGN_RESOLUTION_SIZE  DESIGN_RESOLUTION_480X320

typedef struct tagResource
{
    cocos2d::CCSize size;
    char directory[100];
}Resource;

static Resource smallResource = { cocos2d::CCSizeMake(320, 480), "iphone" };
static Resource mediumResource = { cocos2d::CCSizeMake(768,1024), "iPad" };
static Resource largeResource = { cocos2d::CCSizeMake(1536,2048), "ipadhd" };

#if (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_480X320)
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(320, 480);
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_1024X768)
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(768,1024);
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_2048X1536)
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(1536,2048);
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_NONE)
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(320, 480);
#else
#error unknown target design resolution!
#endif


bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

pDirector->setOpenGLView(pEGLView);

// Set the design resolution
pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height,kResolutionNoBorder);
CCSize frameSize = pEGLView->getFrameSize();

std::vector<std::string> resDirOrders;

// if the frame's height is larger than the height of medium resource size, select large resource.
if (frameSize.width > mediumResource.size.width)
{
    resDirOrders.push_back(largeResource.directory);
    pDirector->setContentScaleFactor(largeResource.size.width/designResolutionSize.width);
}
// if the frame's height is larger than the height of small resource size, select medium resource.
else if (frameSize.width > smallResource.size.width)
{
    resDirOrders.push_back(mediumResource.directory);
    pDirector->setContentScaleFactor(mediumResource.size.width/designResolutionSize.width);

}
// if the frame's height is smaller than the height of medium resource size, select small resource.
else
{
    resDirOrders.push_back(smallResource.directory);
    pDirector->setContentScaleFactor(smallResource.size.width/designResolutionSize.width);
}
CCFileUtils::sharedFileUtils()->setSearchPaths(resDirOrders);

// turn on display FPS
pDirector->setDisplayStats(false);

// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);

// create a scene. it's an autorelease object
CCScene *pScene = GameLayer::scene();

// run
pDirector->runWithScene(pScene);

return true;
}
1

1 Answers

3
votes

Actually, the reason for this is because your resource (assets) size (resolution) does not match with the design resolution you are setting. Your assets are of different aspect ratio and design resolution is of different aspect ratio.

I never really use contentScaleFactor in my games. If all assets are of same scale, just set design resolution to the same resolution of the assets and it will fit. Cocos2d-x automatically rescales to device screen if you are using the kResolutionExactFit policy.

I think you should read the Detailed explanation of cocos2d-x multi-resolution support. It is a well written article.