Search code examples
haxehaxelibkha

Scaling a kha-app for retina on iPad


I have a kha app that runs perfecly on an iPad2 (1024/768px).

When I run the same project on a later iPad Mini with 2048/1516. My coordinates are all half the size, which kinda makes sense.

So when I double all the sizes of my objects and GFX it will work on the iPad mini, but will be too big for iPad2.

I looked into a backbuffer and a renderTarget as explained here: https://www.youtube.com/watch?v=OV1PTo5XSCA

There is also the windowSize option in khafile, which seems to do nothing.

Surface x and y coodinates always seem to come in in real screen coodrdinates of the device.

What is the best way to write a resolution independent app?

Perfect would be a way that is either retina or non-retina, depending on the device, where the code stays the same.


Solution

  • According to https://github.com/Kode/Kha/wiki/Screen-Size-and-Scaling there's automated scaling for some targets. If you need other targets you have to manually scale everything to fit the screen.

    The page mentions using this class for the task: https://github.com/Kode/Kha/blob/master/Sources/kha/Scaler.hx

    Also you could take a look at how Wyngine does it: https://github.com/laxa88/wyngine/search?utf8=%E2%9C%93&q=scale & https://github.com/laxa88/wyngine/blob/master/Wyngine.hx

    You replied (to my comment) that scaling wasn't enough. So far it was enough for all of my games with the right display settings, but if you really need retina sized graphics you always have the option of using multiple graphics sets. Eg:

    • a set for retina resultion (eg iPad 3)
    • a default resolution (eg iPad 2) set at half retina size
    • a low res set for cheap android devices?

    At startup of your app you check the screen size. You use that to choose the internal game size and the graphics set that fits the actual screen resolution the best. The internal game size as well as all X/Y positions for the selected graphics set can be calculated by applying the graphics sets scale factor to the raw base values.

    Finally you use Scale.scale() to scale your game from the internal game size to fit devices like the iPad pro 12" and the wide variety of Android devices.

    That approach is common with a lot of game engines, google should find you links like https://v-play.net/doc/vplay-different-screen-sizes/ that also explain screen ratios and how those can be handled.