library: Allow loading font bitmaps from RAM, not just the filesystem

This commit is contained in:
Antonio Niño Díaz 2024-03-07 01:20:00 +00:00
parent a49ddecda7
commit dd746c0ef5
2 changed files with 83 additions and 8 deletions

View File

@ -121,6 +121,39 @@ int NE_RichTextMaterialSet(u32 slot, NE_Material *mat, NE_Palette *pal);
/// @return Returns 1 on success, 0 on failure.
int NE_RichTextBitmapLoadGRF(u32 slot, const char *path);
/// Assign a texture (and palette) to be used to render text to textures.
///
/// This doesn't load the texture to VRAM, it keeps it in RAM. The texture will
/// be used whenever the user uses NE_RichTextRenderMaterial() to render text to
/// a new material.
///
/// The buffers provided won't be freed when the text font is cleared with
/// NE_RichTextEnd(), they have to be freed manually.
///
/// Also, note that this texture doesn't need to have a size that is a power of
/// two. However, consider that the size of a row should at least be a multiple
/// of a full byte (for example, for a 16 color texture, don't use a texture
/// with width of 143 because the last byte won't be full). To be sure that you
/// never find any issue, ensure that your textures have a width multiple of 4
/// pixels, that will work with all texture formats.
///
/// This is required for NE_RichTextRenderMaterial().
///
/// This isn't required for NE_RichTextRender3D() or NE_RichTextRender3DAlpha().
///
/// @param slot The slot to use.
/// @param texture_buffer Pointer to the texture.
/// @param texture_width Width of the texture.h
/// @param texture_height Height of the texture.
/// @param texture_fmt Format of the texture.
/// @param palette_buffer Pointer to the palette.
/// @param palette_size Size of the palette in bytes.
/// @return Returns 1 on success, 0 on failure.
int NE_RichTextBitmapSet(u32 slot, const void *texture_buffer,
size_t texture_width, size_t texture_height,
NE_TextureFormat texture_fmt,
const void *palette_buffer, size_t palette_size);
/// Render a string by rendering one 3D quad per codepoint.
///
/// This preserves the polygon format that is currently active.

View File

@ -16,6 +16,7 @@ typedef struct {
NE_Palette *palette;
// Fields used when the font texture is stored in RAM
bool has_to_free_buffers;
NE_TextureFormat fmt;
void *texture_buffer;
size_t texture_width;
@ -59,10 +60,13 @@ int NE_RichTextEnd(u32 slot)
if (info->palette != NULL)
NE_PaletteDelete(info->palette);
if (info->texture_buffer != NULL)
free(info->texture_buffer);
if (info->palette_buffer != NULL)
free(info->palette_buffer);
if (info->has_to_free_buffers)
{
if (info->texture_buffer != NULL)
free(info->texture_buffer);
if (info->palette_buffer != NULL)
free(info->palette_buffer);
}
int ret = 0;
@ -189,10 +193,13 @@ int NE_RichTextBitmapLoadGRF(u32 slot, const char *path)
if (!info->active)
return 0;
if (info->texture_buffer != NULL)
free(info->texture_buffer);
if (info->palette_buffer != NULL)
free(info->palette_buffer);
if (info->has_to_free_buffers)
{
if (info->texture_buffer != NULL)
free(info->texture_buffer);
if (info->palette_buffer != NULL)
free(info->palette_buffer);
}
int ret = 0;
@ -265,6 +272,8 @@ int NE_RichTextBitmapLoadGRF(u32 slot, const char *path)
info->palette_size = 0;
}
info->has_to_free_buffers = true;
ret = 1; // Success
cleanup:
@ -274,6 +283,39 @@ cleanup:
#endif // NE_BLOCKSDS
}
int NE_RichTextBitmapSet(u32 slot, const void *texture_buffer,
size_t texture_width, size_t texture_height,
NE_TextureFormat texture_fmt,
const void *palette_buffer, size_t palette_size)
{
NE_AssertPointer(texture_buffer, "NULL texture pointer");
NE_AssertPointer(palette_buffer, "NULL palette pointer");
if (slot >= NE_MAX_RICH_TEXT_FONTS)
return 0;
ne_rich_textinfo_t *info = &NE_RichTextInfo[slot];
if (!info->active)
return 0;
if (info->has_to_free_buffers)
{
if (info->texture_buffer != NULL)
free(info->texture_buffer);
if (info->palette_buffer != NULL)
free(info->palette_buffer);
}
info->texture_buffer = (void *)texture_buffer;
info->texture_width = texture_width;
info->texture_height = texture_height;
info->fmt = texture_fmt;
info->palette_buffer = (void *)palette_buffer;
info->palette_size = palette_size;
return 1;
}
int NE_RichTextRender3D(u32 slot, const char *str, s32 x, s32 y)
{
NE_AssertPointer(str, "NULL str pointer");