SFML community forums

Help => Window => Topic started by: Paki Programmer on June 12, 2012, 03:27:05 am

Title: Calculating Framerate
Post by: Paki Programmer 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
Title: Re: Calculating Framerate
Post by: Perde 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.
Title: Re: Calculating Framerate
Post by: Paki Programmer 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?
Title: Re: Calculating Framerate
Post by: Perde 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.
Title: Re: Calculating Framerate
Post by: eXpl0it3r 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: