How to limit my frames per second?

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
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How to limit my frames per second?

Post by Nixola »

You do NOT have to change DT, unless you know what you're doing, changing DT will not change the framerate. The wiki has also a more complex snip that caps more accurately
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Number-41
Prole
Posts: 5
Joined: Mon May 28, 2012 12:17 pm

Re: How to limit my frames per second?

Post by Number-41 »

I don't want to cap accurately (if that means a matter of 5 or 6 fps), I just want a clear way on how to reduce it to around 50. Which none of the above methods (or the one on the wiki) do (though thanks for the quick replies :) ).

Also the wiki says that it doesn't support that function anymore in 8.0.
Last edited by Number-41 on Mon May 28, 2012 8:51 pm, edited 1 time in total.
User avatar
slime
Solid Snayke
Posts: 3163
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: How to limit my frames per second?

Post by slime »

Here's my love.run. It works decently, although I get stuttering at <= 60 max fps, but I'm not sure what to do about it.

Code: Select all

function love.run()
	love.load(arg)

	local dt = 0
	
	while true do
		local frametimestart = love.timer.getMicroTime()
		
		love.event.pump()
		
		for e,a,b,c,d in love.event.poll() do
			if e == "quit" then
				if not love.quit or not love.quit() then
					if love.audio then
						love.audio.stop()
					end
					return
				end
			end
			love.handlers[e](a,b,c,d)
		end

		love.timer.step()
		dt = love.timer.getDelta()

		love.update(dt)
		
		love.graphics.clear()
		love.draw()
		love.graphics.present()
		
		local frametimeend = love.timer.getMicroTime()
		local framedelta = frametimeend - frametimestart
		
		if maxfps and maxfps > 0 then -- 0 is unlimited
			local max_dt = 1/maxfps
			local sleeptime = max_dt - framedelta
			
			if sleeptime >= 0.001 then -- love will truncate anything less than 1ms down to 0 internally
				love.timer.sleep(sleeptime)
			end
		else
			love.timer.sleep(0.001)
		end
	end
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to limit my frames per second?

Post by Robin »

tsturzl wrote:using the sleep timer just idles the instructions for that duration. You should set the cap through dt, dt is a pointer that references a value from my understanding. Therefore allowing you to change its value outside your scope.
That is all kinds of wrong.
slime wrote:Here's my love.run. It works decently, although I get stuttering at <= 60 max fps, but I'm not sure what to do about it.
Maybe it works better if you provide an upper limit for sleeptime.
Help us help you: attach a .love.
User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: How to limit my frames per second?

Post by Lap »

I've enjoyed this more general article:

http://gafferongames.com/game-physics/f ... -timestep/
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: How to limit my frames per second?

Post by Inny »

If nothing changes during a love.update, then whatever your draw in the next love.draw will be identical to the previous frame. In that regard, you accumulate up enough love.update calls and then react when enough has been called to fit your desired framerate. For instance:

Code: Select all

dt_buffer = 0
desired = 1/60
function love.update(dt)
    dt_buffer = dt_buffer + dt
    if dt_buffer > desired then
        dt_buffer = dt_buffer - desired
        updateWorld()
    end
end
Post Reply

Who is online

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