This commit is contained in:
Jorijn van der Graaf 2026-05-03 02:45:38 +02:00
commit c054f1e0b3
9 changed files with 699 additions and 5 deletions

View file

@ -184,3 +184,50 @@ void Crafter::DrawProgressBar(UIBuffer& buf, Rect r, float t01,
});
}
}
// ─── DrawText ───────────────────────────────────────────────────────────
float Crafter::DrawText(UIBuffer& buf, std::string_view text,
float x, float baselineY,
Font& font, float fontSize,
std::array<float, 4> color,
TextAlign align)
{
if (text.empty() || buf.atlas == nullptr || buf.renderer == nullptr) return 0.0f;
if (buf.glyphs == nullptr || buf.glyphCount == nullptr) return 0.0f;
std::uint32_t before = *buf.glyphCount;
std::uint32_t cap = (buf.glyphCap > before) ? (buf.glyphCap - before) : 0;
if (cap == 0) return 0.0f;
GlyphItem* writePos = buf.glyphs + before;
float advance = 0.0f;
std::uint32_t n = buf.renderer->ShapeText(
font, fontSize, x, baselineY,
text, color, writePos, cap, &advance
);
*buf.glyphCount = before + n;
if (align != TextAlign::Left && n > 0) {
float shift = (align == TextAlign::Center) ? -advance * 0.5f : -advance;
for (std::uint32_t i = 0; i < n; ++i) writePos[i].x += shift;
}
return advance;
}
// ─── DrawImage ──────────────────────────────────────────────────────────
void Crafter::DrawImage(UIBuffer& buf, Rect r,
std::uint32_t textureSlot, std::uint32_t samplerSlot,
std::array<float, 4> tint,
std::array<float, 4> uv)
{
if (buf.images == nullptr || buf.imageCount == nullptr) return;
if (*buf.imageCount >= buf.imageCap) return;
buf.images[(*buf.imageCount)++] = ImageItem{
r.x, r.y, r.w, r.h,
uv[0], uv[1], uv[2], uv[3],
tint[0], tint[1], tint[2], tint[3],
textureSlot, samplerSlot, 0, 0,
};
}