123 lines
3.5 KiB
C
123 lines
3.5 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "asset/asset.h"
|
|
#include "display/text/font.h"
|
|
#include "display/spritebatch/spritebatch.h"
|
|
|
|
#define TEXT_CHAR_START '!'
|
|
|
|
/**
|
|
* Initializes the text system.
|
|
*
|
|
* @return Either an error or success result.
|
|
*/
|
|
errorret_t textInit(void);
|
|
|
|
/**
|
|
* Disposes of the text system.
|
|
*
|
|
* @return Either an error or success result.
|
|
*/
|
|
errorret_t textDispose(void);
|
|
|
|
/**
|
|
* Builds a sprite for a single character at the given position.
|
|
*
|
|
* @param pos The (x, y) position of the character in screen/world space.
|
|
* @param c The character to build a sprite for.
|
|
* @param font Font to use for tile lookup.
|
|
* @return The populated sprite ready for spriteBatchBuffer.
|
|
*/
|
|
spritebatchsprite_t textGetSprite(
|
|
const vec2 pos,
|
|
const char_t c,
|
|
const font_t *font
|
|
);
|
|
|
|
/**
|
|
* Draws a string of text at the specified position.
|
|
*
|
|
* @param x The x-coordinate to draw the text at.
|
|
* @param y The y-coordinate to draw the text at.
|
|
* @param text The null-terminated string of text to draw.
|
|
* @param color The color to draw the text in.
|
|
* @param font Font to use for rendering.
|
|
* @return Either an error or success result.
|
|
*/
|
|
errorret_t textDraw(
|
|
const float_t x,
|
|
const float_t y,
|
|
const char_t *text,
|
|
const color_t color,
|
|
font_t *font
|
|
);
|
|
|
|
/**
|
|
* Builds a cache of sprites (glyph geometry + UVs, relative to origin
|
|
* 0,0) for a string of text. Callers that redraw the same text every
|
|
* frame (e.g. UI labels/widgets) should build this once and reuse it
|
|
* via textDrawSpriteCache, instead of re-deriving glyph geometry every
|
|
* frame the way textDraw does.
|
|
*
|
|
* @param text The null-terminated string to build sprites for.
|
|
* @param font Font to use for tile lookup.
|
|
* @param sprites Destination array to write sprites into.
|
|
* @param spritesMax Capacity of the sprites array.
|
|
* @param outWidth Pointer to store the measured width in pixels.
|
|
* @param outHeight Pointer to store the measured height in pixels.
|
|
* @return The number of sprites written.
|
|
*/
|
|
uint32_t textBuildSpriteCache(
|
|
const char_t *text,
|
|
const font_t *font,
|
|
spritebatchsprite_t *sprites,
|
|
const uint32_t spritesMax,
|
|
int32_t *outWidth,
|
|
int32_t *outHeight
|
|
);
|
|
|
|
/**
|
|
* Draws a previously-built sprite cache (see textBuildSpriteCache) at the
|
|
* given position in a single batched draw call.
|
|
*
|
|
* @param sprites Cached sprites, relative to origin 0,0.
|
|
* @param spriteCount Number of sprites in the cache.
|
|
* @param scratch Caller-owned scratch buffer, at least spriteCount
|
|
* entries, used to translate the cached sprites into position.
|
|
* @param x The x-coordinate to draw the text at.
|
|
* @param y The y-coordinate to draw the text at.
|
|
* @param color The color to draw the text in.
|
|
* @param texture The font's texture to sample glyphs from.
|
|
* @return Either an error or success result.
|
|
*/
|
|
errorret_t textDrawSpriteCache(
|
|
const spritebatchsprite_t *sprites,
|
|
const uint32_t spriteCount,
|
|
spritebatchsprite_t *scratch,
|
|
const float_t x,
|
|
const float_t y,
|
|
const color_t color,
|
|
texture_t *texture
|
|
);
|
|
|
|
/**
|
|
* Measures the width and height of the given text string when rendered.
|
|
*
|
|
* @param text The null-terminated string of text to measure.
|
|
* @param font Font to use for measurement.
|
|
* @param outWidth Pointer to store the measured width in pixels.
|
|
* @param outHeight Pointer to store the measured height in pixels.
|
|
*/
|
|
void textMeasure(
|
|
const char_t *text,
|
|
const font_t *font,
|
|
int32_t *outWidth,
|
|
int32_t *outHeight
|
|
);
|