summaryrefslogtreecommitdiff
path: root/src/graphics/text.cpp
blob: 82b881189fd02a7a1111a017c6835b963088a60c (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
#include "text.h"

using namespace Graphics;

Text::Text(TextureFont &font, const std::string &text)
    : font_(font)
    , text_(text)
    , vertices_(sf::Quads, text.size()*4)
{
    unsigned int length = vertices_.getVertexCount();
    sf::Vector2f glyphSize = font_.getGlyphSize();
    // TODO: this is the simplest rendering way: everything in one line
    //       we should improve this to make it handle newlines or max witdths
    for(unsigned int i=0; i < length; i=i+4){
        vertices_[i+0].position = sf::Vector2f(glyphSize.x * i/4,       0);
        vertices_[i+1].position = sf::Vector2f(glyphSize.x * (i/4 + 1), 0);
        vertices_[i+2].position = sf::Vector2f(glyphSize.x * (i/4 + 1), glyphSize.y);
        vertices_[i+3].position = sf::Vector2f(glyphSize.x * i/4,       glyphSize.y);

        std::array<sf::Vector2f, 4> glyph = font_.getGlyphMapping(text_[i/4]);

        vertices_[i+0].texCoords = glyph[0];
        vertices_[i+1].texCoords = glyph[1];
        vertices_[i+2].texCoords = glyph[2];
        vertices_[i+3].texCoords = glyph[3];
    }
}

void Text::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();
    // you may also override states.shader or states.blendMode if you want
    target.draw(vertices_, states);
}