Search code examples
xcode4cocos2d-x

Auto get current resource for cocos2dx with app


Universal setting for ipad, ipad retina, iphone, iphone4 and iphone5.


Solution

  • create folders "iphone" "iphone4" "iphone5" "ipad" "ipadhd" this code will intelligent load resource from different folder. just like on iphone5, it will first scan "iphone5" folder, if resource don't exist. It will scan "iphone4" folder for retina resource, if it don't exist. It will scan "iphone" folder. So, your resource must the same filename in different folder.

    in AppDelegate.h add thost code:

    typedef struct tagResource{
    CCSize sizeInPixel;
    CCSize sizeDesign;
    char directory[64];
    }Resource;
    static Resource resIphone={CCSizeMake(320, 480),CCSizeMake(320, 480),"iphone"};
    static Resource resIphone4={CCSizeMake(640, 960),CCSizeMake(320, 480),"iphone4"};
    static Resource resIphone5={CCSizeMake(640, 1136),CCSizeMake(320, 568),"iphone5"};
    static Resource resIpad={CCSizeMake(768, 1024),CCSizeMake(768, 1024),"ipad"};
    static Resource resIpad3={CCSizeMake(1536, 2048),CCSizeMake(768, 1024),"ipadhd"};
    

    in Appdelegate.h at bool AppDelegate::applicationDidFinishLaunching() function:

    /////retina
    Resource resDevice[5]={resIphone5,resIphone4,resIphone,resIpad3,resIpad};
    CCEGLView* pEGLView=CCEGLView::sharedOpenGLView();
    CCSize frameSize=pEGLView->getFrameSize();
    Resource resActual=resIphone;
    std::vector<std::string> resPaths;
    for (int i=0; i<5; i++) {
        if (frameSize.equals(resDevice[i].sizeInPixel)) {
            resActual=resDevice[i];
        }
        float scaleBitPortrait=(float)frameSize.height/resDevice[i].sizeInPixel.height;
        float scaleBitLandscape=(float)frameSize.width/resDevice[i].sizeInPixel.height;
        CCLog("[res path] loop for: %s %f or %f",resDevice[i].directory,scaleBitPortrait,scaleBitLandscape);
        if (scaleBitPortrait==1 || scaleBitPortrait==2 || scaleBitLandscape==1 || scaleBitLandscape==2) {
            resPaths.push_back(resDevice[i].directory);
        }
    }
    
    for (int i=0; i<resPaths.size(); i++) {
        CCLog("[res path] load: %s",resPaths[i].c_str());
    }
    
    CCFileUtils::sharedFileUtils()->setSearchPaths(resPaths);
    pDirector->setContentScaleFactor(resActual.sizeInPixel.width/resActual.sizeDesign.width);
    pEGLView->setDesignResolutionSize(resActual.sizeDesign.width, resActual.sizeDesign.height, kResolutionNoBorder);