Android multi screen resolution problem

Hi am new to cocos2d-x and using v3.8 for my new android game.I am having problem with multi screen resolution.All docs on wiki related to multi screen resolution are outdated now.Can you prefer any latest solution for multi screen resolution for android devices or update wiki.

Thanks in advance.

You can read this post. So far it’s working well for me on any screen aspect ratios, I hope it can help you

Thanks wiwing for your help am using v3.8 but i don’t understand how to load resources according to resolution from different directories like “drawable-mdpi,drawable-hdpi etc”. My AppDelegate.cpp looks lke this and it is very different as compared to wiki multi resolution solution.

static cocos2d::Size designResolutionSize = cocos2d::Size(320, 480);
static cocos2d::Size smallResolutionSize = cocos2d::Size(320, 480);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(768, 1024);
static cocos2d::Size largeResolutionSize = cocos2d::Size(1536, 2048);

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}

//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};

GLView::setGLContextAttrs(glContextAttrs);

}

// If you want to use packages manager to install more packages,
// don’t modify or remove this function
static int register_all_packages()
{
return 0; //flag for packages manager
}

bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect(“FlappyBirdClone”, Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else
glview = GLViewImpl::create(“FlappyBirdClone”);
#endif
director->setOpenGLView(glview);
}

// turn on display FPS
director->setDisplayStats(true);

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

// Set the design resolution
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
Size frameSize = glview->getFrameSize();
// if the frame's height is larger than the height of medium size.
if (frameSize.height > mediumResolutionSize.height)
{        
    director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is larger than the height of small size.
else if (frameSize.height > smallResolutionSize.height)
{        
    director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium size.
else
{        
    director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
}

register_all_packages();

// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();

// run
director->runWithScene(scene);

return true;

}

// This function will be called when the app is inactive. When comes a phone call,it’s be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();

}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();

// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();

}

if you read the post referred to you will see some code from wiwing

auto screenSize = glView->getFrameSize();
auto designSize = cocos2d::Size(SCREEN_WIDTH_RETINA, SCREEN_HEIGHT_RETINA);
auto gameSize = cocos2d::Size(GAME_WIDTH_RETINA, GAME_HEIGHT_RETINA);

std::vector<std::string> searchPaths;

if(screenSize.height <= SCREEN_HEIGHT_SD) {
    searchPaths.push_back("SD");
	director->setContentScaleFactor(SCREEN_HEIGHT_SD/designSize.height);
    cocos2d::log("Set SD Design Res");
} else if(screenSize.height <= SCREEN_HEIGHT_HD) {
	searchPaths.push_back("HD");
	director->setContentScaleFactor(SCREEN_HEIGHT_HD/designSize.height);
    cocos2d::log("Set HD Design Res");
} else {
	searchPaths.push_back("RETINA");
	director->setContentScaleFactor(SCREEN_HEIGHT_RETINA/designSize.height);
    cocos2d::log("Set RETINA Design Res");
}

cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths);
glView->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);

See the ‘searchpaths’ stuff? Put different resolution resources in different folders, and the searchpaths tell cocos which one’s to search within for the specific resource.

Thanks Maxxx for your help.I found youtube series - https://www.youtube.com/playlist?list=PLRtjMdoYXLf54dawU8Y0lV7871NRt55Hz by sonar systems.Please let me know is it good approach for multi resolution or not.

Hello

Instead of me telling you it’s a good approach here is a developer who has tried it out.

@pabitrapadhy could you help out @Gurjit

Kind regards
Frahaan Hussain

1 Like

I do’t recall what the Sonar System method was - and don’t have time to watch again - sorry.

Thanks @SonarSystems @maxxx @wiwing for giving your valuable time. SonarSystem’s solution works great for me.

1 Like

Great to hear, if you have any questions feel free to ask plus we have an education platform with thousands of free videos at sonarlearning.co.uk

2 Likes