-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUTText.cpp
More file actions
69 lines (52 loc) · 1.41 KB
/
UTText.cpp
File metadata and controls
69 lines (52 loc) · 1.41 KB
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
64
65
66
67
68
69
#include "UTText.h"
UTText::~UTText() {
for (auto effect : m_effects)
delete effect;
}
void UTText::resetEffects() { for (auto effect : m_effects) effect->reset(); }
void UTText::addEffect(TextEffect* effect) {
m_effects.push_back(effect);
}
UTText::UTText() {
}
UTText::UTText(const sf::Font& font, const sf::String& string) : m_font(&font), m_text(string) {
}
void UTText::setFont(const sf::Font& font) {
m_font = &font;
}
void UTText::setString(const sf::String& string) {
m_text = string;
resetEffects();
}
void UTText::setCharacterSize(unsigned size) {
m_size = size;
}
void UTText::draw(sf::RenderTarget& target, sf::RenderStates states) const {
float advance = 0;
unsigned int line = 0;
for (unsigned int i = 0; i < m_text.getSize(); i++) {
sf::Sprite spr;
auto character = m_text[i];
if (character == '\n') {
advance = 0;
line++;
if (m_text[i+1] == '\r')
i++;
continue;
}
const sf::Glyph& glyph = m_font->getGlyph(character, m_size, false);
spr.setTexture(m_font->getTexture(m_size));
spr.setTextureRect(glyph.textureRect);
//Calculate character position
sf::Vector2f pos(getPosition());
pos.x += advance;
pos.y += (line + 1) * m_size + glyph.bounds.top;
sf::Color sprColor = spr.getColor();
for (auto effect: m_effects)
effect->apply(pos, i, sprColor);
spr.setColor(sprColor);
spr.setPosition(pos);
target.draw(spr);
advance += glyph.advance;
}
}