c++sfmlframe-rateostringstream

Can't get my FPS number to show with SFML


  1. I am making a program that needs to have my FPS counter listed on the top left of the screen. I am using SFML to output my program into a window.
  2. I have an FPS.cpp file that contains the class for my FPS counter. There are two methods in this class, "getFPS()" and "update()".

fps.cpp

#include <SFML/Graphics.hpp>
#include <iostream>

class FPS
{
public:
   FPS() : mFrame(0), mFps(0) {}


   const unsigned int getFPS() const { return mFps; }
private:
   unsigned int mFrame;
   unsigned int mFps;
   sf::Clock mClock;

public:
   void update()
   {
       if (mClock.getElapsedTime().asSeconds() >= 1.f)
       {
           mFps = mFrame;
           mFrame = 0;
           mClock.restart();
       }

       ++mFrame;
   }
};

Game.cpp (SFML inits and Game Loop only)

int Game::run()
{
    //Prepare the Window
    window.create(VideoMode(1920, 1080), "GameWindow", Style::Fullscreen);

    FPS fps;
    Text fpsNum;
    sf::Font font;
    font.loadFromFile("fonts/KOMIKAP_");
    fpsNum.setFont(font);
    fpsNum.setCharacterSize(75);
    fpsNum.setFillColor(Color::Green);
    fpsNum.setPosition(20, 20);

    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (Keyboard::isKeyPressed(Keyboard::Escape))
            {
                window.close();
            }
        }

        

        //Update
        fps.update();
        std::cout << fps.getFPS() << std::endl;
        std::ostringstream ss;
        ss << fps.getFPS();
        fpsNum.setString(ss.str());

        //Draw
        window.clear();
        
        
        window.draw(fpsNum);
        window.display();
    }

    
    return 0;
}
  1. When I run std::cout << fps.getFPS() << std::endl; the correct FPS is being shown in the console, so I know that getFPS() is doing it's job. From what I understand, the problem lies somewhere in how SFML is displaying the actual text because all I am getting on my screen is a green dot where the FPS should be.

Picture: Picture of the output, note the green dot at the top left corner.

NOTE: I understand that the main function is not shown here, there is no reason for me to show this code as it is not related to the issue.

EDIT: I also tried to display a static "Hello" in fpsNum.setString("hello"); and it did not display that either.


Solution

  • Did you tried to just put a plain text instead of your fps count ? You could try to check if another font would work, or just throw an error if the file does not load.

    if (!font.loadFromFile("fonts/KOMIKAP_")) {
        std::cout << "Error with the file" << std::endl;
    }
    

    If you have an error, just try adding a .ttf to the font ?