Page 1 of 1

Fullscreen problem in 0.9.2

Posted: Sat Jul 04, 2015 9:31 am
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

Re: Fullscreen problem in 0.9.2

Posted: Sat Jul 04, 2015 9:56 am
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

Re: Fullscreen problem in 0.9.2

Posted: Sat Jul 04, 2015 10:17 am
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: