diff -r 7daec2f067a9 -r 26ebb2f1e6e6 src/text.c --- a/src/text.c Wed Apr 10 19:43:34 2024 +0200 +++ b/src/text.c Fri Apr 12 22:03:15 2024 +0200 @@ -30,6 +30,8 @@ #include "ascension/error.h" #include "ascension/shader.h" +#include + #include static void asc_text_draw(AscText const *node) { @@ -58,19 +60,27 @@ // Render text onto a surface TTF_Font *font = asc_font_cache_validate(node->font)->ptr; + static int alignments[] = { + TTF_WRAPPED_ALIGN_LEFT, + TTF_WRAPPED_ALIGN_CENTER, + TTF_WRAPPED_ALIGN_RIGHT + }; TTF_SetFontWrappedAlign( - font, node->centered ? - TTF_WRAPPED_ALIGN_CENTER : TTF_WRAPPED_ALIGN_LEFT - ); + font, alignments[node->base.flags & ASC_TEXT_ALIGNMENT_MASK]); SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped( - font, node->text, asc_col_sdl(node->color), node->max_width + font, node->text.ptr, asc_col_sdl(node->color), node->max_width ); if (surface == NULL) { asc_error(SDL_GetError()); return; } asc_set_scale2d(&node->base, surface->w, surface->h); - asc_update_transform((AscSceneNode *) node); + if (asc_test_flag(node->base.flags, ASC_TEXT_CENTERED_FLAG)) { + unsigned short newoffx = surface->w / 2; + asc_vec2i pos = asc_get_position2d(&node->base); + asc_set_position2d(&node->base, pos.x + node->offx - newoffx, pos.y); + node->offx = newoffx; + } // Transfer Image Data asc_texture_from_surface(&node->tex, surface); @@ -80,6 +90,7 @@ } AscSceneNode *asc_text(int x, int y, char const *text) { + // TODO: implement fancy struct "named param" initialization AscText *node = calloc(1, sizeof(AscText)); node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND; @@ -91,8 +102,10 @@ node->base.position.y = (float) y; node->font = asc_context.active_font; node->color = asc_context.ink; - if (text != NULL) { - node->text = strdup(text); + if (text == NULL) { + node->text = cx_mutstr(strdup(" ")); + } else { + node->text = cx_mutstr(strdup(text)); } // initialize @@ -103,6 +116,19 @@ void asc_text_free(AscText *node) { asc_texture_destroy(&node->tex); - free(node->text); + cx_strfree(&node->text); free(node); -} \ No newline at end of file +} + +void asc_text_printf( + AscSceneNode *node, + char const* format, + ... +) { + cxmutstr text = ((AscText*)node)->text; + va_list ap; + va_start(ap, format); + cx_vsprintf(&text.ptr, &text.length, format, ap); + va_end(ap); + asc_node_update(node); +}