summaryrefslogtreecommitdiff
path: root/src/graphics/text.h
diff options
context:
space:
mode:
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