Since at least Cocos2d-x version 2.0.4, you can create separate subdirectories of your Resources folder and select which one to use at runtime. If a resource is not found there, Cocos2d-x will automatically fall back to the Resource folder instead of your set subdirectory. This is all described here in the Cocos2d-x wiki.
As an example, you could do something like this
const CCSize winSize = CCDirector::sharedDirector()->getWinSize();
// Landscape
if( winSize.height < winSize.width) {
if( winSize.width >= 1536 ) {
// iPad Retina here.
CCFileUtils::sharedFileUtils()->setResourceDirectory( "iPadRetina" );
} else {
// Some lower resolution tablet stuff.
CCFileUtils::sharedFileUtils()->setResourceDirectory( "lowResTablet" );
}
}
// Portrait
else {
if ( winSize.width >= 720 ) {
// Phones like Galaxy Nexus.
CCFileUtils::sharedFileUtils()->setResourceDirectory( "720" );
} else if ( winSize.width >= 640 ) {
// iPhone retina
CCFileUtils::sharedFileUtils()->setResourceDirectory( "iPhoneRetina" );
} else {
// Low res phone.
CCFileUtils::sharedFileUtils()->setResourceDirectory( "lowResPhone" );
}
}
You call the above code from your AppDelegate::applicationDidFinishLaunching()
function to tell Cocos2d-x to use the correct subdirectory for resources. Let's say you have set the directory "iPhoneRetina" to be your resource directory. Then when you do
CCSprite *sprite = CCSprite::create( "cookie.png" );
Cocos2d-x will first look for the file "iPhoneRetina/cookie.png". If it doesn't find it there, it will look for "cookie.png" in the root of your Resources folder before giving up.
Good luck with your porting! :)
-- Edit about DPI --
There is apparently a function CCDevice::getDPI()
in newer versions of Cocos2d-x (user in comments suggested), I found it in version 2.1.3. However, unless you absolutely need everything to appear at the same physical size, I suggest you let the real size of things float a bit and focus on matching the resolution 1:1. If you start to scale things, they will look blurry. Also, I don't think that the same resolution at widely different screen sizes is that common. I know there are several physical sizes of screens with full HD for example, but I think all portrait ones are pretty close to each other and all tablet ones too.
I don't know, you could of course be making an app where the physical screen size of elements in the app is very important. A measurement app where you can put things on the screen and measure them in cm or something comes to mind. If this is the case then of course the screen size of things is top priority and you might want to look into that CCDevice::getDPI()
function.