From e45e92e0ff294e6a1899b01b8cfdd8ab25d8dcdc Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Tue, 13 Dec 2022 19:35:48 +0100 Subject: Make a better text rendering system and a simple example in titlestate --- src/graphics/text.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'src/graphics/text.h') diff --git a/src/graphics/text.h b/src/graphics/text.h index 22179f5..f60980c 100644 --- a/src/graphics/text.h +++ b/src/graphics/text.h @@ -3,15 +3,59 @@ #include"textureFont.h" namespace Graphics { + + template class Text : public sf::Drawable, public sf::Transformable { private: - TextureFont &font_; + Font &font_; const std::string text_; sf::VertexArray vertices_; + unsigned int size_, maxWidth_, maxHeight_; + + void arrange(Font &font); // Specialize for sf::Font and TextureFont + public: - Text(TextureFont &font, const std::string &text); + Text(Font &font, const std::string &text, unsigned int maxWidth=0, + unsigned int maxHeight=0); + + void setFont(Font &font); + void setText(const std::string &text); + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; }; + + template + Text::Text(Font &font, const std::string &text, + unsigned int maxWidth, unsigned int maxHeight) + : font_(font) + , text_(text) + , vertices_(sf::Quads, text.size()*4) + , size_(0) + , maxWidth_(maxWidth) + , maxHeight_(maxHeight) + { + arrange(font); + } + + + template + 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(size_); + // you may also override states.shader or states.blendMode if you want + target.draw(vertices_, states); + } + + template + void Text::setFont(Font &font){ + font_ = font; + arrange(font); + } } + + #endif // GRAPHICS_TEXT_H -- cgit v1.2.3