Text.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef SMK_TEXT_HPP
6#define SMK_TEXT_HPP
7
8#include <smk/Texture.hpp>
9#include <smk/Transformable.hpp>
10#include <string>
11
12namespace smk {
13class Font;
14
15/// @example text.cpp
16
17/// @brief A class for displaying text.
18///
19/// A Text uses the Font's glyphs and displays them to the screen. A Text is a
20/// Transformable object, so you can move/rotate/scale/colorize it.
21///
22/// Example:
23/// -------
24///
25/// ~~~cpp
26/// auto font = smk::Font("./arial.ttf", 32);
27///
28/// [...]
29///
30/// auto text = smk::Text(font, "The SMK library can draw text");
31///
32/// text.SetPosition(300,300);
33/// text.SetColor(smk::Color::White);
34///
35/// window.Draw(text);
36/// ~~~
37class Text : public Transformable {
38 public:
40 Text(Font& font);
41 Text(Font& font, const std::string& text);
42 Text(Font& font, const std::wstring& text);
43 virtual ~Text() = default;
44
45 void SetString(const std::wstring& wide_string);
46 void SetString(const std::string& string);
47 void SetFont(Font& font);
48
49 void Draw(RenderTarget& target, RenderState state) const override;
50
51 glm::vec2 ComputeDimensions() const;
52
53 Text(Text&&) = default;
54 Text(const Text&) = default;
55 Text& operator=(Text&&) noexcept = default;
56 Text& operator=(const Text&) = default;
57
58 public:
59 Font* font_ = nullptr;
60 std::wstring string_;
61};
62
63} // namespace smk
64
65#endif /* end of include guard: SMK_TEXT_HPP */
A class for displaying text.
Definition: Text.hpp:37
glm::vec2 ComputeDimensions() const
Definition: Text.cpp:207
void SetFont(Font &font)
Update the Font to be used.
Definition: Text.cpp:155
void Draw(RenderTarget &target, RenderState state) const override
Draw the Text to the screen.
Definition: Text.cpp:160
Text()
Construct a null Text. It can't be drawn.
void SetString(const std::wstring &wide_string)
Update the text to be drawn.
Definition: Text.cpp:145
Contain all the data needed to draw.
Definition: RenderState.hpp:17