[Solved] Problem with graphics.getDimensions on Android

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

[Solved] Problem with graphics.getDimensions on Android

Post by Bigfoot71 »

Hello everyone ! :D

I'm facing a small problem with the love.graphics.getDimensions function on Android, I wrote myself a small script to resize the game according to the resolution which roughly looks like this:

Code: Select all

local lg = love.graphics

Display = {

    size  = {
        w = WIN_W,  -- Where WIN_* is the original game size
        h = WIN_H
    },

    scale = {
        x = 1,
        y = 1
    }

}

function Display:render(drawable, ...)
    lg.push(); lg.scale(self.scale.x, self.scale.y)
    drawable(...); lg.pop()
end

function Display:resize()
    local size, scale = self.size, self.scale
    size.w, size.h = lg.getDimensions()
    scale.x = size.w / WIN_W
    scale.y = size.h / WIN_H
end
On PC it works very well, the problem occurs on Android where the navigation bar (the Home, Return buttons, etc.) does not always take the same time to hide and therefore I sometimes get the screen size without the bar and other times with the bar, which means that once in two the buttons, joystick, etc. find themselves out of step with the rest of the display (I use GOOI for the GUI elements).

I tried several hacks but without success, if anyone has a solution to this problem that would be great!
Last edited by Bigfoot71 on Sun Dec 11, 2022 2:54 pm, edited 1 time in total.
My avatar code for the curious :D V1, V2, V3.
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

Re: Problem with graphics.getDimensions on Android

Post by darkfrei »

Call function setMode twice:
https://love2d.org/wiki/love.window.setMode

Code: Select all

love.window.setMode( width, height, flags )
love.window.setMode( width, height, flags )
Don't forget the safe area:
https://love2d.org/wiki/love.window.getSafeArea
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Problem with graphics.getDimensions on Android

Post by Bigfoot71 »

darkfrei wrote: Sun Dec 11, 2022 2:40 pm Call function setMode twice:
https://love2d.org/wiki/love.window.setMode

Code: Select all

love.window.setMode( width, height, flags )
love.window.setMode( width, height, flags )
Don't forget the safe area:
https://love2d.org/wiki/love.window.getSafeArea
Oh yeeeah, thank you very much for this quick answer, it works wonderfully!
My avatar code for the curious :D V1, V2, V3.
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

Re: Problem with graphics.getDimensions on Android

Post by darkfrei »

Bigfoot71 wrote: Sun Dec 11, 2022 2:54 pm
darkfrei wrote: Sun Dec 11, 2022 2:40 pm Call function setMode twice:
https://love2d.org/wiki/love.window.setMode

Code: Select all

love.window.setMode( width, height, flags )
love.window.setMode( width, height, flags )
Don't forget the safe area:
https://love2d.org/wiki/love.window.getSafeArea
Oh yeeeah, thank you very much for this quick answer, it works wonderfully!
I've added this solution to the wiki, but it was deleted.
ms-mad.png
ms-mad.png (473 Bytes) Viewed 925 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests