src/text.c

changeset 71
baa73a0be3ce
parent 66
8297afa1c29c
--- a/src/text.c	Sun Aug 11 16:29:09 2024 +0200
+++ b/src/text.c	Wed Aug 14 21:09:52 2024 +0200
@@ -34,91 +34,78 @@
 
 #include <GL/glew.h>
 
-static void asc_text_draw(AscText const *node) {
-    // Obtain shader
-    AscShaderSprite *shader = &asc_active_window->glctx.shader.sprite;
-
-    // Upload model matrix
-    glUniformMatrix4fv(shader->base.model, 1,
-                       GL_FALSE, node->base.world_transform);
-
-    // Bind texture
-    asc_texture_bind(&node->tex, shader->tex, 0);
+static void asc_text_update(AscSceneNode *node) {
+    AscSprite *sprite = (AscSprite*) node;
+    AscText *text = (AscText*) node;
 
-    // Apply depth
-    glUniform1f(shader->depth, (float)(node->base.depth));
-
-    // Draw mesh
-    asc_primitives_draw_plane();
-}
-
-static void asc_text_update(AscText *node) {
     // Generate new texture, if required
-    if (asc_texture_uninitialized(&node->tex)) {
-        asc_texture_init_rectangle(&node->tex);
+    if (asc_texture_uninitialized(&sprite->tex)) {
+        asc_texture_init_rectangle(&sprite->tex);
     }
 
     // Render text onto a surface
-    TTF_Font *font = asc_font_load(node->font);
+    TTF_Font *font = asc_font_load(text->font);
     static int alignments[] = {
             TTF_WRAPPED_ALIGN_LEFT,
             TTF_WRAPPED_ALIGN_CENTER,
             TTF_WRAPPED_ALIGN_RIGHT
     };
     TTF_SetFontWrappedAlign(
-            font, alignments[node->base.flags & ASC_TEXT_ALIGNMENT_MASK]);
+            font, alignments[text->base.data.flags & ASC_TEXT_ALIGNMENT_MASK]);
     SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
-            font, node->text.ptr, asc_col_sdl(node->color), node->max_width
+            font, text->text.ptr, asc_col_sdl(text->color), text->max_width
     );
     if (surface == NULL) {
         asc_error(SDL_GetError());
         return;
     }
-    asc_set_scale2d(&node->base, surface->w, surface->h);
-    if (asc_test_flag(node->base.flags, ASC_TEXT_CENTERED_FLAG)) {
+    asc_set_scale2d(node, surface->w, surface->h);
+    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->base);
-        asc_set_position2d(&node->base, pos.x + node->offx - newoffx, pos.y);
-        node->offx = newoffx;
+        asc_vec2i pos = asc_get_position2d(node);
+        asc_set_position2d(node, pos.x + text->offx - newoffx, pos.y);
+        text->offx = newoffx;
     }
 
     // Transfer Image Data
-    asc_texture_from_surface(&node->tex, surface);
+    asc_texture_from_surface(&sprite->tex, surface);
 
     // Free the surface
     SDL_FreeSurface(surface);
 }
 
-AscSceneNode *asc_text_create(struct asc_text_create_args args) {
-    AscText *node = calloc(1, sizeof(AscText));
+static void asc_text_free(AscSceneNode *node) {
+    AscText *text = (AscText*) node;
+    AscSprite *sprite = (AscSprite*) node;
+    asc_texture_destroy(&sprite->tex);
+    cx_strfree(&text->text);
+    free(node);
+}
 
-    node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND;
-    node->base.free_func = (asc_scene_free_func) asc_text_free;
-    node->base.update_func = (asc_scene_update_func) asc_text_update;
-    node->base.draw_func = (asc_scene_draw_func) asc_text_draw;
+AscSceneNode *asc_text_create(struct asc_text_create_args args) {
+    AscText *text = calloc(1, sizeof(AscText));
+    AscSceneNode *node = (AscSceneNode*) text;
 
-    node->base.flags = args.alignment;
-    node->base.position.x = (float) args.x;
-    node->base.position.y = (float) args.y;
-    node->max_width = args.max_width;
-    node->font = asc_active_font;
-    node->color = asc_context.ink;
+    node->render_group = ASC_RENDER_GROUP_SPRITE_BLEND;
+    node->free_func = asc_text_free;
+    node->update_func = asc_text_update;
+
+    node->flags = args.alignment;
+    node->position.x = (float) args.x;
+    node->position.y = (float) args.y;
+    text->max_width = args.max_width;
+    text->font = asc_active_font;
+    text->color = asc_context.ink;
     if (args.text == NULL) {
-        node->text = cx_mutstr(strdup(" "));
+        text->text = cx_mutstr(strdup(" "));
     } else {
-        node->text = cx_mutstr(strdup(args.text));
+        text->text = cx_mutstr(strdup(args.text));
     }
 
     // initialize
     asc_text_update(node);
 
-    return &node->base;
-}
-
-void asc_text_free(AscText *node) {
-    asc_texture_destroy(&node->tex);
-    cx_strfree(&node->text);
-    free(node);
+    return node;
 }
 
 void asc_text_printf(

mercurial