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

Author Topic: FPS to Graphical Font?  (Read 2718 times)

0 Members and 1 Guest are viewing this topic.

Andershizzle

  • Newbie
  • *
  • Posts: 16
    • View Profile
FPS to Graphical Font?
« on: August 30, 2009, 04:57:59 am »
Hi I know this probably seems newb but I've been searching and fooling around but what I'm trying to do is get my app to write the Frame Rate to the top right of the string like "FPS: #" using sf::string.

Is there any way to get the FPS and convert it to an sf::string to write it like this or another way to do it? Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
FPS to Graphical Font?
« Reply #1 on: August 30, 2009, 09:59:47 am »
FPS is 1/window.GetFrameTime().
Then to convert it to a std::string you search "C++ convert number to string" in Google.
Then you pass it to your sf::String using SetText ;)
Laurent Gomila - SFML developer

Meltra Bour

  • Newbie
  • *
  • Posts: 28
    • View Profile
FPS to Graphical Font?
« Reply #2 on: August 30, 2009, 10:15:57 am »
we use something like ...

Code: [Select]

      // Outside of main loop
        unsigned int Frames = 0;
        float FPS, Time;

        sf::String FpsString;
        sf::Clock Timer;

      // inside main loop
        Frames++;
        Time = Timer.GetElapsedTime();
        if (Time >= 2)
        {
            FPS = Frames / Time;

            std::ostringstream os;
            if(os << (int)FPS)
               FpsString.SetText("FPS : " + os.str());

            Timer.Reset();
            Frames = 0;
        }


this updates every 2sec to give a general idea of the average fps, lower/increase the 2 in the if statement if you want it to update faster/slower. Just stay away from the usual sprintf() solution to convert float to string, it's a bad one if you ask me.