Home Projects Links About RSS

SFGUI Frame counter

A frame counter is a simple and handy tool to get a rough idea of the performance of a game or other graphical application. A slightly inconvenient part of using one when developing with SFML is the need to load and store a font. If, however the SFGUI library is also used, that requirement can be side-stepped by making the frame counter a widget, i.e. by inhering from sfg::Label:

class FrameCounter : public sfg::Label
    typedef std::shared_ptr<FrameCounter> Ptr;
    typedef std::shared_ptr<const FrameCounter> PtrConst;
    static Ptr Create(){
        Ptr frameCounter(new FrameCounter());
        return frameCounter;

    FrameCounter() = default;

    virtual void HandleUpdate(float seconds) override{
        m_timeElapsed += seconds;
        m_frameCount += 1;
        if (m_timeElapsed >= 1.f){
            SetText("FPS: " + std::to_string(m_frameCount));
            m_timeElapsed -= 1.f;
            m_frameCount = 0;

    float m_timeElapsed;
    int m_frameCount;

As a side bonus, the counter will be updated through SFGUI’s mechanisms, rather than requiring a separate update function and time. Then it is sufficient to add the widget to any point of SFGUI’s hierarchy: