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

Author Topic: Calculating Framerate  (Read 9833 times)

0 Members and 1 Guest are viewing this topic.

Paki Programmer

  • Newbie
  • *
  • Posts: 31
    • View Profile
Calculating Framerate
« on: June 12, 2012, 03:27:05 am »
Could someone please help me understand how to create a timer in SFML 2.0 RC that keeps track of framerate? I would like my game to run at the same speed on all systems. For instance how would i go about getting about 60 fps? I tried the tutorials and looking around on the web but I'm still kinda stuck. Thanks
« Last Edit: June 12, 2012, 03:35:50 am by Paki Programmer »

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Calculating Framerate
« Reply #1 on: June 12, 2012, 04:58:17 am »
Keeping track of it:
 - Use a sf::Clock to get the time that has passed since the last draw in seconds.
 - f = 1/T

Controlling it:
 - sf::Window::setFramerateLimit(unsigned int limit)
 - sf::Window::setVerticalSyncEnabled(bool enabled)

Also:
 - Documentation and Board-Search.

Paki Programmer

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Calculating Framerate
« Reply #2 on: June 12, 2012, 05:11:34 pm »
So should the time be in seconds or milliseconds when calculating framerate? And so I guess you would just put "F" in for the parameter in Setframerate() function?

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Calculating Framerate
« Reply #3 on: June 12, 2012, 05:37:12 pm »
Uhm, what? I told you it's in seconds, you want to get the "frames per second", don't you?

No you don't. f = 1/T is the formula to calculate the current framerate based on the time that has passed since the last draw (http://en.wikipedia.org/wiki/Frequency). Using that value to set the framerate limit would make absolutely no sense.
If you want 60 fps you use setFramerateLimit(60), or any other value. If you want to use VSync you use SetVerticalSyncEnabled(true).

Quote
Also:
 - Documentation and Board-Search.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Calculating Framerate
« Reply #4 on: June 13, 2012, 12:38:25 am »
- Use a sf::Clock to get the time that has passed since the last draw in seconds.

This is one way and not always the best one. ;)
And in code this would look like:
sf::Clock clock;
float dt = 0.f;
// At the end of the game loop
dt = clock.restart().asSeconds();

This is not always the best solution since you base only upon the timespan of the last update and draw phase. If anything would spike in between there for only one frame, it would make you're framerate drop heavily only to jump back up for the next frame. This can get really annoying and totally break your smooth movments.

Other possibilities are:
  • Fixed timestep (as suggested in this often linked article)
  • To calculate the framerate over a longer timespan, like 1s or 0.5s.
  • ... I guess there could be even more ...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/