Page 2 of 2

Re: Limit framerate to exactly 60

Posted: Sun Apr 07, 2019 9:36 am
by raidho36
Shadowblitz16 wrote: Fri Mar 22, 2019 12:14 am I would like to know how to safeguard against gigantic deltas.
You can manually throttle thread update rate instead of using OS sleep function by spinning the CPU instead. That way you will have very precise and reliable control over the frame timing. The downside is that it stresses the CPU core to 100% at all times. The OS gives no guarantees that the program will wake up after sleep in exactly specified amount of time. It could wake up sooner than specified, or much later.

Instead of using love.timer.sleep you'd run an infinite loop that checks if it's time to update yet.

Code: Select all

while currentTime < targetTime do currentTime = love.timer.getTime ( ) end
targetTime = currentTime + 1/60

Re: Limit framerate to exactly 60

Posted: Tue Dec 15, 2020 2:21 pm
by eforekim
what about this? it might be a little too simple but it works.

Code: Select all

function love.load()
	fps=60
end

function love.draw(dt)
	love.timer.sleep((1/fps)-dt)
	--other stuff
end

Re: Limit framerate to exactly 60

Posted: Tue Dec 15, 2020 7:19 pm
by zorg
eforekim wrote: Tue Dec 15, 2020 2:21 pm what about this? it might be a little too simple but it works.

Code: Select all

function love.load()
	fps=60
end

function love.draw(dt)
	love.timer.sleep((1/fps)-dt)
	--other stuff
end
It works if you don't have any code you want to run, otherwise you need to calculate how much time that took, and subtract that as well, not just whatever dt was... also, you replied to a year old thread.