Cocos2d-x basics

Some articles about Cocos2d-x framework

Working with different screen sizes

Posted at — Sep 27, 2020

In one of the first articles, I’ve recommended to remove from the default AppDelegate.cpp file everything related to work with multiple screen resolutions.

The Cocos2d development started a long long time ago and there were not many different screen sizes available at that time. Later the diversity increased and there a lot of different formats. And now it seems that everybody used more or less same screen sizes again. At some period of time, the resolution was a serious problem: you had to keep a few sets of images for various generations of devices. Again, now it seems that even cheaper devices have decent screens so the problem is not so important (of course you could disagree with this point).

In theory, the development process should look like this:

There was a code like this in default program:

auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin      = Director::getInstance()->getVisibleOrigin();

Here visibleSize is the size of the user’s screen and origin is the position of the scene’s bottom left corner on that screen

Consider the following situation (numbers look unrealistic for our days, but nevermind):

Here:

Cocos2d offers few variants of fitting called (ResolutionPolicy):

Also, you can implement your own policy like is this article.

And another variant, you can skip specifying design resolution and launch with a real screen size. Following code in AppDelegate.cpp does the trick:

Size realScreenSize = glview->getFrameSize();
glview->setDesignResolutionSize(realScreenSize.width, realScreenSize.height,
                                ResolutionPolicy::NO_BORDER);

It doesn’t matter ResolutionPolicy is used here, you get the real screen size anyway.

If you want to use different sets of images for different sizes you’ll have to change the default paths for files downloading. More information about it here, here or in this article.