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
62
63
|
#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_;
float lineHeightRatio_;
unsigned int size_, maxWidth_, maxHeight_;
void arrange(Font &font); // Specialize for sf::Font and TextureFont
public:
Text(Font &font, const std::string &text, float lineHeightRatio=1.,
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, float lineHeightRatio,
unsigned int maxWidth, unsigned int maxHeight)
: font_(font)
, text_(text)
, vertices_(sf::Quads, text.size()*4)
, lineHeightRatio_(lineHeightRatio)
, 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
|