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

Author Topic: How to get FPS in SFML 2  (Read 57247 times)

0 Members and 1 Guest are viewing this topic.

Felshire

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to get FPS in SFML 2
« on: February 12, 2012, 09:46:56 am »
Hers what I have so far:
Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <string>
sf::Time thetime;
sf::Clock elapsed;

std::string IntToString(int d)
{
   std::stringstream ss;
   ss << d;
   return ss.str();
}

std::string GetFrameRate()
{    static int frameCounter = 0;
      static int fps = 0;
      frameCounter++;
      thetime = elapsed.GetElapsedTime();
      if(thetime.AsMilliseconds() > 999)
      {
         fps = frameCounter;
         frameCounter = 0;
         elapsed.Restart();
      }

      return IntToString(fps);
}

int main()
{
    sf::RenderWindow Window(sf::VideoMode(1024, 768, 32), "Test");
    Window.EnableVerticalSync(true);

    sf::Text FPS("0", sf::Font::GetDefaultFont(), 25);


    while (Window.IsOpen())
    {
            Window.Clear();
            FPS.SetString(GetFrameRate());
            Window.Draw(FPS);
            Window.Display();
        }

    return 0;
}


The problem with my code is that its not exactly frames per second.
Its always slightly higher than 1000 milliseconds and would probably be much higher if I were to draw more things in the loop.

So what do I change here?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to get FPS in SFML 2
« Reply #1 on: February 12, 2012, 10:01:38 am »
Why is it such a problem? FPS should only be used to have a rough estimate of the performances, it doesn't have to be accurate.

You can try this way:
Code: [Select]
int main()
{
    sf::RenderWindow Window(sf::VideoMode(1024, 768, 32), "Test");

    sf::Clock clock;
    float lastTime = 0;
    while (Window.IsOpen())
    {
            ...
            float currentTime = clock.Restart().AsSeconds();
            float fps = 1.f / (currentTime - lastTime);
            lastTime = currentTime;
    }

    return 0;
}

It won't be 100% accurate because frame times are tiny, but at least the error won't accumulate over time.
Laurent Gomila - SFML developer

Felshire

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to get FPS in SFML 2
« Reply #2 on: February 12, 2012, 10:07:15 am »
Quote from: "Laurent"
Why is it such a problem? FPS should only be used to have a rough estimate of the performances, it doesn't have to be accurate.

You can try this way:
Code: [Select]
int main()
{
    sf::RenderWindow Window(sf::VideoMode(1024, 768, 32), "Test");

    sf::Clock clock;
    float lastTime = 0;
    while (Window.IsOpen())
    {
            ...
            float currentTime = clock.Restart().AsSeconds();
            float fps = 1.f / (currentTime - lastTime);
            lastTime = currentTime;
    }

    return 0;
}

It won't be 100% accurate because frame times are tiny, but at least the error won't accumulate over time.


Didnt know you could do that "clock.Restart().AsSeconds();" thing. Thanks.

But how am i supposed to convert a float to a string that I can print out with Window.Draw()?

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
How to get FPS in SFML 2
« Reply #3 on: February 12, 2012, 10:27:31 am »
Quote
But how am i supposed to convert a float to a string that I can print out with Window.Draw()?


It should be something like this (I didn't test this code).
Code: [Select]
char c[10];
sprintf(c, "%f", theFloat);
std::string string(c);
sf::String str(string);
sf::Text text(str);
window.Draw(text);
TGUI: C++ SFML GUI

Felshire

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to get FPS in SFML 2
« Reply #4 on: February 12, 2012, 10:38:29 am »
Quote from: "Laurent"
Why is it such a problem? FPS should only be used to have a rough estimate of the performances, it doesn't have to be accurate.

You can try this way:
Code: [Select]
int main()
{
    sf::RenderWindow Window(sf::VideoMode(1024, 768, 32), "Test");

    sf::Clock clock;
    float lastTime = 0;
    while (Window.IsOpen())
    {
            ...
            float currentTime = clock.Restart().AsSeconds();
            float fps = 1.f / (currentTime - lastTime);
            lastTime = currentTime;
    }

    return 0;
}

It won't be 100% accurate because frame times are tiny, but at least the error won't accumulate over time.


Also,
That code doesnt produce any meaningful results....

Its just random numbers if you print them out

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
How to get FPS in SFML 2
« Reply #5 on: February 12, 2012, 11:04:03 am »
Shouldn't this line
Quote
float fps = 1.f / (currentTime - lastTime);

be this:
Code: [Select]
float fps = 1.f / currentTime
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to get FPS in SFML 2
« Reply #6 on: February 12, 2012, 12:17:03 pm »
Quote
But how am i supposed to convert a float to a string

The same way you convert an int to a string.

Quote
That code doesnt produce any meaningful results....

I wrote it quickly without testing, but in case there's an error you should be able to correct it, I think you get the idea.
Laurent Gomila - SFML developer

Felshire

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to get FPS in SFML 2
« Reply #7 on: February 12, 2012, 01:18:37 pm »
Quote from: "Laurent"
Quote
But how am i supposed to convert a float to a string

The same way you convert an int to a string.

Quote
That code doesnt produce any meaningful results....

I wrote it quickly without testing, but in case there's an error you should be able to correct it, I think you get the idea.


unfortunately i dont ;_;

toying around and cant get it without using my first method

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to get FPS in SFML 2
« Reply #8 on: February 12, 2012, 02:38:13 pm »
There are two ways to compute the FPS:
1. counting how many frames you have in one second (that's your code)
2. counting how many seconds you have in one frame, and inverting the result (that's my code)

So basically my code is fps = 1 / frame_duration. Nothing fancy ;)
Laurent Gomila - SFML developer

imanifacier

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: How to get FPS in SFML 2
« Reply #9 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.

 

anything