galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Text.cpp
Go to the documentation of this file.
1
7
8#include <glm/gtc/type_ptr.hpp>
9#include <msdfgl.h>
10#include <nlohmann/json.hpp>
11
12#include "galaxy/core/ServiceLocator.hpp"
15
16#include "Text.hpp"
17
18namespace galaxy
19{
20 namespace graphics
21 {
23 : m_font {nullptr}
24 , m_size {0.0f}
25 , m_alignment {Alignment::LEFT}
26 {
27 auto indicies = graphics::gen_default_indices();
28 m_vao.buffer(4, indicies);
29
30 m_rt.create(1, 1);
31 }
32
34 {
35 this->m_colour = std::move(t.m_colour);
36 this->m_vao = std::move(t.m_vao);
37 this->m_rt = std::move(t.m_rt);
38 this->m_font_name = t.m_font_name;
39 this->m_font = t.m_font;
40 this->m_text = std::move(t.m_text);
41 this->m_size = t.m_size;
42 this->m_alignment = t.m_alignment;
43
44 t.m_font = nullptr;
45 }
46
48 {
49 if (this != &t)
50 {
51 this->m_vao = std::move(t.m_vao);
52 this->m_colour = std::move(t.m_colour);
53 this->m_rt = std::move(t.m_rt);
54 this->m_font_name = t.m_font_name;
55 this->m_font = t.m_font;
56 this->m_text = std::move(t.m_text);
57 this->m_size = t.m_size;
58 this->m_alignment = t.m_alignment;
59
60 t.m_font = nullptr;
61 }
62
63 return *this;
64 }
65
67 {
68 m_font = nullptr;
69 }
70
71 void Text::create(std::string_view text, const float size, const std::string& font, const graphics::Colour& colour, Alignment alignment)
72 {
73 m_colour = colour;
74 m_font_name = font;
75 m_text = text;
76 m_size = size;
77 m_alignment = alignment;
78
79 strutils::replace_all(m_text, "\t", " "); // Handle tabs.
80
81 update();
82 }
83
84 void Text::set_font(std::string_view font)
85 {
86 m_font_name = font;
87 }
88
90 {
91 if (!m_font_name.empty())
92 {
93 auto& fonts = core::ServiceLocator<resource::Fonts>::ref();
94 m_font = fonts.get(m_font_name);
95
96 const auto vec = m_font->get_text_size(m_text, m_size);
97 const auto width = vec.x;
98 const auto height = vec.y;
99
100 if (width > m_rt.width() || height > m_rt.height())
101 {
102 m_rt.recreate(static_cast<int>(width), static_cast<int>(height));
103 }
104
105 m_rt.bind();
106
107 std::size_t start = 0;
108 std::size_t end = m_text.find('\n');
109
110 auto proj = glm::ortho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
111 auto y_off = m_font->vertical_advance(m_size);
112
113 while (end != std::string::npos)
114 {
115 const auto block = m_text.substr(start, end);
116 msdfgl_printf(
117 0,
118 y_off,
119 static_cast<int>(m_alignment),
120 m_font->handle(),
121 m_size,
122 0xffffffff,
123 glm::value_ptr(proj),
124 static_cast<msdfgl_printf_flags>(MSDFGL_UTF8 | MSDFGL_KERNING),
125 block.c_str()
126 );
127
128 y_off += m_font->vertical_advance(m_size);
129 start = end + 1;
130 end = m_text.find('\n', start);
131 }
132
133 const auto last_block = m_text.substr(start, end);
134
135 msdfgl_printf(
136 0,
137 y_off,
138 static_cast<int>(m_alignment),
139 m_font->handle(),
140 m_size,
141 0xffffffff,
142 glm::value_ptr(proj),
143 static_cast<msdfgl_printf_flags>(MSDFGL_UTF8 | MSDFGL_KERNING),
144 last_block.c_str()
145 );
146
147 m_rt.unbind();
148
151 }
152 else
153 {
154 GALAXY_LOG(GALAXY_ERROR, "Attempted to create text without a font.");
155 }
156 }
157
158 void Text::update(std::string_view text)
159 {
160 m_text = text;
161 strutils::replace_all(m_text, "\t", " "); // Handle tabs.
162
163 update();
164 }
165
166 void Text::update(const float size)
167 {
168 m_size = size;
169
170 update();
171 }
172
173 void Text::update(Alignment alignment)
174 {
175 m_alignment = alignment;
176
177 update();
178 }
179
180 float Text::width() const
181 {
182 return static_cast<float>(m_rt.width());
183 }
184
185 float Text::height() const
186 {
187 return static_cast<float>(m_rt.height());
188 }
189
190 const std::string& Text::get_text() const
191 {
192 return m_text;
193 }
194
195 float Text::get_size() const
196 {
197 return m_size;
198 }
199
201 {
202 return m_alignment;
203 }
204
205 const std::string& Text::get_font() const
206 {
207 return m_font_name;
208 }
209
211 {
212 return m_vao;
213 }
214
219 } // namespace graphics
220} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_ERROR
Definition Log.hpp:24
thread_local const float vertices[]
Video.cpp galaxy.
Definition Video.cpp:19
Represents an RGBA colour.
Definition Colour.hpp:27
msdfgl_font_t handle()
Get handle to internal font object.
Definition Font.cpp:170
msdfgl_font_t m_font
Font object.
Definition Font.hpp:110
glm::vec2 get_text_size(const std::string &text, const float size)
Query text size with font.
Definition Font.cpp:130
float vertical_advance(const float size) const
Get vertical advance for drawing multiline text.
Definition Font.cpp:125
Draw to an opengl texture instead of the screen.
int height() const
Get texture height.
void unbind()
Deactivate context.
void create(const int width, const int height)
Create framebuffer and texture.
void recreate(const int width=-1, const int height=-1)
Destroy and re-create framebuffer.
void bind(bool clear=true)
Activate context.
int width() const
Get texture width.
String of glyphs rendered with a font.
Definition Text.hpp:23
const std::string & get_font() const
Get font ID.
Definition Text.cpp:205
const std::string & get_text() const
Get text.
Definition Text.cpp:190
Alignment get_alignment() const
Get the text alignment.
Definition Text.cpp:200
void create(std::string_view text, const float size, const std::string &font, const graphics::Colour &colour, Alignment alignment=Alignment::LEFT)
Creates the rendered text.
Definition Text.cpp:71
float m_size
EM size.
Definition Text.hpp:215
Text & operator=(Text &&)
Move assignment operator.
Definition Text.cpp:47
Alignment
Text alignment.
Definition Text.hpp:29
~Text()
Destructor.
Definition Text.cpp:66
std::string m_font_name
Font Name.
Definition Text.hpp:200
void set_font(std::string_view font)
Update the font used.
Definition Text.cpp:84
graphics::Colour m_colour
Text colour.
Definition Text.hpp:184
graphics::RenderTexture & render_texture()
Get render texture.
Definition Text.cpp:215
graphics::Font * m_font
Pointer to font resource.
Definition Text.hpp:205
float height() const
Get text height.
Definition Text.cpp:185
graphics::VertexArray m_vao
Vertex Array Object.
Definition Text.hpp:190
Alignment m_alignment
Alignment.
Definition Text.hpp:220
void update()
Update the rendered text.
Definition Text.cpp:89
std::string m_text
Text.
Definition Text.hpp:210
float get_size() const
Get text em size.
Definition Text.cpp:195
float width() const
Get text width.
Definition Text.cpp:180
Text()
Constructor.
Definition Text.cpp:22
graphics::VertexArray & vao()
Get vertex array.
Definition Text.cpp:210
graphics::RenderTexture m_rt
Font render texture.
Definition Text.hpp:195
Abstraction for OpenGL vertex array objects.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex array object.
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex array.
std::array< Vertex, 4 > gen_quad_vertices(const float width, const float height)
Generate some default verticies.
Definition Vertex.cpp:14
std::array< unsigned int, 6 > gen_default_indices()
Generate some default indicies.
Definition Vertex.cpp:37
Animated.cpp galaxy.
Definition Animated.cpp:16