I use a modified version of the Ultimate Config, and with that I get display.contentWidth/contentHeight to report “actual” width/height, not the letterboxed width/height.
My standard config.lua is like this:
-- config.lua local aspectRatio = display.pixelHeight / display.pixelWidth; SAFE\_WIDTH = 684; SAFE\_HEIGHT = 1026; local maxAspect = SAFE\_HEIGHT / SAFE\_WIDTH; application = { content = { width = aspectRatio \> maxAspect and SAFE\_WIDTH or math.ceil(SAFE\_HEIGHT / aspectRatio), height = aspectRatio \< maxAspect and SAFE\_HEIGHT or math.ceil(SAFE\_WIDTH \* aspectRatio), scale = "letterbox", fps = 60, antialias = false, xAlign = "center", yAlign = "center", imageSuffix = { ["@0.5x"] = 0, [""] = 0.7, ["@2x"] = 1.4 } } }
(Off topic: I know! 684 x 1026 seems weird, but I want the iPad resolution as standard but with the iPhone’s 3:2 aspect ratio, and this is the closest I can get with whole integers)
I also have another module which defines the screen boundaries for me.
(I’ve stripped away a bunch of irrelevant stuff for this topic).
-- appglobals.lua local M = {}; require("config"); -- this reads in config.lua so that the device width/height can be accessed . . . a bunch of other stuff here... . . local deviceScreen = {}; M.configureScreen = function() local dWidth = display.contentWidth \> application.content.width and SAFE\_HEIGHT or SAFE\_WIDTH; local dHeight = display.contentWidth \> application.content.width and SAFE\_WIDTH or SAFE\_HEIGHT; deviceScreen.left = 0; deviceScreen.top = 0; deviceScreen.right = display.contentWidth; deviceScreen.bottom = display.contentHeight; deviceScreen.width = deviceScreen.right - deviceScreen.left; deviceScreen.height = deviceScreen.bottom - deviceScreen.top; deviceScreen.safeLeft = math.ceil((deviceScreen.width - dWidth) / 2); deviceScreen.safeRight = deviceScreen.right - deviceScreen.safeLeft; deviceScreen.safeTop = math.ceil((deviceScreen.height - dHeight) / 2); deviceScreen.safeBottom = deviceScreen.bottom - deviceScreen.safeTop; end M.getScreen = function() return deviceScreen; end -- init module M.configureScreen(); return M;
In my other modules, I then call
local app = require("appglobals"); local screen = app.getScreen();
If I need to position stuff within the 3:2 aspect ratio (safe-zone) I use the screen.safe* variants, otherwise I just use left, right etc. to position relative to the device’s actual screen boundaries.
If the app supports both landscape and portrait I call app.configureScreen() on orientation change to calculate the new dimensions.
So far this approach has been successful.