almost fixing text rendering

This commit is contained in:
2023-04-08 20:45:06 +02:00
parent 157a099ed2
commit 599f1007ab
8 changed files with 166 additions and 25 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
/* Updated: 2023/04/08 00:48:48 by maldavid ### ########.fr */
/* Updated: 2023/04/08 20:44:51 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,25 +14,28 @@
#include <fstream>
#include <utils/opensans-regular.h>
#include <cstdio>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#include <iostream>
namespace mlx
{
static uint8_t tmp_bitmap[512 * 512];
uint8_t tmp_bitmap[512 * 512];
void TextPutPipeline::init(Renderer* renderer) noexcept
{
_renderer = renderer;
stbtt_BakeFontBitmap(opensans_regular_ttf, 0, 32.0, tmp_bitmap, 512, 512, 32, 96, _cdata);
_atlas.create(reinterpret_cast<uint8_t*>(_cdata), 512, 512, VK_FORMAT_R8G8B8A8_UNORM);
stbtt_BakeFontBitmap(opensans_regular_ttf, 0, 20.0f, tmp_bitmap, 512, 512, 32, 96, _cdata);
_atlas.create(tmp_bitmap, 512, 512, VK_FORMAT_R8_UNORM);
_atlas.setDescriptor(renderer->getFragDescriptorSet().duplicate());
}
void TextPutPipeline::put(int x, int y, int color, std::string str)
{
_drawlist.emplace_back(std::move(str), color, x, y);
_drawlist.emplace(std::move(str), color, x, y);
}
void TextPutPipeline::render()
@@ -41,13 +44,16 @@ namespace mlx
for(auto& draw : _drawlist)
{
_atlas.setColor(draw.color);
int i = 0;
for(char c : draw.text)
{
if(c < 32 && c > 127)
if(c < 32 || c > 127)
continue;
stbtt_aligned_quad q;
stbtt_GetBakedQuad(_cdata, 512, 512, c - 32, &draw.x, &draw.y, &q, 1);
float null_x = 0.0f;
float null_y = 0.0f;
stbtt_GetBakedQuad(_cdata, 512, 512, c - 32, &null_x, &null_y, &q, 1);
std::array<glm::vec2, 4> pos = {
glm::vec2{q.x0, q.y0},
@@ -63,7 +69,7 @@ namespace mlx
glm::vec2{q.s0, q.t1}
};
_atlas.render(*_renderer, 0, 0, std::move(pos), std::move(uv));
_atlas.render(*_renderer, draw.x, draw.y, std::move(pos), std::move(uv));
}
}
}