1
General / Re: How to get FPS in SFML 2
« on: July 29, 2020, 03:10:05 pm »
Hi everyone, even though this post is old, it was at the top of my google search. Moreover, I found that the answer given here is unsatisfactory. I therefore propose the following code.
#include <SFML/Audio.hpp>
float fps;
sf::Clock clock = sf::Clock::Clock();
sf::Time previousTime = clock.getElapsedTime();
sf::Time currentTime;
while (window.isOpen())
{
// Event handling
// your looped code
currentTime = clock.getElapsedTime();
fps = 1.0f / (currentTime.asSeconds() - previousTime.asSeconds()); // the asSeconds returns a float
std::cout << "fps =" << floor(fps) << std::endl; // flooring it will make the frame rate a rounded number
previousTime = currentTime;
}
This will display your fps on the console, if you want to display it in the sfml window, you will have to load a font file and use the sfml text function which require a few extra steps.
I hope someone will find it helpful.
#include <SFML/Audio.hpp>
float fps;
sf::Clock clock = sf::Clock::Clock();
sf::Time previousTime = clock.getElapsedTime();
sf::Time currentTime;
while (window.isOpen())
{
// Event handling
// your looped code
currentTime = clock.getElapsedTime();
fps = 1.0f / (currentTime.asSeconds() - previousTime.asSeconds()); // the asSeconds returns a float
std::cout << "fps =" << floor(fps) << std::endl; // flooring it will make the frame rate a rounded number
previousTime = currentTime;
}
This will display your fps on the console, if you want to display it in the sfml window, you will have to load a font file and use the sfml text function which require a few extra steps.
I hope someone will find it helpful.