src/text.c

changeset 116
bfb2a7d62047
parent 115
e5f8c99b0987
--- a/src/text.c	Sat May 10 18:51:45 2025 +0200
+++ b/src/text.c	Sun May 11 14:51:00 2025 +0200
@@ -54,20 +54,27 @@
         asc_error("Rendering TTF surface failed: %s", SDL_GetError());
         return;
     }
-    // TODO: don't use scale - create an own fitting mesh
-    asc_set_scale(node, (float)surface->w, (float)surface->h, 1.f);
     if (asc_test_flag(text->base.data.flags, ASC_TEXT_CENTERED_FLAG)) {
         unsigned short newoffx = surface->w / 2;
-        asc_vec2i pos = asc_get_position2d(node);
-        asc_set_position2d(node, pos.x + text->offx - newoffx, pos.y);
+        node->position.x = node->position.x + (float)(text->offx - newoffx);
         text->offx = newoffx;
     }
 
+    // If dimensions changed, update the mesh
+    if (text->dimension.x != (unsigned)surface->w || text->dimension.y != (unsigned)surface->h) {
+        text->dimension.x = surface->w;
+        text->dimension.y = surface->h;
+        asc_mesh_plane_2d(&text->base.mesh, .size = asc_vec2f_new(surface->w, surface->h));
+    }
+
     // Transfer Image Data
     asc_texture_from_surface(sprite->texture, surface);
 
     // Free the surface
     SDL_FreeSurface(surface);
+
+    // Schedule for transform update
+    asc_node_update_transform(node);
 }
 
 static void asc_text_destroy(AscSceneNode *node) {
@@ -90,7 +97,7 @@
     node->destroy_func = asc_text_destroy;
     node->update_func = asc_text_update;
     node->position = asc_vec3f_new(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
-    node->scale.depth = 1.f;
+    node->scale = asc_vec3f_one;
 
     // text properties
     node->flags = args.alignment; // use flags variable to save some space
@@ -103,8 +110,8 @@
         text->text = cx_mutstr(strdup(args.text));
     }
 
-    // initialize mesh and texture
-    asc_mesh_plane_2d(&text->base.mesh);
+    // initialize texture
+    // mesh will be created in the update func
     text->base.texture = malloc(sizeof(AscTexture));
     asc_texture_init_rectangle(text->base.texture, 1);
     asc_text_update(node);

mercurial