diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2022-12-13 19:35:48 +0100 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2022-12-13 19:35:48 +0100 |
commit | e45e92e0ff294e6a1899b01b8cfdd8ab25d8dcdc (patch) | |
tree | 6021345d94502db896ca0174abf270b51e89e306 /src/graphics/textureFont.cpp | |
parent | 70c0be5b2cfb3b9cfe86d5a6e6624e28fb845453 (diff) |
Make a better text rendering system and a simple example in titlestate
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_; } |