summaryrefslogtreecommitdiff
path: root/src/graphics/text.h
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2022-12-13 19:35:48 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2022-12-13 19:35:48 +0100
commite45e92e0ff294e6a1899b01b8cfdd8ab25d8dcdc (patch)
tree6021345d94502db896ca0174abf270b51e89e306 /src/graphics/text.h
parent70c0be5b2cfb3b9cfe86d5a6e6624e28fb845453 (diff)
Make a better text rendering system and a simple example in titlestate
Diffstat (limited to 'src/graphics/text.h')
-rw-r--r--src/graphics/text.h48
1 files changed, 46 insertions, 2 deletions
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<typename Font>
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 <typename Font>
+ Text<Font>::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 <typename Font>
+ void Text<Font>::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 <typename Font>
+ void Text<Font>::setFont(Font &font){
+ font_ = font;
+ arrange(font);
+ }
}
+
+
#endif // GRAPHICS_TEXT_H