- 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.
- 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;
}
- 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.