Skip to content

Instantly share code, notes, and snippets.

@MarioLiebisch
Last active July 16, 2018 08:09
Show Gist options
  • Save MarioLiebisch/e0de8f31816517fc15798467568b5102 to your computer and use it in GitHub Desktop.
Save MarioLiebisch/e0de8f31816517fc15798467568b5102 to your computer and use it in GitHub Desktop.
A simple to use, self-containing FPS meter for SFML programs I use for testing
/*
Usage:
* Load a font, if you don't have one yet
* Create the `FpsMeter` object
* Optional: Set a position
* Draw the meter
sf::Font font;
font.loadFromFile("myfont.ttf");
FpsMeter fps(font);
fps.setPosition(20, 10);
window.draw(fps);
*/
class FpsMeter : public sf::Drawable, public sf::Transformable {
private:
mutable sf::Clock mTimer;
mutable unsigned int mCounter;
mutable sf::Text mText;
public:
FpsMeter(const sf::Font &font) : mCounter(0), mText("", font, 20) {
mText.setFillColor(sf::Color::White);
}
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const {
++mCounter;
if (mTimer.getElapsedTime() > sf::seconds(1)) {
mTimer.restart();
std::stringstream str;
str << mCounter;
mText.setString(str.str());
mCounter = 0;
}
states.transform *= getTransform();
target.draw(mText, states);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment