Fullscreen problem in 0.9.2

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
taateli05
Prole
Posts: 7
Joined: Sat Jun 27, 2015 1:31 pm

Fullscreen problem in 0.9.2

Post by taateli05 »

Hello! i'am making game in Love2d 0.9.2 (Ubuntu 14.04). But i have a problem. i want if game window is not fullscreen, then you can go to fullscreen pressing "f". But if you are already in fullscreen mode, then you can go to normal window mode pressing "f" key again.


Here are my code:

Code: Select all

function love.update(dt)
    if love.keyboard.isDown("f") then
    love.window.setFullscreen(true, "desktop")
    elseif love.keyboard.isDown("f") then
    love.window.setFullscreen(false, "desktop")
end



When i run that code, it just opens normal window where are my game and enters fullscreen pressing "f" but if i press "f" again, then it does not exit fullscreen mode. :(

Please help me!!! And sorry my very bad english. i am from finland an 10 yo
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Fullscreen problem in 0.9.2

Post by Robin »

Okay!

First, you check for love.keyboard.isDown("f") twice. So let's see what that does:

Code: Select all

if the f key is down, then:
    make the window fullscreen
else, if the f key is not down then:
    if the f key is down then:
        make the window not fullscreen
So you see, the last part is never used, because the f key cannot both be down and not down at the same time. So what do you do? Check if the window already is fullscreen with [wiki]love.window.getFullscreen[/wiki]:

Code: Select all

function love.update(dt)
    if love.keyboard.isDown("f") then
        if love.window.getFullscreen() then
            love.window.setFullscreen(false, "desktop")
        else
            love.window.setFullscreen(true, "desktop")
        end
    end
end
Now, this works, but it can be shorter:

Code: Select all

function love.update(dt)
    if love.keyboard.isDown("f") then
        love.window.setFullscreen(not love.window.getFullscreen(), "desktop")
    end
end
Okay, but now it checks every frame, so if you keep the f key down just a little bit too long, the game starts to go to fullscreen and back many times per second.

So how do we fix that? With [wiki]love.keypressed[/wiki].

Code: Select all

function love.update(dt)
end

function love.keypressed(key, isrepeat)
    if key == "f" then
        love.window.setFullscreen(not love.window.getFullscreen(), "desktop")
    end
end
Help us help you: attach a .love.
taateli05
Prole
Posts: 7
Joined: Sat Jun 27, 2015 1:31 pm

Re: Fullscreen problem in 0.9.2

Post by taateli05 »

Robin wrote:Okay!

First, you check for love.keyboard.isDown("f") twice. So let's see what that does:

Code: Select all

if the f key is down, then:
    make the window fullscreen
else, if the f key is not down then:
    if the f key is down then:
        make the window not fullscreen
So you see, the last part is never used, because the f key cannot both be down and not down at the same time. So what do you do? Check if the window already is fullscreen with [wiki]love.window.getFullscreen[/wiki]:

Code: Select all

function love.update(dt)
    if love.keyboard.isDown("f") then
        if love.window.getFullscreen() then
            love.window.setFullscreen(false, "desktop")
        else
            love.window.setFullscreen(true, "desktop")
        end
    end
end
Now, this works, but it can be shorter:

Code: Select all

function love.update(dt)
    if love.keyboard.isDown("f") then
        love.window.setFullscreen(not love.window.getFullscreen(), "desktop")
    end
end
Okay, but now it checks every frame, so if you keep the f key down just a little bit too long, the game starts to go to fullscreen and back many times per second.

So how do we fix that? With [wiki]love.keypressed[/wiki].

Code: Select all

function love.update(dt)
end

function love.keypressed(key, isrepeat)
    if key == "f" then
        love.window.setFullscreen(not love.window.getFullscreen(), "desktop")
    end
end

thank you!! :awesome:
Post Reply

Who is online

Users browsing this forum: H.I, slime and 1 guest