How to limit FPS?

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.
Post Reply
User avatar
LuckyChicken
Prole
Posts: 11
Joined: Sun Oct 01, 2017 11:16 am

How to limit FPS?

Post by LuckyChicken »

I would like to limit the FPS of my game to 60. Because I dont want to encumber the CPU of the player too much.

Mostly if you have vSync enabled the FPS are limited to 60. But not always. Sometimes the FPS are not different if you have vSync enabled or disabled. This could maybe be fixed by going into graphic driver settings etc. But I dont want to tell the player in my game that he should do that first. Thats just not how a game works!
So I wanted to ask here if theres maybe some other good solution to fix this problem.

I have this right now:

Code: Select all

function love.load()
  sleep = 0
end

function love.update(dt)
  love.timer.sleep(sleep)

  if love.timer.getFPS()>=60 then
    sleep = sleep + 0.0001 --Make the sleep longer and longer until the user doesnt have more then 60 FPS.
  end
end

function love.draw()
  love.graphics.print(love.timer.getFPS())
end

Its actually limiting the FPS to like 51 right now. And I dont think thats the "cleanest" and best solution.

Does anyone have an idea? Is there maybe some kind of Library which can limit the FPS?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to limit FPS?

Post by zorg »

LuckyChicken wrote: Sun Dec 17, 2017 6:57 pmI would like to limit the FPS of my game to 60. Because I dont want to encumber the CPU of the player too much.
The only way it gets stressed if you remove the default love.timer.sleep from love.run OR you perform too many calculations per frame/tick/whatever; and even then it shouldn't be an issue.
LuckyChicken wrote: Sun Dec 17, 2017 6:57 pmMostly if you have vSync enabled the FPS are limited to 60. But not always. Sometimes the FPS are not different if you have vSync enabled or disabled. This could maybe be fixed by going into graphic driver settings etc. But I dont want to tell the player in my game that he should do that first. Thats just not how a game works!
VSync on can either limit to the refresh rate of your screen (the one where the top-left corner of Löve's graphical window is, that is), a multiple of that, or some weird value for whatever reason; the latter could happen because of drivers, or the OS itself. Technically, it's not your responsibility to code against faults in other people's code, not on that scale anyway; The best you could do would be to either give them options (enable/disable vsync at the very least). Sadly, that's indeed how modern systems work; this isn't a console where you have the luxury of knowing how things are timed and such; The generic computer sphere has wildly differing hard- and software, for which not one person can safeguard against perfectly; that's the reality people need to face.
LuckyChicken wrote: Sun Dec 17, 2017 6:57 pmSo I wanted to ask here if theres maybe some other good solution to fix this problem.
Use dt, maybe separate the logic and render frames, use two variables holding your rates and two more as accumulators; modify love.run to only call love.update if the accumulator for that is >= the update rate, and to only call love.draw if the accumulator for that is >= the draw rate.
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.
User avatar
LuckyChicken
Prole
Posts: 11
Joined: Sun Oct 01, 2017 11:16 am

Re: How to limit FPS?

Post by LuckyChicken »

,,The only way it gets stressed if you remove the default love.timer.sleep from love.run OR you perform too many calculations per frame/tick/whatever; and even then it shouldn't be an issue." -Zorg

But I have a laptop. And this laptop isn't reaaally good. And when I run any LÖVE game where the FPS are not limited, my computer gets really loud and it works on 'high pressure' then. And I dont want that. This is why rather would like to limit the FPS.

,,VSync on can either limit to the refresh rate of your screen (the one where the top-left corner of Löve's graphical window is, that is), a multiple of that, or some weird value for whatever reason; the latter could happen because of drivers, or the OS itself. Technically, it's not your responsibility to code against faults in other people's code, not on that scale anyway; The best you could do would be to either give them options (enable/disable vsync at the very least). Sadly, that's indeed how modern systems work; this isn't a console where you have the luxury of knowing how things are timed and such; The generic computer sphere has wildly differing hard- and software, for which not one person can safeguard against perfectly; that's the reality people need to face." -Zorg

Yeah I will try my best and will definitely include an option to disable or enable VSync.

,,Use dt, maybe separate the logic and render frames, use two variables holding your rates and two more as accumulators; modify love.run to only call love.update if the accumulator for that is >= the update rate, and to only call love.draw if the accumulator for that is >= the draw rate." -Zorg

I actually found a good solution for now I think.
I dont want to try anymore to have it exactly at 60 limited. I think I will just try to get it under 80. That should be enough.

Code: Select all

  if love.timer.getFPS()>80 then
    sleep = sleep + 0.00001 --I also added one more zero here so its a bit more accurate. Maybe I will make this number even shorter in sometime.
  end
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: How to limit FPS?

Post by Tjakka5 »

There's 2 problems with the above solution:

1. I use a 144Hz monitor. I don't want to be limited to 80 fps, especially since my computer should be more than able to run the game at high refresh rates.

2. If the "real" fps is close to 80 this will cause a lot of jittering.


I suggest you have a look at Zorg's answer. It really is by far the best way of doing it.
If you need an example for implementing it, I know Zorg has a lot of examples in his post history.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to limit FPS?

Post by zorg »

Also, just wanted to say that laptops (with windows on it) have performance settings you can change, either letting the CPU always work at 100% or throttling it when it's not needed. Löve may or may not force the "high performance" behaviour due to some setting even if your code doesn't do all that much.

As for it being loud, well, i'd recommend bringing it into a repair shop, since i know from experience that internal laptop fans may deform due to excessive heat, making them louder than they should be.

Oh, and doing what you're doing there, incrementing the sleep time, is not the best of ideas. Especially if your initial value (which you didn't seem to care to share) is less than 0.001 exactly, since that's too small of a value for love.timer.sleep to actually do anything, and it might stress your CPU needlessly.
I did explain elsewhere my own experience that 0.002 worked better -on my computer- than 0.001, but there's no computers out there where less than a microsecond sleep time would do anything but tax the scheduler, and make a process consume a CPU core.
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 4 guests