libgdx/libgdx

FPS Cap at runtime

Andrei0100 opened this issue · 4 comments

Issue details

I would like to request a feature that allows for dynamic adjustments to the FPS cap at runtime in LibGDX applications. This functionality would enable developers to offer users the option to select their preferred FPS cap within the game's options menu, enhancing user experience by accommodating a wider range of preferences and hardware capabilities.

Reproduction steps/code

As this is a feature request rather than a bug report, there are no specific reproduction steps or code. However, the desired functionality would allow developers to use code similar to the following to adjust the FPS cap based on user input:

// Example method call to adjust FPS cap dynamically
Gdx.graphics.setFrameRateLimit(targetFPS);

Version of libGDX and/or relevant dependencies

This feature request is relevant for all current versions of libGDX as of the last version available.

Stacktrace

N/A - This is a feature request.

Please select the affected platforms

  • Android
  • iOS
  • HTML/GWT
  • Windows
  • Linux
  • macOS

I think this is so simple to implement by ourself the consumer coders that consume this libgdx library.

First, you declare a function that takes in the elapsed time between the two frames. So that, later you can pass delta time to the function.
in the render() method of Game class or Screen class.

Let's take one example if you want to limit it to 60FPS, then each elapsed time in between two frames should be 1/60 second or 0.01667. Then, you check if the elapsed time is less than that, you need to add that up so it becomes 0.01667. To get how much you need to add is 0.01667 - elapsedTime, and store it as temporary variable. Meaning, you add the elapsedTime with this temporary variable so it becomes 0.01667 second every frame.

If it is higher you substract by that amount.
I.e elapsedTime-= (elapsedTime-0.01667).
Hopefully, you get the idea.
Then, you just return that number.
Then, you can use that return value in the render() method.

AnotherCoder1

I tried everything but only Thread.sleep works, It would be better to have a built-in method like
"Gdx.graphics.setFrameRateLimit(targetFPS);" for straightforward frame rate control. Thanks for your suggestions!

How about Gdx.graphics.setForegroundFPS(int)?

Oh wow... for some reason I didn't see it, THANK YOU!