5
\$\begingroup\$

Is there a way to use Density Independent pixels(dp) in cocos2d-x?

I am developing an Android game using Cocos2d-x 3.0.

The Android reference http://developer.android.com/guide/practices/screens_support.html tells me not to use pixels in code but use dp when supporting multi-resolution support. It also tells me to provide different images for different screen size groups and different density groups.

However, cocos2d-x tutorials use pixels for multi-resolution support. If I were to use pixels, I think it means I need to have images for all these different resolutions:

240x320, 240x400, 240x432 320x480 480x640, 480x800, 480x854 600x1024 640x960 1024x600, 1024x768 1280x768, 1280x800 1536x1152 1920x1152, 1920x1200 2048x1536 2560x1536, 2560x1600

Even if I don't support every screen resolutions, I think this requires more work than following the Android reference's best practice.

Am I missing something on how supporting multi-resolution works or do I indeed do need to do much more work using cocos2d-x?

\$\endgroup\$
7
\$\begingroup\$

There is no option about dp in cocos2dx and in fact we don't need it.

But cocos2D-x 2.x and 3.x have very easy solution for multi-resolution problem ( either iDevices or Android devices) As you saw in Multi-resolution support document.

In fact you just need to set your DesignResolution and then just imagine your target device will has this resolution.

If target device really has this resolution ( or any other with same ratio) cocos2d will handle to fix screen and your game looks same in all devices.

And when ratio of target device is different, you have many option ( policy) to manage that.

For example if you use Exact fit policy, cocos2d will force your game ( and design) to fit target device screen (without that black boarder).

Exact fit

The entire application is visible in the specified area without trying to preserve the original aspect ratio. Distortion can occur, and the application may appear stretched or compressed.

For more detail just take a look at the official wiki.

Beside all above word, I found This Link (from V-Play engine) and Its Safe Zone definition really interesting and I highly recommend you to use recommended-resolution-value of this page for your work as I did.

Although this link is from another engine but the description helps you understand everything better. Here is a map between this page terms to cocos2d-x terms:

V-Play::letterbox => Cocos2dx::Show All

V-Play::ZoomToBiggerSide => Cocos2dx::NoBorder


In addition you did ask about required image size in cocos2dx development:

As you know the different size of image is not about game-look in different resolutions and you can publish your game with one size for each asset and your game/app looks good in all resolution with above description for resolution Policies.

But we need multiple image-size for each asset, to optimize memory usage. in this case ( as cocso2dx solution) we check device size and mark appropriate set of image ( each image set is in one folder with same-structure/different-size like HDR/HD/SD) as default folder of resource:

    CCSize frameSize = pEGLView->getFrameSize();

    if (frameSize.height > mediumResource.size.height)
    { 
        searchPath.push_back(largeResource.directory); //mark HDR default
        pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height); //handle scaling because of different between our programming-design-resolution and artist-design-canvase-resolution
    }
    else ...
\$\endgroup\$
  • \$\begingroup\$ Sorry. I tried to upvote but it says I need 15 reputations to do that and I only have 8 reputations.. \$\endgroup\$ – whiteSkar Dec 29 '14 at 20:04
  • \$\begingroup\$ Is this answer still valid? the linked docs says it is outdated. \$\endgroup\$ – Jonny Apr 17 '15 at 9:46
  • 1
    \$\begingroup\$ @Jonny Yes. cocos2d-x team outdated them because they're working on new wiki but until then there is no alternative except current links. \$\endgroup\$ – Emadpres Apr 17 '15 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.