Resource selection problem in cocos2d-x

I am making different images for iphone and ipad. I have two different images of background but I kept the name of the file bg1.png for both. I put the ipad resolution file in ipad folder and iphone resolution file in iphone4 folder in my resource. My mode is landscape so the height becomes width and width is height
in my AppDelegate.cpp in didfinishlaunch function I put the following code

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    
    std::vector<std::string> searchPath;
    
    CCSize winSize=CCDirector::sharedDirector()->getWinSize();
    
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);
    
    // turn on display FPS
    pDirector->setDisplayStats(true);

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

    
    if(pDirector->getWinSize().height>=768)
    { 
     CCLOG("if width = %f",pDirector->getWinSize().height);
        
        searchPath.push_back("ipad");
       // CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
    }
    else if(pDirector->getWinSize().height<768)
    {
        CCLOG("else width = %f",pDirector->getWinSize().height);
        
        searchPath.push_back("iphone4");

    }
        CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
    
    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}

Every time it is coming in the if block but every alternate time it shows the different images. Suppose if I run for the first time it shows the ipad image. Then i stop the build and re run the code, then it shows iphone image. But I am using the Ipad simulator in both the case. Is anything wrong with the code?

In my code, to avoid issues when the device is in portrait or landscape mode when the app is started is to do something like this:

auto director = Director::getInstance();
auto glview = director->getOpenGLView();

Size frameSize = glview->getFrameSize();

float height = frameSize.height < frameSize.width ? frameSize.height : frameSize.width;
float width = frameSize.height < frameSize.width ? frameSize.width : frameSize.height;

This way, height is always set to the height as if it were in landscape mode (the lesser of the two). Then I use either height or width to determine which graphics to use.

Another thing you should double check is that you added the graphics folders to Xcode as folders and not as groups.

@toojuice
Thanks for the reply. This information is really useful.
Anyways I solved the problem by giving the ipad and iphone4 folder referencing path (blue folder color) and code perfectly worked.
Earlier I was not doing the same.
I want to ask that android devices are in so many dimensions. What is the best resource size dimension for it so that I dont have to put so many different dimension images in assets and the game work perfectly in almost all devices (hdpi,xhdpi,mdpi,ldpi etc).
What should be the resolution that I should take for android?

Yup, that’s what I meant by adding the graphics folders to Xcode as folders and not as groups :smile:

To keep things simple for my games, I use 5 sets of graphics for Android, 4 of which are the same ones I use for iOS. I use the following background sizes:

Phones:
569 x 320 - based on non-retina iPhone, but extended to be 16x9
1138 x 640 - based on retina iPhone, but extended to be 16x9
2276 x 1280 - 2x my retina iPhone resolution

Tablets:
1366 x 768 - based on non-retine iPad, but extended to 16x9
2732 x 1536 - based on retina iPad, but extended to 16x9

@rajan, I do basically what you do, but my math is a bit different, although I only write my games for Landscape.