Page 1 of 2

Limiting FPS in 0.9.0

Posted: Wed Feb 12, 2014 6:24 pm
by aidenn
Hi,

I've been seraching for a bit now, but only found outdated examples for 0.8.0. What's the proper way to limit FPS in 0.9.0?

Re: Limiting FPS in 0.9.0

Posted: Wed Feb 12, 2014 7:02 pm
by Azhukar

Code: Select all

local t = 0
local fps = 1/30
function love.update(dt)
	t=t+dt
	if (t < fps) then return end
	t=t%fps

	print("30 times per second")
end
You can also turn on vsync in conf.lua.

Re: Limiting FPS in 0.9.0

Posted: Wed Feb 12, 2014 7:09 pm
by aidenn
But that only makes an action happen 30 times in a second. I'd like to have proper framelimiting for everything.

Vsync in LÖVE, unfortunately, tends to drop single frames here and there.

Re: Limiting FPS in 0.9.0

Posted: Wed Feb 12, 2014 7:38 pm
by Automatik
Try this:

Code: Select all

local fps = 1/30
function love.update(dt)
   if (dt < fps) then
       love.timer.sleep(fps-dt)
   end
end

Re: Limiting FPS in 0.9.0

Posted: Wed Feb 12, 2014 7:54 pm
by Azhukar
Automatik wrote:Try this:

Code: Select all

local fps = 1/30
function love.update(dt)
   if (dt < fps) then
       love.timer.sleep(fps-dt)
   end
end
love.timer.sleep((fps-dt)*2)

Re: Limiting FPS in 0.9.0

Posted: Wed Feb 12, 2014 8:18 pm
by aidenn
Thanks, it kind-of works, but it's not really what I had in mind.

Let me tell you why I need it first:

I'm doing my first proper LÖVE project I've been meaning to do since oh so long. Today I did collisions, which worked nicely. However, when I gave the game to my friend with a really crappy PC, collisions went a bit glitchy due to lower FPS. It's not really broken, but it DOES behave differently, even with proper dt usage.

So, if I limit the framerate, I can develop collisions and movement targeting that exact FPS which will behave identically on all systems. Two options there: I can use Vsync or I can use a hard framerate cap. Vsync kinda works, but people may not want it, which leaves the second option. Earlier I wrote that Vsync loses a frame or two here or there... that FPS limit example kills any semblance of smoothness, probably because love.update() isn't really the place to do it.

I saw this post back when I was searching, which looks like something I might want: http://love2d.org/forums/viewtopic.php? ... 184#p75184, however we don't have getMicroTime() anymore and I'm once again in a stump.

Re: Limiting FPS in 0.9.0

Posted: Wed Feb 12, 2014 8:25 pm
by Azhukar
aidenn wrote:I'm doing my first proper LÖVE project I've been meaning to do since oh so long. Today I did collisions, which worked nicely. However, when I gave the game to my friend with a really crappy PC, collisions went a bit glitchy due to lower FPS. It's not really broken, but it DOES behave differently, even with proper dt usage.
Then you only need to limit the amount of times your physics update gets called and pass it a fixed dt value instead of one you get from update.

In other words: use the method from my first post and pass your physics update 1/30 instead of dt. That way the most the game will do when it cannot reach 30fps is run slower.
aidenn wrote:I saw this post back when I was searching, which looks like something I might want: http://love2d.org/forums/viewtopic.php? ... 184#p75184, however we don't have getMicroTime() anymore and I'm once again in a stump.
love.timer.getMicroTime() is now love.timer.getTime().

Re: Limiting FPS in 0.9.0

Posted: Wed Feb 12, 2014 8:37 pm
by aidenn
Oooh, that explains so much. Thanks! I guess I don't need to limit FPS arbitrarily anymore. I have a question though - is it accurate? That is, can updates achieved this way synchronize perfectly with Hz 1:1?

BTW, out of curiousity I tried to test that limiter I linked with getTime() but couldn't get it to work anyway.

Re: Limiting FPS in 0.9.0

Posted: Thu Feb 13, 2014 8:44 am
by Azhukar
aidenn wrote:That is, can updates achieved this way synchronize perfectly with Hz 1:1?
I wouldn't rely on that.

Re: Limiting FPS in 0.9.0

Posted: Thu Feb 13, 2014 9:02 am
by aidenn
That's a bummer. Any way to do clean and healthy frame-Hz relationship with LÖVE?