Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem locking framerate  (Read 2569 times)

0 Members and 1 Guest are viewing this topic.

TheGuerilla

  • Newbie
  • *
  • Posts: 27
  • Cynical Prick.exe
    • View Profile
    • Email
Problem locking framerate
« 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.

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Problem locking framerate
« Reply #1 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?

« Last Edit: April 11, 2016, 06:09:35 am by Tukimitzu »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Problem locking framerate
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Problem locking framerate
« Reply #3 on: April 11, 2016, 03:10:52 pm »
Why don't you use setFramerateLimit()?

TheGuerilla

  • Newbie
  • *
  • Posts: 27
  • Cynical Prick.exe
    • View Profile
    • Email
Re: Problem locking framerate
« Reply #4 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!
« Last Edit: April 12, 2016, 03:19:50 am by TheGuerilla »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Problem locking framerate
« Reply #5 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/