From d7c15660e5d73b950b81625821184688af28ed6f Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Tue, 17 Sep 2024 17:37:29 +0200 Subject: [PATCH] Colorful text. --- private/sdl_gpu_test/6_ui/ui/ui_renderer.cpp | 74 +++++++++++++++----- private/sdl_gpu_test/6_ui/ui/ui_renderer.hpp | 3 + 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/private/sdl_gpu_test/6_ui/ui/ui_renderer.cpp b/private/sdl_gpu_test/6_ui/ui/ui_renderer.cpp index 3316a81..431198f 100644 --- a/private/sdl_gpu_test/6_ui/ui/ui_renderer.cpp +++ b/private/sdl_gpu_test/6_ui/ui/ui_renderer.cpp @@ -1,6 +1,8 @@ #include "./ui_renderer.hpp" +#include + namespace sdl_gpu_test::inline app6 { namespace @@ -94,7 +96,7 @@ void UIRenderer::init(Application& application) drawText({ .x = 100, .y = 100, - .text = "Test-Text!\nSecond line?" + .text = "Test-Text!\nSecond $f00line$fff?" }); // create UI vertex buffer @@ -145,23 +147,58 @@ void UIRenderer::render(const UIRendererRenderArgs& args) void UIRenderer::drawText(const DrawTextArgs& args) { + glm::vec4 color = args.color; unsigned posX = args.x; unsigned posY = args.y; - for (const char chr : args.text) + + for (std::size_t pos = 0; pos < args.text.size(); ++pos) { + char chr = args.text[pos]; switch (chr) { - case '\n': - posX = args.x; - posY += mFontMap.lineHeight; + case '\n': + posX = args.x; + posY += mFontMap.lineHeight; + break; + case '$': + ++pos; + if (pos >= args.text.size()) + { break; - default: - posX += drawChar({ - .x = posX, - .y = posY, - .chr = chr - }); + } + chr = args.text[pos]; + if (pos < args.text.size() - 2 + && mijin::isHexadecimalChar(chr) + && mijin::isHexadecimalChar(args.text[pos + 1]) + && mijin::isHexadecimalChar(args.text[pos + 2])) + { + unsigned num = 0; + (void) mijin::toNumber(args.text.substr(pos, 3), num, 16); + const std::uint8_t red = static_cast(num >> 8); + const std::uint8_t green = static_cast((num >> 4) & 0xF); + const std::uint8_t blue = static_cast(num & 0xF); + color.r = static_cast(red) / 15.f; + color.g = static_cast(green) / 15.f; + color.b = static_cast(blue) / 15.f; + pos += 2; + } + break; + case '\\': + ++pos; + if (pos >= args.text.size()) + { break; + } + chr = args.text[pos]; + [[fallthrough]]; + default: + posX += drawChar({ + .x = posX, + .y = posY, + .chr = chr, + .color = color + }); + break; } } } @@ -174,7 +211,8 @@ unsigned UIRenderer::drawChar(const DrawCharArgs& args) .topLeft = topLeft, .bottomRight = {topLeft.x + static_cast(entry.width), topLeft.y + static_cast(entry.height)}, .uvTopLeft = {entry.uvX, entry.uvY}, - .uvBottomRight = {entry.uvX + entry.uvWidth, entry.uvY + entry.uvHeight} + .uvBottomRight = {entry.uvX + entry.uvWidth, entry.uvY + entry.uvHeight}, + .color = args.color }); return entry.xAdvance; @@ -184,19 +222,23 @@ void UIRenderer::drawQuadInternal(const DrawQuadInternalArgs& args) { const UIVertex topLeft = { .pos = args.topLeft, - .texcoord = args.uvTopLeft + .texcoord = args.uvTopLeft, + .color = args.color }; const UIVertex bottomRight = { .pos = args.bottomRight, - .texcoord = args.uvBottomRight + .texcoord = args.uvBottomRight, + .color = args.color }; const UIVertex bottomLeft = { .pos = {topLeft.pos.x, bottomRight.pos.y}, - .texcoord = {topLeft.texcoord.x, bottomRight.texcoord.y} + .texcoord = {topLeft.texcoord.x, bottomRight.texcoord.y}, + .color = args.color }; const UIVertex topRight = { .pos = {bottomRight.pos.x, topLeft.pos.y}, - .texcoord = {bottomRight.texcoord.x, topLeft.texcoord.y} + .texcoord = {bottomRight.texcoord.x, topLeft.texcoord.y}, + .color = args.color }; mUIVertices.push_back(topLeft); diff --git a/private/sdl_gpu_test/6_ui/ui/ui_renderer.hpp b/private/sdl_gpu_test/6_ui/ui/ui_renderer.hpp index 57a2458..d97aea0 100644 --- a/private/sdl_gpu_test/6_ui/ui/ui_renderer.hpp +++ b/private/sdl_gpu_test/6_ui/ui/ui_renderer.hpp @@ -24,6 +24,7 @@ struct DrawTextArgs unsigned x = 0; unsigned y = 0; std::string_view text; + glm::vec4 color = glm::vec4(1.f); }; struct DrawCharArgs @@ -31,6 +32,7 @@ struct DrawCharArgs unsigned x = 0; unsigned y = 0; char chr; + glm::vec4 color = glm::vec4(1.f); }; struct UIRendererRenderArgs @@ -64,6 +66,7 @@ private: glm::vec2 bottomRight; glm::vec2 uvTopLeft; glm::vec2 uvBottomRight; + glm::vec4 color = glm::vec4(1.f); }; void drawQuadInternal(const DrawQuadInternalArgs& args); };