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

Author Topic: How to make a game loop and draw the fps on the screen?  (Read 2726 times)

0 Members and 1 Guest are viewing this topic.

Mago

  • Newbie
  • *
  • Posts: 1
    • View Profile
How to make a game loop and draw the fps on the screen?
« 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();
        }
 


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: How to make a game loop and draw the fps on the screen?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything