2026-07-30 01:10:31 -04:00
|
|
|
/**
|
|
|
|
|
* @file text.c
|
|
|
|
|
* @brief Implements the text subsystem.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-05-06 23:18:42 -04:00
|
|
|
#include <akerror.h>
|
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
|
#include <SDL3_ttf/SDL_ttf.h>
|
2026-05-07 22:20:10 -04:00
|
|
|
#include <akgl/text.h>
|
|
|
|
|
#include <akgl/registry.h>
|
|
|
|
|
#include <akgl/game.h>
|
2026-05-06 23:18:42 -04:00
|
|
|
|
|
|
|
|
akerr_ErrorContext AKERR_NOIGNORE *akgl_text_loadfont(char *name, char *filepath, int size)
|
|
|
|
|
{
|
|
|
|
|
TTF_Font *font = NULL;
|
Reindent all C sources to the canonical stroustrup style
Mechanical whitespace-only change across src/, include/, tests/, and
util/, applied with scripts/reindent.sh. No behavioral change.
Most files already conformed. The genuine outliers indented at 2 columns
(json_helpers.c, util.c from akgl_rectangle_points onward, assets.c,
staticstring.c, akgl_actor_add_child, util.h, staticstring.h) or used
spaces where the canonical form is a tab (draw.c). cc-mode also re-aligns
line-continuation backslashes in multi-line macros to its default
c-backslash-column, which is required for the tree to be a fixed point of
the indenter.
Verified whitespace-only: `git diff -w` over this change is empty apart
from three trailing blank lines removed at EOF in src/heap.c,
src/registry.c, and tests/tilemap.c. The build produces no new errors and
ctest is unchanged at 13/14, with `character` still the one intentional
failure.
The tree is now a fixed point of `scripts/reindent.sh --check`.
include/akgl/SDL_GameControllerDB.h is generated and excluded.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 18:17:37 -04:00
|
|
|
|
2026-05-06 23:18:42 -04:00
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, name, AKERR_NULLPOINTER, "Null font name");
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, name, AKERR_NULLPOINTER, "Null filepath");
|
|
|
|
|
font = TTF_OpenFont(filepath, size);
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, font, AKGL_ERR_SDL, "%s", SDL_GetError());
|
|
|
|
|
FAIL_ZERO_RETURN(
|
|
|
|
|
errctx,
|
|
|
|
|
SDL_SetPointerProperty(AKGL_REGISTRY_FONT, name, (void *)font),
|
|
|
|
|
AKERR_KEY,
|
|
|
|
|
"Unable to add font %p to registry as %s : %s",
|
|
|
|
|
(void *)font,
|
|
|
|
|
name,
|
|
|
|
|
SDL_GetError());
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext AKERR_NOIGNORE *akgl_text_rendertextat(TTF_Font *font, char *text, SDL_Color color, int wraplength, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
SDL_Surface *textsurf = NULL;
|
|
|
|
|
SDL_Texture *texture = NULL;
|
|
|
|
|
SDL_FRect dest;
|
Reindent all C sources to the canonical stroustrup style
Mechanical whitespace-only change across src/, include/, tests/, and
util/, applied with scripts/reindent.sh. No behavioral change.
Most files already conformed. The genuine outliers indented at 2 columns
(json_helpers.c, util.c from akgl_rectangle_points onward, assets.c,
staticstring.c, akgl_actor_add_child, util.h, staticstring.h) or used
spaces where the canonical form is a tab (draw.c). cc-mode also re-aligns
line-continuation backslashes in multi-line macros to its default
c-backslash-column, which is required for the tree to be a fixed point of
the indenter.
Verified whitespace-only: `git diff -w` over this change is empty apart
from three trailing blank lines removed at EOF in src/heap.c,
src/registry.c, and tests/tilemap.c. The build produces no new errors and
ctest is unchanged at 13/14, with `character` still the one intentional
failure.
The tree is now a fixed point of `scripts/reindent.sh --check`.
include/akgl/SDL_GameControllerDB.h is generated and excluded.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 18:17:37 -04:00
|
|
|
|
2026-05-06 23:18:42 -04:00
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, font, AKERR_NULLPOINTER, "NULL font");
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, text, AKERR_NULLPOINTER, "NULL text string");
|
|
|
|
|
if ( wraplength > 0 ) {
|
|
|
|
|
textsurf = TTF_RenderText_Blended_Wrapped(
|
|
|
|
|
font,
|
|
|
|
|
text,
|
|
|
|
|
0,
|
|
|
|
|
color,
|
|
|
|
|
wraplength);
|
|
|
|
|
} else {
|
|
|
|
|
textsurf = TTF_RenderText_Blended(
|
|
|
|
|
font,
|
|
|
|
|
text,
|
|
|
|
|
0,
|
|
|
|
|
color);
|
|
|
|
|
}
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, textsurf, AKERR_NULLPOINTER, "%s", SDL_GetError());
|
2026-06-02 13:15:26 -04:00
|
|
|
texture = SDL_CreateTextureFromSurface(renderer->sdl_renderer, textsurf);
|
2026-05-06 23:18:42 -04:00
|
|
|
FAIL_ZERO_RETURN(errctx, texture, AKERR_NULLPOINTER, "%s", SDL_GetError());
|
|
|
|
|
dest.x = x;
|
|
|
|
|
dest.y = y;
|
|
|
|
|
SDL_GetTextureSize(texture, &dest.w, &dest.h);
|
2026-06-02 13:15:26 -04:00
|
|
|
PASS(errctx, renderer->draw_texture(renderer, texture, NULL, &dest, 0, NULL, SDL_FLIP_NONE));
|
2026-05-06 23:18:42 -04:00
|
|
|
SDL_DestroyTexture(texture);
|
2026-05-15 11:07:31 -04:00
|
|
|
SDL_DestroySurface(textsurf);
|
2026-05-06 23:18:42 -04:00
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|