summaryrefslogtreecommitdiff
path: root/src/graphics/textureFont.cpp
blob: 29f9c7109afe00525aac16411c5b3734727b3193 (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
#include "textureFont.h"

using namespace Graphics;

TextureFont::TextureFont(const sf::Texture &texture,
        TextureFont::Descr description)
    : tex_(texture)
    , description_(description)
{
    sf::Vector2u totalSize = tex_.getSize();
    sf::Vector2f totalSize_f (totalSize.x, totalSize.y);
    glyphSize_ = sf::Vector2f( totalSize.x / description_.numcols,
                               totalSize.y / description_.numrows );
}

std::array<sf::Vector2f, 4> TextureFont::getGlyphMapping(const char ch){
    unsigned int pos = static_cast<unsigned int>( description_.mapping.find_first_of(ch));
    if (pos == std::string::npos){
        // TODO: Error handling!
        pos = 0;
    }
    unsigned int x = pos % description_.numcols;
    unsigned int y = (pos - x) / description_.numcols;

    sf::Vector2f basePos(x*glyphSize_.x, y*glyphSize_.y);

    // 1 --- 2
    // |     |
    // 4 --- 3
    return std::array<sf::Vector2f,4> {
        basePos,
        basePos + sf::Vector2f( glyphSize_.x, 0 ),
        basePos + glyphSize_,
        basePos + sf::Vector2f( 0, glyphSize_.y ),
    };
}

sf::Vector2f TextureFont::getGlyphSize(){
    return glyphSize_;
}

const sf::Texture * TextureFont::getTexture(){
    return &tex_;
}