SFML community forums

Help => Graphics => Topic started by: Andershizzle on August 30, 2009, 04:57:59 am

Title: FPS to Graphical Font?
Post by: Andershizzle 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.
Title: FPS to Graphical Font?
Post by: Laurent 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 ;)
Title: FPS to Graphical Font?
Post by: Meltra Bour 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.