I'm writing a game, and I saw the FPS algorithm doesn't work correctly (when he have to calculate more, he sleeps longer...) So, the question is very simple: how to calculate the sleeptime for having correct FPS?
I know how long it took to update the game one frame in microseconds and of course the FPS I want to reach.
I'm searching crazy for a simple example, but I can't find one....
The code may be in Java, C++ or pseudo....
Open up your Nvidia Control Panel settings and in the Global Settings section set Adaptive V-Sync to Half Refresh Rate. If you have a 60Hz monitor, this will limit your FPS to 30 frames per second. If you don't have a 60Hz monitor then simply use GeForce Experience to create a custom resolution.
The time you should spend on rendering one frame is 1/FPS
seconds (if you're aiming for, say 10 FPS, you should spend 1/10 = 0.1 seconds on each frame). So if it took X
seconds to render, you should "sleep" for 1/FPS - X
seconds.
Translating that to for instance milliseconds, you get
ms_to_sleep = 1000 / FPS - elapsed_ms;
If it, for some reason took more than 1/FPS
to render the frame, you'll get a negative sleep time in which case you obviously just skip the sleeping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With