Limit framerate to exactly 60

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Limit framerate to exactly 60

Post 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
eforekim
Prole
Posts: 4
Joined: Sat Sep 19, 2020 12:20 pm

Re: Limit framerate to exactly 60

Post 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
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Limit framerate to exactly 60

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests