Page 1 of 1
Different fps on different machines
Posted: Sat Mar 04, 2017 6:22 am
by megalukes
I usually run love on Mac and Windows 10. With vsync on, my fps is alway locked in 60 on my iMac, but when I run the same .love on Windows 10, fps keeps oscilating between 56-59 and it never reaches 60. Is there a reason for that?
Re: Different fps on different machines
Posted: Sat Mar 04, 2017 9:29 am
by evölbug
Different machines with differing specs. It's fine as long as you use delta time (dt) to compensate for FPS changes in your code.
Re: Different fps on different machines
Posted: Sat Mar 04, 2017 11:31 pm
by Positive07
VSync synchronizes to the screen FPS, if your screen has 59/58 FPS instead of 60 you won't get 60 FPS... It's as simple as that. If you don't use VSync you are tied to the CPU so it depends on what the CPU wants to give you and you may get greater variations on FPS.
zorg uses a custom love.run that gives you something close to constant 60 FPS... But you will never get perfect accuracy, so you should ALWAYS use dt
Re: Different fps on different machines
Posted: Sun Mar 05, 2017 3:29 am
by zorg
Positive07 wrote: ↑Sat Mar 04, 2017 11:31 pm
zorg uses a custom love.run that gives you something close to constant 60 FPS... But you will never get perfect accuracy, so you should ALWAYS use dt
Hello
Just wanted to mention that mine also uses dt, and without vsync, that's why i can separate the game loop into different speeds for updating and drawing things.
It has its own caveats though, of course; e.g. my usual loop doesn't use interpolation with drawing, so no matter how many updates happened, drawing will always render with the current state of the game, whatever that may be. It also doesn't do anything against screen tearing.
Re: Different fps on different machines
Posted: Sun Mar 05, 2017 4:23 am
by megalukes
Could you share your love.run code? I'd like to have a look, if that's ok. Thanks for the answers, guys.
Re: Different fps on different machines
Posted: Sun Mar 05, 2017 10:50 am
by zorg
megalukes wrote: ↑Sun Mar 05, 2017 4:23 am
Could you share your love.run code? I'd like to have a look, if that's ok. Thanks for the answers, guys.
Sure, just needed to sleep a bit.
Code: Select all
love.run = function()
if love.math then
love.math.setRandomSeed(os.time())
end
if love.load then love.load(arg) end
-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end
local dt = 0.0 -- delta time
local tr = 1/60 -- tick rate
local fr = 1/60 -- frame rate (this can be set dynamically to the screen's refresh rate too, with a bit of trickery :P)
local da = 0.0 -- draw accumulator
local ua = 0.0 -- update accumulator
-- Main loop time.
while true do
-- Process events.
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a
end
end
love.handlers[name](a,b,c,d,e,f)
end
end
-- Update dt, as we'll be passing it to update
if love.timer then
love.timer.step()
dt = love.timer.getDelta()
da = da + dt
ua = ua + dt
end
-- Call atomic
if love.atomic then love.atomic(dt) end
-- Call update
if ua > tr then
if love.update then
love.update(tr) -- will pass 0 if love.timer is disabled
end
ua = ua % tr
end
-- Call draw
if da > fr then
if love.graphics and love.graphics.isActive() then
love.graphics.clear(love.graphics.getBackgroundColor())
love.graphics.origin()
if love.draw then love.draw() end -- no interpolation
love.graphics.present()
end
da = da % fr
end
-- Optimal sleep time (for my pc, anyway), anything higher does not go below 0.40 % cpu utilization;
-- 0.001 results in 0.72 %, so this is an improvement.
if love.timer then love.timer.sleep(0.002) end
end
end
As i said above, this will only work "correctly" if you turn off vsync.