So i have this FPS counter here but its counting it way too fast for me:
void Fps::Update(sf::Clock& clock) {
float currentTime = clock.getElapsedTime().asSeconds();
float fps = 1.0f / currentTime;
std::string fpsAsString = std::to_string((int)fps);
text.setString("FPS: " + (fpsAsString));
clock.restart();
}
If i do:
void Fps::Update(sf::Clock& clock) {
if (clock.getElapsedTime().asMilliseconds() >= 100) {
float currentTime = clock.getElapsedTime().asSeconds();
float fps = 1.0f / currentTime;
std::string fpsAsString = std::to_string((int)fps);
text.setString("FPS: " + (fpsAsString));
clock.restart();
}
}
it shows 9fps constantly which is clearly wrong.
I want it to calculate FPS every 100ms or so, so its slower and more pleasant for the eye but i don’t quite understand how to do that.
Could somebody explain how can i do it and why the method i tried doesnt work?
PS. i also tried creating a clock separate from the one used in function like:
void Fps::Update(sf::Clock& clock) {
sf::clock clock2;
if (clock2.getElapsedTime().asMilliseconds() >= 100) {
float currentTime = clock.getElapsedTime().asSeconds();
float fps = 1.0f / currentTime;
std::string fpsAsString = std::to_string((int)fps);
text.setString("FPS: " + (fpsAsString));
clock.restart();
}
}
New contributor
Rogl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2