aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-03-17 13:04:20 +0100
committerdec05eba <dec05eba@protonmail.com>2021-03-17 13:04:20 +0100
commita3d12de5f6adf7882b885fb1fcbc22c6a2763242 (patch)
tree016e0b013168e407febc679ad63405eab9f3b36e /src
parente0ba22f713415316a07edc818e443c0d8cb892f7 (diff)
Use triangles instead of quads for text
Diffstat (limited to 'src')
-rw-r--r--src/Text.cpp110
1 files changed, 61 insertions, 49 deletions
diff --git a/src/Text.cpp b/src/Text.cpp
index 46e09de..ca59146 100644
--- a/src/Text.cpp
+++ b/src/Text.cpp
@@ -45,9 +45,9 @@ namespace QuickMedia
caretIndex(0),
caret_offset_x(0.0f)
{
- vertices[0].setPrimitiveType(sf::PrimitiveType::Quads);
- vertices[1].setPrimitiveType(sf::PrimitiveType::Quads);
- vertices[2].setPrimitiveType(sf::PrimitiveType::Quads);
+ vertices[0].setPrimitiveType(sf::PrimitiveType::Triangles);
+ vertices[1].setPrimitiveType(sf::PrimitiveType::Triangles);
+ vertices[2].setPrimitiveType(sf::PrimitiveType::Triangles);
setString(std::move(_str));
}
@@ -267,11 +267,11 @@ namespace QuickMedia
}
float Text::get_text_quad_left_side(const VertexRef &vertex_ref) const {
- return vertices[vertex_ref.vertices_index][vertex_ref.index].position.x;
+ return vertices[vertex_ref.vertices_index][vertex_ref.index + 1].position.x;
}
float Text::get_text_quad_right_side(const VertexRef &vertex_ref) const {
- return vertices[vertex_ref.vertices_index][vertex_ref.index + 1].position.x;
+ return vertices[vertex_ref.vertices_index][vertex_ref.index + 5].position.x;
}
float Text::get_caret_offset_by_caret_index(int index) const {
@@ -375,10 +375,12 @@ namespace QuickMedia
sf::Vector2f textureBottomLeft(emoji_rec.x, emoji_rec.y + emoji_rec.height);
sf::Vector2f textureBottomRight(emoji_rec.x + emoji_rec.width, emoji_rec.y + emoji_rec.height);
- vertices[vertices_index].append({ vertexTopLeft, sf::Color::White, textureTopLeft });
vertices[vertices_index].append({ vertexTopRight, sf::Color::White, textureTopRight });
- vertices[vertices_index].append({ vertexBottomRight, sf::Color::White, textureBottomRight });
+ vertices[vertices_index].append({ vertexTopLeft, sf::Color::White, textureTopLeft });
+ vertices[vertices_index].append({ vertexBottomLeft, sf::Color::White, textureBottomLeft });
vertices[vertices_index].append({ vertexBottomLeft, sf::Color::White, textureBottomLeft });
+ vertices[vertices_index].append({ vertexBottomRight, sf::Color::White, textureBottomRight });
+ vertices[vertices_index].append({ vertexTopRight, sf::Color::White, textureTopRight });
glyphPos.x += emoji_rec.width + characterSpacing;
vertices_linear.push_back({vertices_index, vertexStart, 0, codePoint});
@@ -403,10 +405,18 @@ namespace QuickMedia
{
case ' ':
{
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x, glyphPos.y - vspace), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x + hspace, glyphPos.y - vspace), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x + hspace, glyphPos.y), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x, glyphPos.y), sf::Color::Transparent, sf::Vector2f() });
+ sf::Vector2f vertexTopLeft(glyphPos.x, glyphPos.y - vspace);
+ sf::Vector2f vertexTopRight(glyphPos.x + hspace, glyphPos.y - vspace);
+ sf::Vector2f vertexBottomLeft(glyphPos.x, glyphPos.y);
+ sf::Vector2f vertexBottomRight(glyphPos.x + hspace, glyphPos.y);
+
+ vertices[vertices_index].append({ vertexTopRight, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexTopLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomRight, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexTopRight, sf::Color::Transparent, sf::Vector2f() });
+
glyphPos.x += hspace;
vertices_linear.push_back({vertices_index, vertexStart, 0, codePoint});
continue;
@@ -414,20 +424,36 @@ namespace QuickMedia
case '\t':
{
const float char_width = hspace * TAB_WIDTH;
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x, glyphPos.y - vspace), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x + char_width, glyphPos.y - vspace), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x + char_width, glyphPos.y), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x, glyphPos.y), sf::Color::Transparent, sf::Vector2f() });
+ sf::Vector2f vertexTopLeft(glyphPos.x, glyphPos.y - vspace);
+ sf::Vector2f vertexTopRight(glyphPos.x + char_width, glyphPos.y - vspace);
+ sf::Vector2f vertexBottomLeft(glyphPos.x, glyphPos.y);
+ sf::Vector2f vertexBottomRight(glyphPos.x + char_width, glyphPos.y);
+
+ vertices[vertices_index].append({ vertexTopRight, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexTopLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomRight, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexTopRight, sf::Color::Transparent, sf::Vector2f() });
+
glyphPos.x += char_width;
vertices_linear.push_back({vertices_index, vertexStart, 0, codePoint});
continue;
}
case '\n':
{
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x, glyphPos.y - vspace), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x, glyphPos.y - vspace), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x, glyphPos.y), sf::Color::Transparent, sf::Vector2f() });
- vertices[vertices_index].append({ sf::Vector2f(glyphPos.x, glyphPos.y), sf::Color::Transparent, sf::Vector2f() });
+ sf::Vector2f vertexTopLeft(glyphPos.x, glyphPos.y - vspace);
+ sf::Vector2f vertexTopRight(glyphPos.x, glyphPos.y - vspace);
+ sf::Vector2f vertexBottomLeft(glyphPos.x, glyphPos.y);
+ sf::Vector2f vertexBottomRight(glyphPos.x, glyphPos.y);
+
+ vertices[vertices_index].append({ vertexTopRight, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexTopLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomLeft, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexBottomRight, sf::Color::Transparent, sf::Vector2f() });
+ vertices[vertices_index].append({ vertexTopRight, sf::Color::Transparent, sf::Vector2f() });
+
glyphPos.x = 0.0f;
glyphPos.y += floor(vspace + lineSpacing);
vertices_linear.push_back({vertices_index, vertexStart, 0, codePoint});
@@ -449,10 +475,12 @@ namespace QuickMedia
sf::Color fontColor = (textElement.type == TextElement::Type::TEXT ? color : urlColor);
- vertices[vertices_index].append({ vertexTopLeft, fontColor, textureTopLeft });
vertices[vertices_index].append({ vertexTopRight, fontColor, textureTopRight });
- vertices[vertices_index].append({ vertexBottomRight, fontColor, textureBottomRight });
+ vertices[vertices_index].append({ vertexTopLeft, fontColor, textureTopLeft });
vertices[vertices_index].append({ vertexBottomLeft, fontColor, textureBottomLeft });
+ vertices[vertices_index].append({ vertexBottomLeft, fontColor, textureBottomLeft });
+ vertices[vertices_index].append({ vertexBottomRight, fontColor, textureBottomRight });
+ vertices[vertices_index].append({ vertexTopRight, fontColor, textureTopRight });
glyphPos.x += glyph.advance + characterSpacing;
vertices_linear.push_back({vertices_index, vertexStart, 0, codePoint});
@@ -476,15 +504,10 @@ namespace QuickMedia
VertexRef &vertex_ref = vertices_linear[i];
sf::Vertex *vertex = &vertices[vertex_ref.vertices_index][vertex_ref.index];
- vertex[0].position.x -= text_wrap_offset;
- vertex[1].position.x -= text_wrap_offset;
- vertex[2].position.x -= text_wrap_offset;
- vertex[3].position.x -= text_wrap_offset;
-
- vertex[0].position.y += text_offset_y;
- vertex[1].position.y += text_offset_y;
- vertex[2].position.y += text_offset_y;
- vertex[3].position.y += text_offset_y;
+ for(int v = 0; v < 6; ++v) {
+ vertex[v].position.x -= text_wrap_offset;
+ vertex[v].position.y += text_offset_y;
+ }
vertex_ref.line = num_lines - 1;
switch(vertex_ref.codepoint) {
@@ -510,31 +533,20 @@ namespace QuickMedia
for(int j = last_space_index + 1; j <= i; ++j) {
VertexRef &vertex_ref_wrap = vertices_linear[j];
sf::Vertex *vertex = &vertices[vertex_ref_wrap.vertices_index][vertex_ref_wrap.index];
- vertex[0].position.x -= vertex_left_side;
- vertex[1].position.x -= vertex_left_side;
- vertex[2].position.x -= vertex_left_side;
- vertex[3].position.x -= vertex_left_side;
-
- vertex[0].position.y += line_height;
- vertex[1].position.y += line_height;
- vertex[2].position.y += line_height;
- vertex[3].position.y += line_height;
-
+ for(int v = 0; v < 6; ++v) {
+ vertex[v].position.x -= vertex_left_side;
+ vertex[v].position.y += line_height;
+ }
vertex_ref_wrap.line = num_lines - 1;
}
last_space_index = -1;
text_wrap_offset += vertex_left_side;
} else {
float vertex_left_side = get_text_quad_left_side(vertex_ref);
- vertex[0].position.x -= vertex_left_side;
- vertex[1].position.x -= vertex_left_side;
- vertex[2].position.x -= vertex_left_side;
- vertex[3].position.x -= vertex_left_side;
-
- vertex[0].position.y += line_height;
- vertex[1].position.y += line_height;
- vertex[2].position.y += line_height;
- vertex[3].position.y += line_height;
+ for(int v = 0; v < 6; ++v) {
+ vertex[v].position.x -= vertex_left_side;
+ vertex[v].position.y += line_height;
+ }
text_wrap_offset += vertex_left_side;
vertex_ref.line = num_lines - 1;