From a3d12de5f6adf7882b885fb1fcbc22c6a2763242 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 17 Mar 2021 13:04:20 +0100 Subject: Use triangles instead of quads for text --- src/Text.cpp | 110 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 49 deletions(-) (limited to 'src/Text.cpp') 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; -- cgit v1.2.3