Hi folks. I was answering a recent post where I made an assertion that “calculated config.lua resolutions are of no value, especially for new folks.”
Now, I still believe this, but I’d love to hear what others think.
(Also, I hate to put down or poo poo other folks’ work or even seem like I am. So while I don’t use these myself, I really want to learn the value of them so I can correct my attitude. )
What I mean by FIXED and CALCULATED
To be clear, here is what I mean by,
Fixed
application = { content = { width = 640, height = 960, scale = "letterbox", fps = 60, }, }
Calculated
(Just in case anyone decides to use this, I’ve modified it as per @davebollinger’s reply below.)
local aspectRatio = display.pixelHeight / display.pixelWidth application = { content = { fps = 60, -- WRONG width = aspectRatio \> 1.5 and 320 or math.ceil( 480 / aspectRatio ), -- WRONG height = aspectRatio \< 1.5 and 480 or math.ceil( 320 \* aspectRatio ), width = aspectRatio \> 1.5 and 320 or math.floor( 480 / aspectRatio ), -- BETTER height = aspectRatio \< 1.5 and 480 or math.floor( 320 \* aspectRatio ), -- BETTER scale = "letterbox", } }
Why I Like Fixed
A long time ago, I stumbled upon an blog post that talked about the ‘magic recipe’ for config.lua files and projects.
It was essentially a fixed resolution config.lua file and a 570 x 380 background image.
I use a modern version of this that works for all known devices, including the terrible/painful to deal with iPhone X.
The config.lua is the one I show above (fixed example) and the image is 720 x 1386. I use this recipe for my askEd starter kit here: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/askEdStarter.zip
The image (as shown) has a wonderful guideline. The checkerboard area is guaranteed to be visible on all screens. The black area bounded by red rectangles is bleed space. In short, if you want a full screen image as a background, you can use this template, then put the important parts of the image in the ‘guranteed’ space and the filler in the bleed.
Also, the image above, has a wonderful attribute that is easy to miss. It is very high resolution, so it looks OK if you run it on higher-resolution screens. However, it will always work and can be loaded on low-end devices that only have 2k x 2k texture buffers. If you really need the @2x versions of these images, they will maintain their low-end backward compatibility while being very suitable for all modern devices.
Auto-Scaling
Because this fixed recipe is using letterbox scaling I am guaranteed that my images placed with newImageRect() (which I use almost exclusively) will automatically be scaled while maintaining the aspect ratio of my content.
What is not guaranteed is that things like. display.contentWidth, display.contentHeight, etc are the same. i.e. 0 may be the left side of the screen on some devices and not on others. So, how do I deal with that?
Relative Placement
In my designs, I typically use fixed sizes and relative placements for my interface elements (buttons, score labels, etc.).
By relative, I mean “relative to the current device’s” left, top, right, bottom, full with, full height, centerX, and centerY.
If you know me, then you know I never code without SSK2. SSK2 automaticall calculates these values for me and exposes them as globals: https://roaminggamer.github.io/RGDocs/pages/SSK2/globals/#global-variables
Planning Is Easier
At the end of the day, for me, the main reason fixed resolution config.lua files are best is because I know, going in, exactly what my minimum and safe content area sizes will be and I can plan my art accordingly.
For me, a non-artist, this is a major plus.
Also, it makes doing in-the head continuations while planning a game layout a snap.
Calculated Config.lua Files I Do Like
There is one calculated, config.lua file I do like. Sergey Lerg’s Smart Pixel / Pixel Perfect config.lua
https://github.com/Lerg/smartpixel-config-lua
It fixes the image rounding issue/artifact problem seen when scaling goes wrong and a pixel in the original design ends up being some value plus a sub-pixel. Ex: 1 becomes 0.45, 2 becomes 0.9, or the other way, 1 becomes 1.75, 2 becomes 3.5, etc.
Ideally a 1 pixel object in the original design would be an integer multiple of the original size. This config.lua recipe ensures that.
-- Author: Lerg - spiralcodestudio.com -- GitHub: https://github.com/Lerg/smartpixel-config-lua -- SmartPixel config.lua -- Uses pixel perfect content scaling when possible local w, h = display.pixelWidth, display.pixelHeight local modes = {1, 1.5, 2, 3, 4, 6, 8} -- Scaling factors to try local contentW, contentH = 320, 480 -- Minimal size your content can fit in -- Try each mode and find the best one local \_w, \_h, \_m = w, h, 1 for i = 1, #modes do local m = modes[i] local lw, lh = w / m, h / m if lw \< contentW or lh \< contentH then break else \_w, \_h, \_m = lw, lh, m end end -- If scaling is not pixel perfect (between 1 and 2) - use letterbox if \_m \< 2 then local scale = math.max(contentW / w, contentH / h) \_w, \_h = w \* scale, h \* scale end application = { content = { width = \_w, height = \_h, scale = 'letterbox', fps = 60, imageSuffix = { ['@2x'] = 1.1, ['@4x'] = 2.1, } } }