Prevent window rotation 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
Endaris
Prole
Posts: 12
Joined: Sun Oct 09, 2022 11:55 pm

Prevent window rotation on Android

Post by Endaris »

Hi, not sure if someone can help me with this.
When running the game with love 11.3 on android, regardless of how I rotate my phone, the game stays in landscape mode, only flipping by 180° if anything - which is what I want.
When running the game with love 11.4, rotating will change the orientation and also change it to portrait mode.
Both conveniently ignore the rotation lock setting of android.
We updated the game to 11.4 some time ago for the LuaJIT version update among other things I would like to use 11.4 with the old behaviour but right now I am clueless how I can enforce landscape mode.

I read a bit around in the forums and tried to intercept the display rotation via love.displayrotated or love.resize and flip the width/height values via love.window.getMode and love.window.setMode if height > width as that is what I had found in an older thread as a method to enforce portrait mode on android. Didn't work as intended though.

I then found https://github.com/love2d/love-android/issues/196 and figured based on the statement
The t.window.resizable = false is needed there to prevent the game change orientation so that's fine.
that I need to make an OS specific differentation for the resizable flag. And great, it did work for my device (running LineageOS with Android 9) with Love 11.4, the game stays in landscape mode and only changes to the flipped version when I turn it by 180°.

So I shared that version with the person that originally brought that behaviour to my attention and...it doesn't work for them. They use a Galaxy S10 with Android 12.
And now I'm completely lost where I should continue. What other options do I have to control orientation other than preventing resizing?

Game source: https://github.com/panel-attack/panel-attack
PR that works for me but not for others: https://github.com/panel-attack/panel-a ... /686/files
Manually packaged .love from the PR code: https://endaris.s-ul.eu/M9lN4n2u.love
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Prevent window rotation on Android

Post by pgimeno »

I think that it was possible to force an orientation using love.window.setMode with width > height or width < height depending on the desired orientation. I'm not sure if it still applies.
User avatar
Endaris
Prole
Posts: 12
Joined: Sun Oct 09, 2022 11:55 pm

Re: Prevent window rotation on Android

Post by Endaris »

Hi, thanks for your reply!
That is what I tried at first. It might be that I have to do both resizable = false and that thing though.

I added the following code to the PR in the previous post:

Code: Select all

function love.displayrotated(index, orientation)
  local os = love.system.getOS()
  if os == "Android" or os == "iOS" then
    if orientation == "portrait" or "portraitflipped" then
      local width, height, flags = love.window.getMode()
      if index == flags.display then
        if height > width then
          -- just to make sure
          flags.resizable = false
          love.window.setMode(height, width, flags)
        end
      end
    end
  end
end
At least one person already reported to me that it still rotates even with that change.
Is there anywhere else where I should try to use setMode instead or where I better intercept a display rotation?
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Prevent window rotation on Android

Post by pgimeno »

I think you should place it on initialization, not on displayrotated.
User avatar
Endaris
Prole
Posts: 12
Joined: Sun Oct 09, 2022 11:55 pm

Re: Prevent window rotation on Android

Post by Endaris »

The conclusion:
I obviously can't do it in love.conf due to love.system not being loaded at that point in time.
The reason why the previous solution didn't work for the users was due to the auto updater apparently doing weird stuff and overriding the present version with the one on the server which obviously led to that not working.
I now inserted the code above more or less identically in love.load and it works fine. Screen rotates twice on bootup which looks weird but I suppose it achieves the result still.
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Prevent window rotation on Android

Post by pgimeno »

Endaris wrote: Fri Oct 21, 2022 7:29 am I obviously can't do it in love.conf due to love.system not being loaded at that point in time.
[...]
I now inserted the code above more or less identically in love.load and it works fine. Screen rotates twice on bootup which looks weird but I suppose it achieves the result still.
You can set t.window = nil in love.conf to prevent the screen from being initialized at all, and then call setMode in the top level of main.lua or in love.load to initialize the screen.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Prevent window rotation on Android

Post by DaedalusYoung »

Endaris wrote: Mon Oct 10, 2022 10:13 pm

Code: Select all

    if orientation == "portrait" or "portraitflipped" then
I realise this is an older post, but I believe that if-statement is wrong. I think it should be:

Code: Select all

if orientation == "portrait" or orientation == "portraitflipped"
Otherwise, it will always return true, and then obviously it will always execute the nested code.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests