src/text.c

changeset 32
86468a71dd73
parent 29
1d001eb694dc
child 33
e7ddb52facd3
     1.1 --- a/src/text.c	Mon Feb 26 21:16:00 2024 +0100
     1.2 +++ b/src/text.c	Mon Mar 04 21:16:46 2024 +0100
     1.3 @@ -37,21 +37,15 @@
     1.4          return;
     1.5      }
     1.6  
     1.7 +    // TODO: when we group draw calls, we don't need to activate shader here
     1.8      glUseProgram(ASC_SHADER_FONT.base.id);
     1.9  
    1.10 -    // Upload projection
    1.11 -    // TODO: when we group UI draw calls, we don't need this
    1.12 +    // TODO: when we group UI draw calls, we don't need to upload matrices here
    1.13 +    // Upload matrices
    1.14      glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
    1.15                         GL_FALSE, asc_context.active_window->projection);
    1.16 -
    1.17 -    // Upload model matrix
    1.18 -    asc_mat4f model = {0};
    1.19 -    model[asc_mat4_index(0, 0)] = node->internal.dimension.width;
    1.20 -    model[asc_mat4_index(1, 1)] = node->internal.dimension.height;
    1.21 -    model[asc_mat4_index(3, 0)] = node->position.x;
    1.22 -    model[asc_mat4_index(3, 1)] = node->position.y;
    1.23 -    model[asc_mat4_index(3, 3)] = 1;
    1.24 -    glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model);
    1.25 +    glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1,
    1.26 +                       GL_FALSE, node->base.transform);
    1.27  
    1.28      // Upload surface
    1.29      glActiveTexture(GL_TEXTURE0);
    1.30 @@ -62,27 +56,7 @@
    1.31      asc_primitives_draw_plane();
    1.32  }
    1.33  
    1.34 -AscText *asc_text(int x, int y, char const *text) {
    1.35 -    AscText *node = calloc(1, sizeof(AscText));
    1.36 -    if (node == NULL) {
    1.37 -        asc_error("Out of memory.");
    1.38 -        return NULL;
    1.39 -    }
    1.40 -
    1.41 -    node->node.free_func = (asc_scene_free_func) asc_text_free;
    1.42 -    node->node.draw_func = (asc_scene_draw_func) asc_text_draw;
    1.43 -
    1.44 -    node->position = (asc_vec2i) {x, y};
    1.45 -    node->font = asc_context.active_font;
    1.46 -    node->color = asc_context.ink;
    1.47 -    if (text != NULL) {
    1.48 -        node->text = strdup(text);
    1.49 -    }
    1.50 -
    1.51 -    return node;
    1.52 -}
    1.53 -
    1.54 -void asc_text_update(AscText *node) {
    1.55 +static void asc_text_update(AscText *node) {
    1.56      // short circuit if fully transparent or hidden, we don't need anything
    1.57      if (node->color.alpha == 0 || node->hidden) {
    1.58          return;
    1.59 @@ -109,10 +83,9 @@
    1.60          return;
    1.61      }
    1.62  
    1.63 -    // Store basic node information
    1.64 -    node->position = node->position;
    1.65 -    node->internal.dimension.width = surface->w;
    1.66 -    node->internal.dimension.height = surface->h;
    1.67 +    // Transform
    1.68 +    asc_transform_scale(node->base.transform, (float) surface->w, (float) surface->h, 0);
    1.69 +    asc_transform_translate2i(node->base.transform, node->position);
    1.70  
    1.71      // Transfer Image Data
    1.72      // TODO: move the image data transfer to a separate function - we will need it more often
    1.73 @@ -126,6 +99,28 @@
    1.74      SDL_FreeSurface(surface);
    1.75  }
    1.76  
    1.77 +AscText *asc_text(int x, int y, char const *text) {
    1.78 +    AscText *node = calloc(1, sizeof(AscText));
    1.79 +    if (node == NULL) {
    1.80 +        asc_error("Out of memory.");
    1.81 +        return NULL;
    1.82 +    }
    1.83 +
    1.84 +    node->base.free_func = (asc_scene_free_func) asc_text_free;
    1.85 +    node->base.update_func = (asc_scene_update_func) asc_text_update;
    1.86 +    node->base.draw_func = (asc_scene_draw_func) asc_text_draw;
    1.87 +
    1.88 +    node->position.x = x;
    1.89 +    node->position.y = y;
    1.90 +    node->font = asc_context.active_font;
    1.91 +    node->color = asc_context.ink;
    1.92 +    if (text != NULL) {
    1.93 +        node->text = strdup(text);
    1.94 +    }
    1.95 +
    1.96 +    return node;
    1.97 +}
    1.98 +
    1.99  void asc_text_free(AscText *node) {
   1.100      asc_dprintf("Release text node texture: %u", node->internal.tex_id);
   1.101      glDeleteTextures(1, &node->internal.tex_id);

mercurial