From d0ee939a32fc7cec65a77d028d72881ee8ea1495 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sat, 10 Dec 2022 20:42:27 +0100 Subject: Move font support and clean --- src/graphics/font.cpp | 86 -------------------------------------------- src/graphics/font.h | 35 ------------------ src/graphics/textureFont.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++ src/graphics/textureFont.h | 38 ++++++++++++++++++++ 4 files changed, 123 insertions(+), 121 deletions(-) delete mode 100644 src/graphics/font.cpp delete mode 100644 src/graphics/font.h create mode 100644 src/graphics/textureFont.cpp create mode 100644 src/graphics/textureFont.h (limited to 'src') diff --git a/src/graphics/font.cpp b/src/graphics/font.cpp deleted file mode 100644 index 42176b2..0000000 --- a/src/graphics/font.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "font.h" - -using namespace Graphics; - -// Font - -Font::Font(const sf::Texture &texture, const std::string &mapping, - unsigned int numrows, unsigned int numcols) - : tex_(texture) - , mapping_(mapping) - , numrows_(numrows) - , numcols_(numcols) -{ - sf::Vector2u totalSize = tex_.getSize(); - sf::Vector2f totalSize_f (totalSize.x, totalSize.y); - glyphSize_ = sf::Vector2f( totalSize.x / numcols_, totalSize.y / numrows_ ); -} - -std::array Font::getGlyphMapping(const char ch){ - unsigned int pos = static_cast( 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_; - - sf::Vector2f basePos(x*glyphSize_.x, y*glyphSize_.y); - - // 1 --- 2 - // | | - // 4 --- 3 - return std::array { - basePos, - basePos + sf::Vector2f( glyphSize_.x, 0 ), - basePos + glyphSize_, - basePos + sf::Vector2f( 0, glyphSize_.y ), - }; -} - -sf::Vector2f Font::getGlyphSize(){ - return glyphSize_; -} - -const sf::Texture * Font::getTexture(){ - return &tex_; -} - - - - -// Text - -Text::Text(Font &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 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); -} diff --git a/src/graphics/font.h b/src/graphics/font.h deleted file mode 100644 index 9a8e919..0000000 --- a/src/graphics/font.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef GRAPHICS_FONT_H -#define GRAPHICS_FONT_H - -#include -#include -#include"SFML/Graphics.hpp" - -namespace Graphics { - class Font { - private: - const sf::Texture &tex_; - const std::string mapping_; - const unsigned int numrows_, numcols_; - sf::Vector2f glyphSize_; - public: - Font(const sf::Texture &texture, const std::string &mapping, - unsigned int numrows, unsigned int numcols); - std::array getGlyphMapping(const char ch); - sf::Vector2f getGlyphSize(); - const sf::Texture *getTexture(); - }; - - class Text : public sf::Drawable, public sf::Transformable { - private: - Font &font_; - const std::string text_; - sf::VertexArray vertices_; - public: - Text(Font &font, const std::string &text); - virtual void draw(sf::RenderTarget& target, - sf::RenderStates states) const override; - }; -} - -#endif // GRAPHICS_FONT_H diff --git a/src/graphics/textureFont.cpp b/src/graphics/textureFont.cpp new file mode 100644 index 0000000..a95c71c --- /dev/null +++ b/src/graphics/textureFont.cpp @@ -0,0 +1,85 @@ +#include "textureFont.h" + +using namespace Graphics; + +// TextureFont + +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 TextureFont::getGlyphMapping(const char ch){ + unsigned int pos = static_cast( 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 { + 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_; +} + + + + +// Text + +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 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); +} diff --git a/src/graphics/textureFont.h b/src/graphics/textureFont.h new file mode 100644 index 0000000..aa89e50 --- /dev/null +++ b/src/graphics/textureFont.h @@ -0,0 +1,38 @@ +#ifndef GRAPHICS_FONT_H +#define GRAPHICS_FONT_H + +#include +#include +#include"SFML/Graphics.hpp" + +namespace Graphics { + class TextureFont { + public: + struct Descr { + std::string mapping; + unsigned int numrows, numcols; + }; + private: + const sf::Texture &tex_; + const Descr description_; + sf::Vector2f glyphSize_; + public: + TextureFont(const sf::Texture &texture, Descr desc); + std::array getGlyphMapping(const char ch); + sf::Vector2f getGlyphSize(); + const sf::Texture *getTexture(); + }; + + class Text : public sf::Drawable, public sf::Transformable { + private: + TextureFont &font_; + const std::string text_; + sf::VertexArray vertices_; + public: + Text(TextureFont &font, const std::string &text); + virtual void draw(sf::RenderTarget& target, + sf::RenderStates states) const override; + }; +} + +#endif // GRAPHICS_FONT_H -- cgit v1.2.3