diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2022-12-10 20:42:27 +0100 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2022-12-10 20:46:26 +0100 |
commit | d0ee939a32fc7cec65a77d028d72881ee8ea1495 (patch) | |
tree | dc6983005b145c6be2dbcde5268544adee4fcba7 /src | |
parent | 2b6cd04c1fcf9807b6da9aa03e9920c28780db2c (diff) |
Move font support and clean
Diffstat (limited to 'src')
-rw-r--r-- | src/graphics/textureFont.cpp (renamed from src/graphics/font.cpp) | 29 | ||||
-rw-r--r-- | src/graphics/textureFont.h (renamed from src/graphics/font.h) | 17 |
2 files changed, 24 insertions, 22 deletions
diff --git a/src/graphics/font.cpp b/src/graphics/textureFont.cpp index 42176b2..a95c71c 100644 --- a/src/graphics/font.cpp +++ b/src/graphics/textureFont.cpp @@ -1,29 +1,28 @@ -#include "font.h" +#include "textureFont.h" using namespace Graphics; -// Font +// TextureFont -Font::Font(const sf::Texture &texture, const std::string &mapping, - unsigned int numrows, unsigned int numcols) +TextureFont::TextureFont(const sf::Texture &texture, + TextureFont::Descr description) : tex_(texture) - , mapping_(mapping) - , numrows_(numrows) - , numcols_(numcols) + , description_(description) { sf::Vector2u totalSize = tex_.getSize(); sf::Vector2f totalSize_f (totalSize.x, totalSize.y); - glyphSize_ = sf::Vector2f( totalSize.x / numcols_, totalSize.y / numrows_ ); + glyphSize_ = sf::Vector2f( totalSize.x / description_.numcols, + totalSize.y / description_.numrows ); } -std::array<sf::Vector2f, 4> Font::getGlyphMapping(const char ch){ - unsigned int pos = static_cast<unsigned int>( mapping_.find_first_of(ch)); +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 % numcols_; - unsigned int y = (pos - x) / numcols_; + unsigned int x = pos % description_.numcols; + unsigned int y = (pos - x) / description_.numcols; sf::Vector2f basePos(x*glyphSize_.x, y*glyphSize_.y); @@ -38,11 +37,11 @@ std::array<sf::Vector2f, 4> Font::getGlyphMapping(const char ch){ }; } -sf::Vector2f Font::getGlyphSize(){ +sf::Vector2f TextureFont::getGlyphSize(){ return glyphSize_; } -const sf::Texture * Font::getTexture(){ +const sf::Texture * TextureFont::getTexture(){ return &tex_; } @@ -51,7 +50,7 @@ const sf::Texture * Font::getTexture(){ // Text -Text::Text(Font &font, const std::string &text) +Text::Text(TextureFont &font, const std::string &text) : font_(font) , text_(text) , vertices_(sf::Quads, text.size()*4) diff --git a/src/graphics/font.h b/src/graphics/textureFont.h index 9a8e919..aa89e50 100644 --- a/src/graphics/font.h +++ b/src/graphics/textureFont.h @@ -6,15 +6,18 @@ #include"SFML/Graphics.hpp" namespace Graphics { - class Font { + class TextureFont { + public: + struct Descr { + std::string mapping; + unsigned int numrows, numcols; + }; private: const sf::Texture &tex_; - const std::string mapping_; - const unsigned int numrows_, numcols_; + const Descr description_; sf::Vector2f glyphSize_; public: - Font(const sf::Texture &texture, const std::string &mapping, - unsigned int numrows, unsigned int numcols); + TextureFont(const sf::Texture &texture, Descr desc); std::array<sf::Vector2f,4> getGlyphMapping(const char ch); sf::Vector2f getGlyphSize(); const sf::Texture *getTexture(); @@ -22,11 +25,11 @@ namespace Graphics { class Text : public sf::Drawable, public sf::Transformable { private: - Font &font_; + TextureFont &font_; const std::string text_; sf::VertexArray vertices_; public: - Text(Font &font, const std::string &text); + Text(TextureFont &font, const std::string &text); virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; }; |