don't force the use of CxBuffer in AscTextNode

Thu, 18 Jan 2024 21:53:16 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Jan 2024 21:53:16 +0100
changeset 23
ab07757004b4
parent 22
30b12e88fd84
child 24
7183b4ae9b20

don't force the use of CxBuffer in AscTextNode

src/ascension/text.h file | annotate | diff | comparison | revisions
src/text.c file | annotate | diff | comparison | revisions
test/sandbox.c file | annotate | diff | comparison | revisions
--- a/src/ascension/text.h	Mon Dec 18 19:05:30 2023 +0100
+++ b/src/ascension/text.h	Thu Jan 18 21:53:16 2024 +0100
@@ -33,7 +33,7 @@
 #include <cx/buffer.h>
 
 typedef struct AscTextNode {
-    CxBuffer text;
+    char *text;
     AscFont const *font;
     asc_vec2i position;
     asc_col4i color;
--- a/src/text.c	Mon Dec 18 19:05:30 2023 +0100
+++ b/src/text.c	Thu Jan 18 21:53:16 2024 +0100
@@ -42,9 +42,7 @@
     node->position = (asc_vec2i) {x, y};
     node->font = asc_context.active_font;
     node->color = asc_context.ink;
-    cxBufferInit(&node->text, NULL, strlen(text)+8,
-                 cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
-    cxBufferPutString(&node->text, text);
+    node->text = strdup(text);
 
     return node;
 }
@@ -64,15 +62,10 @@
         asc_dprintf("Generated new texture for text node: %u", node->internal.tex_id);
     }
 
-    // ensure the text is zero-terminated
-    CxBuffer* text = &(node->text);
-    cxBufferMinimumCapacity(text, text->size+1);
-    text->space[text->size] = '\0';
-
     // Render text onto a surface
     SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
             asc_font_cache_validate(node->font)->ptr,
-            text->space,
+            node->text,
             asc_col_sdl(node->color),
             node->max_width
     );
@@ -136,6 +129,6 @@
 void asc_text_free(AscTextNode *node) {
     asc_dprintf("Release text node texture: %u", node->internal.tex_id);
     glDeleteTextures(1, &node->internal.tex_id);
-    cxBufferDestroy(&node->text);
+    free(node->text);
     free(node);
 }
\ No newline at end of file
--- a/test/sandbox.c	Mon Dec 18 19:05:30 2023 +0100
+++ b/test/sandbox.c	Thu Jan 18 21:53:16 2024 +0100
@@ -54,7 +54,7 @@
 
     asc_set_font(asc_font(ASC_FONT_REGULAR, 24));
     asc_ink_rgb(255, 0, 0);
-    AscTextNode *fps_counter = asc_text(50, 50, "60 FPS");
+    AscTextNode *fps_counter = asc_text(50, 50, "9999 FPS");
     unsigned last_fps = 0;
 
     while (asc_loop_next()) {
@@ -67,8 +67,7 @@
             unsigned fps = 1000u / asc_context.elapsed_millis;
             if (fps != last_fps) {
                 last_fps = fps;
-                cxBufferReset(&fps_counter->text);
-                cx_bprintf(&fps_counter->text, "%u FPS", fps);
+                snprintf(fps_counter->text, 9, "%u FPS", fps);
                 asc_text_update(fps_counter);
             }
         }

mercurial