[WIP] Add text rendering

This commit is contained in:
Piotr Krygier committed 2025-06-20 13:44:33 +02:00
1 parent 3bd14b20c2
commit b2508ce4ff
15 files changed
+248 -27

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
#include "rse_text_renderer.h"
#include <stdint.h>
#include <strings.h>
#include "freetype/fttypes.h"
#include "locale_fonts.h"
#include "rse_font_errors.h"
#include "utilities/logger.h"
#include "utilities/rse_commons.h"
#include "utilities/rse_errors_common.h"
#include "rse_vulkan_image.h"
#include "ft2build.h"
#include "vulkan/vulkan_core.h"
#include FT_FREETYPE_H
#define FONT_CHARACTERS_COUNT 126
uint16_t g_font_textures[FONT_CHARACTERS_COUNT];
FT_Library g_library;
rse_err_t rse_fonts_initialize()
{
FT_Error error;
error = FT_Init_FreeType(&g_library);
if (error) {
LOGF(font_messages[FONT_INITIALIZATION_FAILED]);
return FONT_ERROR_INIT_FAILED;
}
return RSE_ERROR_NO_ERROR;
}
rse_err_t rse_fonts_load_from_file(const char* file_path)
{
rse_err_t status = RSE_ERROR_NO_ERROR;
FT_Error error;
FT_Face face;
size_t iter = 0U;
error = FT_New_Face(g_library, file_path, 0, &face);
if (error) {
LOGF(font_messages[FONT_LOAD_FONT_FAILED]);
return FONT_ERROR_LOAD_FAILED;
}
/* By default we set font size to 16px. TODO: change later? */
error = FT_Set_Pixel_Sizes(face, 0, 48);
if (error) {
LOGF(font_messages[FONT_CHANGE_SIZE_FAILED]);
return FONT_ERROR_CHANGE_SIZE_FAILED;
}
/* Convert font face to textured image */
/* Loading only needed ascii characters, from 40 (space) to 126 (~) */
for (iter = 33U; iter < face->num_glyphs && iter < 127U; ++iter) {
error = FT_Load_Char(face, iter, FT_LOAD_RENDER);
if (error) {
LOGF(font_messages[FONT_LOAD_GLYPH_FAILED]);
return FONT_ERROR_LOAD_GLYPH_FAILED;
}
STATUS_CHECK(rse_load_texture_from_bitmat(face->glyph->bitmap.buffer,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
&g_font_textures[iter], VK_FORMAT_R8_UNORM));
}
return status;
}
rse_err_t rse_print_text(const char* text)
{
rse_err_t status = RSE_ERROR_NO_ERROR;
return status;
}