使用app自动获取cocos2dx的当前资源

时间:2013-08-01 03:34:34

标签: xcode4 cocos2d-x

ipad,ipad retina,iphone,iphone4和iphone5的通用设置。

1 个答案:

答案 0 :(得分:0)

创建文件夹“iphone”“iphone4”“iphone5”“ipad”“ipadhd” 此代码将智能加载来自不同文件夹的资源。就像在iphone5上一样,如果资源不存在,它将首先扫描“iphone5”文件夹。如果它不存在,它将扫描“iphone4”文件夹中的视网膜资源。它将扫描“iphone”文件夹。 因此,您的资源必须与不同文件夹中的文件名相同。

在AppDelegate.h中添加代码:

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"};

在Appdelegate.h中的bool AppDelegate :: applicationDidFinishLaunching()函数:

/////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);