Page 1 of 1
Toggling Fullscreen with Escape
Posted: Sat Oct 22, 2016 2:41 am
by bigdevlover
I can't seem to figure out how I would toggle fullscreen with escape. This is the code I currently have in my love.udate function:
Code: Select all
function love.update (dt)
if love.keyboard.isDown("d") then
x = x +1
y = y + 1
end
if love.keyboard.isDown("a") then
x = x - 1
y = y - 1
end
if love.keyboard.isDown("escape") then
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
end
if fullscreen == true then
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
end
Currently, when I press escape the window maximizes and then minimizes very quickly. Am I doing something wrong or is this the wrong way to go about it?
Re: Toggling Fullscreen with Escape
Posted: Sat Oct 22, 2016 8:24 am
by drunken_munki
I assume love.keyboard.isDown() is firing every update, even in the miliseconds when you release the key; which may account for your side-effect.
Try to use love.keypressed(key, scancode, repeating) which will fire once when the key is pressed:
https://love2d.org/wiki/love.keypressed
Something like:
Code: Select all
function love.keypressed(key, scancode, repeating)
if key == "escape" then
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
end
if fullscreen == true then
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
end
end
Re: Toggling Fullscreen with Escape
Posted: Sat Oct 22, 2016 8:28 am
by Fuzzlix
bigdevlover wrote:I can't seem to figure out how I would toggle fullscreen with escape. This is the code I currently have in my love.udate function:
Code: Select all
function love.update (dt)
if love.keyboard.isDown("d") then
x = x +1
y = y + 1
end
if love.keyboard.isDown("a") then
x = x - 1
y = y - 1
end
if love.keyboard.isDown("escape") then
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
end
if fullscreen == true then
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
end
Currently, when I press escape the window maximizes and then minimizes very quickly. Am I doing something wrong or is this the wrong way to go about it?
Your escape button is still down when you enter love.update(dt) the second time. It seems to be the wrong idea to use love.keyboard.isDown("escape") for this scenario. I would think about using love.keypressed() instead.
Re: Toggling Fullscreen with Escape
Posted: Sat Oct 22, 2016 8:59 am
by ivan
Code: Select all
function toggleFullscreen()
local fs = love.window.getFullscreen()
fs = not fs
local rs = love.window.setFullscreen(fs, 'desktop')
return rs and fs or nil
end
Returns nil if the toggle fails or a Boolean if the mode was toggled successfully.
Re: Toggling Fullscreen with Escape
Posted: Sat Oct 22, 2016 11:28 am
by zorg
People, people!
Code: Select all
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
end
if fullscreen == true then
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
The above snippet amounts to this:
Code: Select all
if not a then a = true end
if a then a = false end -- a will always be true here, because of the above, so it'll be set back to false -in the same frame-!
Which is short for toggling a twice, back to what it was.
Both the op's code and drunken_munki's code had this... so yeah, use an else instead:
Code: Select all
function love.keypressed(key, scancode, repeating)
if key == "escape" then
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
else
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
end
end
(Or use ivan's solution
)
Re: Toggling Fullscreen with Escape
Posted: Sun Jun 19, 2022 12:34 pm
by UsualBasementKid
My setup for toggling fullscreen:
Code: Select all
-- In love.load(),
love.window.setFullscreen(false) -- <- change this to true if you want your game to be fullscreen when you run
fullscreen = false -- <- change this to true if you want your game to be fullscreen when you run
-- In love.update(),
-- This is the important stuff, I kinda don't wanna explain but it works sooooo,
-- Fullscreen / Not Fullscreen
if fullscreen == true then
love.window.setFullscreen(true)
elseif fullscreen == false then
love.window.setFullscreen(false)
end
function love.keypressed(k)
if k == "escape" and fullscreen == false then
fullscreen = true
elseif k == "escape" and fullscreen == true then
fullscreen = false
end
end
Re: Toggling Fullscreen with Escape
Posted: Sun Jun 19, 2022 1:34 pm
by GVovkiv
UsualBasementKid wrote: ↑Sun Jun 19, 2022 12:34 pm
My setup for toggling fullscreen:
Code: Select all
-- In love.load(),
love.window.setFullscreen(false) -- <- change this to true if you want your game to be fullscreen when you run
fullscreen = false -- <- change this to true if you want your game to be fullscreen when you run
-- In love.update(),
-- This is the important stuff, I kinda don't wanna explain but it works sooooo,
-- Fullscreen / Not Fullscreen
if fullscreen == true then
love.window.setFullscreen(true)
elseif fullscreen == false then
love.window.setFullscreen(false)
end
function love.keypressed(k)
if k == "escape" and fullscreen == false then
fullscreen = true
elseif k == "escape" and fullscreen == true then
fullscreen = false
end
end
Huh, why you check for key escape 2 times?
Why you elseif when you can just else?
And why not wrap it in function, so you can call this outside of love keypress?
And, most important, why necropost on like 6 year post?
Re: Toggling Fullscreen with Escape
Posted: Mon Jun 20, 2022 6:53 am
by nephele
In general you should only use keydown in love.update for things that should happen continuosly.
For one off things (as in you press and release the key for the action) love.keypressed or love.keyreleased is the right tool :)
This is how I would solve this problem:
Code: Select all
local fullscreen = love.window.getFullscreen()
love.keyreleased = function(key, _)
if key == "escape" then
fullscreen = not fullscreen
love.window.setFullscreen(fullscreen, "desktop")
end
end
Re: Toggling Fullscreen with Escape
Posted: Mon Jun 20, 2022 12:57 pm
by darkfrei
nephele wrote: ↑Mon Jun 20, 2022 6:53 am
In general you should only use keydown in love.update for things that should happen continuosly.
For one off things (as in you press and release the key for the action) love.keypressed or love.keyreleased is the right tool
This is how I would solve this problem:
Code: Select all
Fullscreen = love.window.getFullscreen() -- global
function love.keypressed (key, scancode) -- faster than key released
if key == "f11"
and love.keyboard.isDown( "rctrl", "lctrl" )
and not love.keyboard.isDown( "lalt", "ralt", "lshift", "rshift") then
Fullscreen = not Fullscreen
love.window.setFullscreen(Fullscreen, "desktop")
end
end
The best solution! But set the Fullscreen as global and set fullscreen on Ctrl + F11, not on Escape
Re: Toggling Fullscreen with Escape
Posted: Mon Jun 20, 2022 3:06 pm
by marclurr
Just to add yet another variation on the same thing
, this is the toggle fullscreen implementation I have in my game:
Code: Select all
function toggleFullscreen()
love.window.setFullscreen(not love.window.getFullscreen(), "desktop")
updateDrawScaling()
end
It's triggered by a callback in my UI code, which its self is ultimately triggered by keypressed/keyreleased events. updateDrawScaling just figures out how much the drawing canvas must be scaled to fill the new window and it handles the case when the aspect ratios don't match (it's pretty inelegant though, and is driven by the window width ignoring the height).