diff -r 8324037e0148 -r 86468a71dd73 src/text.c --- a/src/text.c Mon Feb 26 21:16:00 2024 +0100 +++ b/src/text.c Mon Mar 04 21:16:46 2024 +0100 @@ -37,21 +37,15 @@ return; } + // TODO: when we group draw calls, we don't need to activate shader here glUseProgram(ASC_SHADER_FONT.base.id); - // Upload projection - // TODO: when we group UI draw calls, we don't need this + // TODO: when we group UI draw calls, we don't need to upload matrices here + // Upload matrices glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, GL_FALSE, asc_context.active_window->projection); - - // Upload model matrix - asc_mat4f model = {0}; - model[asc_mat4_index(0, 0)] = node->internal.dimension.width; - model[asc_mat4_index(1, 1)] = node->internal.dimension.height; - model[asc_mat4_index(3, 0)] = node->position.x; - model[asc_mat4_index(3, 1)] = node->position.y; - model[asc_mat4_index(3, 3)] = 1; - glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model); + glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, + GL_FALSE, node->base.transform); // Upload surface glActiveTexture(GL_TEXTURE0); @@ -62,27 +56,7 @@ asc_primitives_draw_plane(); } -AscText *asc_text(int x, int y, char const *text) { - AscText *node = calloc(1, sizeof(AscText)); - if (node == NULL) { - asc_error("Out of memory."); - return NULL; - } - - node->node.free_func = (asc_scene_free_func) asc_text_free; - node->node.draw_func = (asc_scene_draw_func) asc_text_draw; - - node->position = (asc_vec2i) {x, y}; - node->font = asc_context.active_font; - node->color = asc_context.ink; - if (text != NULL) { - node->text = strdup(text); - } - - return node; -} - -void asc_text_update(AscText *node) { +static void asc_text_update(AscText *node) { // short circuit if fully transparent or hidden, we don't need anything if (node->color.alpha == 0 || node->hidden) { return; @@ -109,10 +83,9 @@ return; } - // Store basic node information - node->position = node->position; - node->internal.dimension.width = surface->w; - node->internal.dimension.height = surface->h; + // Transform + asc_transform_scale(node->base.transform, (float) surface->w, (float) surface->h, 0); + asc_transform_translate2i(node->base.transform, node->position); // Transfer Image Data // TODO: move the image data transfer to a separate function - we will need it more often @@ -126,6 +99,28 @@ SDL_FreeSurface(surface); } +AscText *asc_text(int x, int y, char const *text) { + AscText *node = calloc(1, sizeof(AscText)); + if (node == NULL) { + asc_error("Out of memory."); + return NULL; + } + + 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; + + node->position.x = x; + node->position.y = y; + node->font = asc_context.active_font; + node->color = asc_context.ink; + if (text != NULL) { + node->text = strdup(text); + } + + return node; +} + void asc_text_free(AscText *node) { asc_dprintf("Release text node texture: %u", node->internal.tex_id); glDeleteTextures(1, &node->internal.tex_id);