SFML community forums

Help => General => Topic started by: Mago on April 11, 2021, 09:36:10 pm

Title: How to make a game loop and draw the fps on the screen?
Post by: Mago on April 11, 2021, 09:36:10 pm
Hello, I've made the game loop from the SFML Game Development book. But now I want to figure out how to show the FPS on the screen. What is the right way of doing it?
        int fps = 60;
        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        sf::Time timePerFrame = sf::seconds(1.f / fps);

        while (window.isOpen())
        {
                timeSinceLastUpdate += clock.restart();
                processEvents();

                while (timeSinceLastUpdate > timePerFrame) // time per frame
                {
                        timeSinceLastUpdate -= timePerFrame;
                        processEvents();
                        update(timePerFrame);
                }

                render();
        }
 

Title: Re: How to make a game loop and draw the fps on the screen?
Post by: eXpl0it3r on April 19, 2021, 09:25:32 pm
With the current setup, you need to get the value from clock.restart() and divide it by 1.f / deltaTime.
Then you can use std::to_string(fps) to convert it to a string, which you can then draw with a sf::Text object after loading an sf::Font object.

If this is all a bit much, then I recommend to keep reading on, you'll eventually learn most of this in the book.

Btw. I believe the processEvents should only exist once in your code.