multi resolution support for all android devices

有些话、适合烂在心里 提交于 2019-12-03 16:32:06

In AppDeligate.cpp add the following lines to

bool AppDelegate::applicationDidFinishLaunching() after the glview is set.

CCEGLView *ev = CCEGLView::sharedOpenGLView();
ev->setDesignResolutionSize(480, 320, kResolutionShowAll);

480, 320 being the resolution you designed your app for. If you want portrait use 320, 480 instead. This solution will show black borders if the phone aspect ratio doesn't match the 480/320 aspect ratio.

Sumit Kandoi

In AppDelegate.cpp

This is for landscape Mode

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director

    director = CCDirector::sharedDirector();
    EGLView  = CCEGLView::sharedOpenGLView();

    director->setOpenGLView(EGLView);


    CCSize screenSize = EGLView->getFrameSize();
    CCSize designSize = CCSizeMake(800, 480);
    EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);


    if(screenSize.height > 480 && screenSize.height < 720 )
    {

        CCSize resourceSize = CCSizeMake(960, 540);
        director->setContentScaleFactor(resourceSize.height/screenSize.height);
        CCLog("Resolution Scale OF Karboon=%f",resourceSize.width/screenSize.width);
    }


    else if (screenSize.height >= 720 && screenSize.height < 800)
    {

        CCSize resourceSize = CCSizeMake(1280, 720);
        director->setContentScaleFactor(resourceSize.height/screenSize.height);
        CCLog("Resolution Scale OF NOTE=%f",resourceSize.width/screenSize.width);

    }

    else if(screenSize.height > 800)
    {
        CCSize resourceSize = CCSizeMake(1920, 1080);
        director->setContentScaleFactor(resourceSize.height/screenSize.height);
        CCLog("Resolution Scale OF Nexus=%f",resourceSize.width/screenSize.width);

    }

    else
    {


     director->setContentScaleFactor(1);
    CCLog("Resolution Scale OF S Advance=%f");

    }

return true;

}

Here is some code that might help You make following folders in "Resource" folder

ipadhd ipad iphone

Use this code in Appdelegate.cpp in applicationdidfinishing method

    CCSize screenSize = pEGLView->getFrameSize();
        //set design size for iPad retina
        CCSize designSize = CCSize(2048, 1536);

        CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionExactFit);

        if (screenSize.height > 768) {
            CCFileUtils::sharedFileUtils()->setResourceDirectory("ipadhd");
        } else if (screenSize.height > 320) {
            CCFileUtils::sharedFileUtils()->setResourceDirectory("ipad");
        } else {
            CCFileUtils::sharedFileUtils()->setResourceDirectory("iphone");
        }
pDirector->setContentScaleFactor(screenSize.height/designSize.height)

Hope this helps.Put your images accordingly for iphone in iphone folder,ipad images in ipad folder and hd images in ipadhd folder. pDirector here is CCDirector variable.

i followed this nice tutorial http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Multi_resolution_support

This way it worked for me. i used one high Resolution image

AppDelegate.cpp

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

static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(640,1024);


CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

CCSize frameSize = pDirector->getOpenGLView()->getFrameSize();


pEGLView->setDesignResolutionSize( designResolutionSize.width, designResolutionSize.height,  kResolutionExactFit);


if ((frameSize.height > mediumResource.size.height))
{ 
    pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height);


}
// if the frame's height is larger than the height of small resource size, select medium resource.
else if ((frameSize.height > smallResource.size.height)) 

{ 
    pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);

}
// if the frame's height is smaller than the height of medium resource size, select small resource.
else
{
    pDirector->setContentScaleFactor(smallResource.size.height/designResolutionSize.height);

}    

CCDirector::sharedDirector()->setContentScaleFactor(1.f); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!