diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2022-12-13 13:29:06 +0100 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2022-12-13 13:29:06 +0100 |
commit | 70c0be5b2cfb3b9cfe86d5a6e6624e28fb845453 (patch) | |
tree | 862b3a2c38c35e0c6c739c96cb9772eb784c622a /src/graphics | |
parent | 82be0bb0f9e19ea578858d6afff96ef8966424bc (diff) |
Move text to a separate file
Diffstat (limited to 'src/graphics')
-rw-r--r-- | src/graphics/text.cpp | 37 | ||||
-rw-r--r-- | src/graphics/text.h | 17 | ||||
-rw-r--r-- | src/graphics/textureFont.cpp | 41 | ||||
-rw-r--r-- | src/graphics/textureFont.h | 12 |
4 files changed, 55 insertions, 52 deletions
diff --git a/src/graphics/text.cpp b/src/graphics/text.cpp new file mode 100644 index 0000000..82b8811 --- /dev/null +++ b/src/graphics/text.cpp @@ -0,0 +1,37 @@ +#include "text.h" + +using namespace Graphics; + +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<sf::Vector2f, 4> 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/text.h b/src/graphics/text.h new file mode 100644 index 0000000..22179f5 --- /dev/null +++ b/src/graphics/text.h @@ -0,0 +1,17 @@ +#ifndef GRAPHICS_TEXT_H +#define GRAPHICS_TEXT_H +#include"textureFont.h" + +namespace Graphics { + 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_TEXT_H diff --git a/src/graphics/textureFont.cpp b/src/graphics/textureFont.cpp index a95c71c..29f9c71 100644 --- a/src/graphics/textureFont.cpp +++ b/src/graphics/textureFont.cpp @@ -2,8 +2,6 @@ using namespace Graphics; -// TextureFont - TextureFont::TextureFont(const sf::Texture &texture, TextureFont::Descr description) : tex_(texture) @@ -44,42 +42,3 @@ sf::Vector2f TextureFont::getGlyphSize(){ 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<sf::Vector2f, 4> 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 index aa89e50..5e78f9e 100644 --- a/src/graphics/textureFont.h +++ b/src/graphics/textureFont.h @@ -3,7 +3,7 @@ #include<string> #include<array> -#include"SFML/Graphics.hpp" +#include<SFML/Graphics.hpp> namespace Graphics { class TextureFont { @@ -23,16 +23,6 @@ namespace Graphics { 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 |