// /** // * Copyright (c) 2025 Dominic Masters // * // * This software is released under the MIT License. // * https://opensource.org/licenses/MIT // */ #include "uitext.h" #include "asset/assetmanager.h" #include "assert/assert.h" #include "util/memory.h" #include "display/spritebatch/spritebatch.h" uitext_t UI_TEXT; errorret_t uiTextInit(void) { memoryZero(&UI_TEXT, sizeof(uitext_t)); errorChain(assetManagerLoadAsset( TILESET_FONT_MINOGRAM.image, &UI_TEXT.asset, &UI_TEXT.assetRef )); errorOk(); } void uiTextDispose(void) { if(UI_TEXT.asset) { assetUnlock(UI_TEXT.asset, UI_TEXT.assetRef); } } void uiTextDrawChar( const float_t x, const float_t y, const char_t c, const color_t color ) { int32_t tileIndex = (int32_t)(c) - UI_TEXT_CHAR_START; if(tileIndex < 0 || tileIndex >= TILESET_FONT_MINOGRAM.tileCount) { tileIndex = ((int32_t)'@') - UI_TEXT_CHAR_START; } assertTrue( tileIndex >= 0 && tileIndex <= TILESET_FONT_MINOGRAM.tileCount, "Character is out of bounds for font tiles" ); vec4 uv; tilesetTileGetUV(&TILESET_FONT_MINOGRAM, tileIndex, uv); spriteBatchPush( &UI_TEXT.asset->alphaImage.texture, x, y, x + TILESET_FONT_MINOGRAM.tileWidth, y + TILESET_FONT_MINOGRAM.tileHeight, color, uv[0], uv[1], uv[2], uv[3] ); } void uiTextDraw( const float_t x, const float_t y, const char_t *text, const color_t color ) { assertNotNull(text, "Text cannot be NULL"); float_t posX = x; float_t posY = y; char_t c; int32_t i = 0; while((c = text[i++]) != '\0') { if(c == '\n') { posX = x; posY += TILESET_FONT_MINOGRAM.tileHeight; continue; } if(c == ' ') { posX += TILESET_FONT_MINOGRAM.tileWidth; continue; } uiTextDrawChar(posX, posY, c, color); posX += TILESET_FONT_MINOGRAM.tileWidth; } } void uiTextMeasure( const char_t *text, int32_t *outWidth, int32_t *outHeight ) { assertNotNull(text, "Text cannot be NULL"); assertNotNull(outWidth, "Output width pointer cannot be NULL"); assertNotNull(outHeight, "Output height pointer cannot be NULL"); int32_t width = 0; int32_t height = TILESET_FONT_MINOGRAM.tileHeight; int32_t lineWidth = 0; char_t c; int32_t i = 0; while((c = text[i++]) != '\0') { if(c == '\n') { if(lineWidth > width) { width = lineWidth; } lineWidth = 0; height += TILESET_FONT_MINOGRAM.tileHeight; continue; } lineWidth += TILESET_FONT_MINOGRAM.tileWidth; } if(lineWidth > width) { width = lineWidth; } *outWidth = width; *outHeight = height; }