Colorful text.
This commit is contained in:
parent
303ea5dd27
commit
d7c15660e5
@ -1,6 +1,8 @@
|
||||
|
||||
#include "./ui_renderer.hpp"
|
||||
|
||||
#include <mijin/util/string.hpp>
|
||||
|
||||
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,21 +147,56 @@ 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;
|
||||
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:
|
||||
posX += drawChar({
|
||||
.x = posX,
|
||||
.y = posY,
|
||||
.chr = chr
|
||||
.chr = chr,
|
||||
.color = color
|
||||
});
|
||||
break;
|
||||
}
|
||||
@ -174,7 +211,8 @@ unsigned UIRenderer::drawChar(const DrawCharArgs& args)
|
||||
.topLeft = topLeft,
|
||||
.bottomRight = {topLeft.x + static_cast<float>(entry.width), topLeft.y + static_cast<float>(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);
|
||||
|
@ -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);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user