src/text.c

changeset 207
4d184a8706b1
parent 206
26726b7a89a7
child 208
658bccb1bf73
--- a/src/text.c	Mon Jul 14 21:56:53 2025 +0200
+++ b/src/text.c	Wed Jul 16 23:27:34 2025 +0200
@@ -70,7 +70,11 @@
 
     // Render text onto a surface
     TTF_Font *font = asc_font_load(text->font);
-    if (font == NULL) return;
+    if (font == NULL) {
+        // cannot load font - hide the text node to avoid errors when trying to draw
+        asc_set_flag(node->flags, ASC_SCENE_NODE_HIDDEN);
+        return;
+    }
     static int alignments[] = {
             TTF_WRAPPED_ALIGN_LEFT,
             TTF_WRAPPED_ALIGN_CENTER,
@@ -141,17 +145,15 @@
 
 AscSceneNode *asc_text_create(struct asc_text_create_args args) {
     AscText *text = cxZallocDefault(sizeof(AscText));
-    AscSceneNode *node = &text->base;
+    asc_ptr_cast(AscSceneNode, node, text);
 
     // node properties
-    asc_scene_node_name(node, args.name);
-    node->render_group = ASC_RENDER_GROUP_2D_BLEND;
-    node->destroy_func = asc_text_destroy;
-    node->update_func = asc_text_update;
-    node->draw_func = asc_text_draw;
-    node->position = ASC_VEC3F(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
-    node->scale = ASC_VEC3F_1;
-    asc_mat4f_unit(node->rotation);
+    asc_scene_node_init(node,
+        ASC_SCENE_NODE_FUNCS(asc_text),
+        .name = args.name,
+        .render_group = ASC_RENDER_GROUP_2D_BLEND,
+        .pos2d = ASC_VEC2I(args.x, args.y)
+    );
 
     // text properties
     node->flags = args.alignment; // use flags variable to save some space
@@ -171,6 +173,7 @@
     // mesh will be created in the update func
     text->texture = cxMallocDefault(sizeof(AscTexture));
     asc_texture_init_rectangle(text->texture, 1);
+    // TODO: check why we can't just wait for the first normal update to happen
     asc_text_update(node);
 
     return node;

mercurial