summaryrefslogtreecommitdiff
path: root/src/graphics/text.h
blob: f60980cba570d3583085ef4f3cf5e157789a6c3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef GRAPHICS_TEXT_H
#define GRAPHICS_TEXT_H
#include"textureFont.h"

namespace Graphics {

    template<typename Font>
    class Text : public sf::Drawable, public sf::Transformable  {
        private:
            Font &font_;
            const std::string text_;
            sf::VertexArray vertices_;
            unsigned int size_, maxWidth_, maxHeight_;

            void arrange(Font &font); // Specialize for sf::Font and TextureFont

        public:
            Text(Font &font, const std::string &text, unsigned int maxWidth=0,
                    unsigned int maxHeight=0);

            void setFont(Font &font);
            void setText(const std::string &text);

            virtual void draw(sf::RenderTarget& target,
                    sf::RenderStates states) const override;
    };

    template <typename Font>
    Text<Font>::Text(Font &font, const std::string &text,
            unsigned int maxWidth, unsigned int maxHeight)
        : font_(font)
        , text_(text)
        , vertices_(sf::Quads, text.size()*4)
        , size_(0)
        , maxWidth_(maxWidth)
        , maxHeight_(maxHeight)
    {
        arrange(font);
    }


    template <typename Font>
    void Text<Font>::draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // TODO: only repaint if needed: when repainting is needed
        // vertices_.clear() and fun
        states.transform *= getTransform();
        states.texture = font_.getTexture(size_);
        // you may also override states.shader or states.blendMode if you want
        target.draw(vertices_, states);
    }

    template <typename Font>
    void Text<Font>::setFont(Font &font){
        font_ = font;
        arrange(font);
    }
}


#endif // GRAPHICS_TEXT_H