SFML community forums

Help => General => Topic started by: TheGuerilla on April 11, 2016, 01:16:47 am

Title: Problem locking framerate
Post by: TheGuerilla on April 11, 2016, 01:16:47 am
When I locked the framerate, I was assuming that to stay at 60 FPS, you take the amount of time 60FPS would be in microseconds minus the time it took to actually process the frame and sleep that long; however, it doesn't work for me.
uint64_t allocFrameTime = 1000000 / SEV_Framelock;
if (between < allocFrameTime)
        sf::sleep(sf::microseconds(allocFrameTime - between));
between is of course the time the frame took to process in microseconds, and SEV_Framelock is 60. However, when I use this code, the game runs at ~100 fps. Uncapped, it reaches ~900 FPS. What am I doing wrong? I don't want it capped at 100, I want it at 60.
Title: Re: Problem locking framerate
Post by: Tukimitzu on April 11, 2016, 06:05:13 am
If SEV_Framelock is 60 seconds (and you converted to microseconds), allocFrameTime will be 0, because 1,000,000/60,000,000 is 0.0166 and allocFrameTime is an integer type.

Then between < allocFrameTime will always be false (except I don't know what between holds).

But why do you want to sleep anyway? Are you using threads?

Title: AW: Problem locking framerate
Post by: eXpl0it3r on April 11, 2016, 08:13:18 am
Additionally don't forget that the precision of the sleep function can vary, as such you will never get a fully stable framerate and you should not build your code assuming the FPS stays the same.
Title: Re: Problem locking framerate
Post by: Jesper Juhl on April 11, 2016, 03:10:52 pm
Why don't you use setFramerateLimit() (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Window.php#af4322d315baf93405bf0d5087ad5e784)?
Title: Re: Problem locking framerate
Post by: TheGuerilla on April 12, 2016, 03:09:17 am
If SEV_Framelock is 60 seconds (and you converted to microseconds), allocFrameTime will be 0, because 1,000,000/60,000,000 is 0.0166 and allocFrameTime is an integer type.

Then between < allocFrameTime will always be false (except I don't know what between holds).

But why do you want to sleep anyway? Are you using threads?
no the integer value of SEV_FrameLock is 60, so it is just 1,000,000 / 60 which is ~16,666. As for blaming this on the sleep function, I have delta timing so it's not an issue that way, it just that if I say I want to lock it at 60, I would like the framerate around anywhere from 60-65ish, not 100.

But I didn't know that function existed. Imma give it a go right now.

EDIT: After using that magical function, it caps the framerate well. Thanks!
Title: AW: Problem locking framerate
Post by: eXpl0it3r on April 12, 2016, 07:50:26 am
Well your delta value is not good enough if the sleep function always doesn't sleep long enough. You'd have to dynamically adjust sleep time to compensate for the imprecision.

As said setFramerateLimit sets the precision of sleep timer to the max, which gives a higher accuracy. ;)