diff options
Diffstat (limited to 'src/graphics/textureFont.cpp')
-rw-r--r-- | src/graphics/textureFont.cpp | 48 |
1 files changed, 23 insertions, 25 deletions
diff --git a/src/graphics/textureFont.cpp b/src/graphics/textureFont.cpp index 29f9c71..3be2444 100644 --- a/src/graphics/textureFont.cpp +++ b/src/graphics/textureFont.cpp @@ -8,37 +8,35 @@ TextureFont::TextureFont(const sf::Texture &texture, , description_(description) { sf::Vector2u totalSize = tex_.getSize(); - sf::Vector2f totalSize_f (totalSize.x, totalSize.y); - glyphSize_ = sf::Vector2f( totalSize.x / description_.numcols, + glyphSize_ = sf::Vector2i( 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; + for(unsigned int i=0; i<description.mapping.size(); i++){ + sf::Glyph glyph; + glyph.advance = glyphSize_.x; + + unsigned int x = i % description_.numcols; + unsigned int y = (i - x) / description_.numcols; + + glyph.textureRect = sf::IntRect(x*glyphSize_.x, + y*glyphSize_.y, + glyphSize_.x, + glyphSize_.y); + + glyph.bounds = sf::FloatRect(0, + 0, + glyphSize_.x, + glyphSize_.y); + + glyphTable_.insert(std::make_pair(description.mapping[i], glyph)); } - 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::Glyph & TextureFont::getGlyph(char ch) const{ + auto pair = glyphTable_.find(ch); + return pair->second; } -const sf::Texture * TextureFont::getTexture(){ +const sf::Texture * TextureFont::getTexture(unsigned int _){ return &tex_; } |