Colorful text.

This commit is contained in:
Patrick 2024-09-17 17:37:29 +02:00
parent 303ea5dd27
commit d7c15660e5
2 changed files with 61 additions and 16 deletions

View File

@ -1,6 +1,8 @@
#include "./ui_renderer.hpp" #include "./ui_renderer.hpp"
#include <mijin/util/string.hpp>
namespace sdl_gpu_test::inline app6 namespace sdl_gpu_test::inline app6
{ {
namespace namespace
@ -94,7 +96,7 @@ void UIRenderer::init(Application& application)
drawText({ drawText({
.x = 100, .x = 100,
.y = 100, .y = 100,
.text = "Test-Text!\nSecond line?" .text = "Test-Text!\nSecond $f00line$fff?"
}); });
// create UI vertex buffer // create UI vertex buffer
@ -145,21 +147,56 @@ void UIRenderer::render(const UIRendererRenderArgs& args)
void UIRenderer::drawText(const DrawTextArgs& args) void UIRenderer::drawText(const DrawTextArgs& args)
{ {
glm::vec4 color = args.color;
unsigned posX = args.x; unsigned posX = args.x;
unsigned posY = args.y; 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) switch (chr)
{ {
case '\n': case '\n':
posX = args.x; posX = args.x;
posY += mFontMap.lineHeight; posY += mFontMap.lineHeight;
break; break;
case '$':
++pos;
if (pos >= args.text.size())
{
break;
}
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<std::uint8_t>(num >> 8);
const std::uint8_t green = static_cast<std::uint8_t>((num >> 4) & 0xF);
const std::uint8_t blue = static_cast<std::uint8_t>(num & 0xF);
color.r = static_cast<float>(red) / 15.f;
color.g = static_cast<float>(green) / 15.f;
color.b = static_cast<float>(blue) / 15.f;
pos += 2;
}
break;
case '\\':
++pos;
if (pos >= args.text.size())
{
break;
}
chr = args.text[pos];
[[fallthrough]];
default: default:
posX += drawChar({ posX += drawChar({
.x = posX, .x = posX,
.y = posY, .y = posY,
.chr = chr .chr = chr,
.color = color
}); });
break; break;
} }
@ -174,7 +211,8 @@ unsigned UIRenderer::drawChar(const DrawCharArgs& args)
.topLeft = topLeft, .topLeft = topLeft,
.bottomRight = {topLeft.x + static_cast<float>(entry.width), topLeft.y + static_cast<float>(entry.height)}, .bottomRight = {topLeft.x + static_cast<float>(entry.width), topLeft.y + static_cast<float>(entry.height)},
.uvTopLeft = {entry.uvX, entry.uvY}, .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; return entry.xAdvance;
@ -184,19 +222,23 @@ void UIRenderer::drawQuadInternal(const DrawQuadInternalArgs& args)
{ {
const UIVertex topLeft = { const UIVertex topLeft = {
.pos = args.topLeft, .pos = args.topLeft,
.texcoord = args.uvTopLeft .texcoord = args.uvTopLeft,
.color = args.color
}; };
const UIVertex bottomRight = { const UIVertex bottomRight = {
.pos = args.bottomRight, .pos = args.bottomRight,
.texcoord = args.uvBottomRight .texcoord = args.uvBottomRight,
.color = args.color
}; };
const UIVertex bottomLeft = { const UIVertex bottomLeft = {
.pos = {topLeft.pos.x, bottomRight.pos.y}, .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 = { const UIVertex topRight = {
.pos = {bottomRight.pos.x, topLeft.pos.y}, .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); mUIVertices.push_back(topLeft);

View File

@ -24,6 +24,7 @@ struct DrawTextArgs
unsigned x = 0; unsigned x = 0;
unsigned y = 0; unsigned y = 0;
std::string_view text; std::string_view text;
glm::vec4 color = glm::vec4(1.f);
}; };
struct DrawCharArgs struct DrawCharArgs
@ -31,6 +32,7 @@ struct DrawCharArgs
unsigned x = 0; unsigned x = 0;
unsigned y = 0; unsigned y = 0;
char chr; char chr;
glm::vec4 color = glm::vec4(1.f);
}; };
struct UIRendererRenderArgs struct UIRendererRenderArgs
@ -64,6 +66,7 @@ private:
glm::vec2 bottomRight; glm::vec2 bottomRight;
glm::vec2 uvTopLeft; glm::vec2 uvTopLeft;
glm::vec2 uvBottomRight; glm::vec2 uvBottomRight;
glm::vec4 color = glm::vec4(1.f);
}; };
void drawQuadInternal(const DrawQuadInternalArgs& args); void drawQuadInternal(const DrawQuadInternalArgs& args);
}; };