diff -r 00c0632f0f40 -r d0e88022e209 src/text.c --- a/src/text.c Sun Nov 19 13:27:08 2023 +0100 +++ b/src/text.c Thu Nov 23 23:08:57 2023 +0100 @@ -32,38 +32,49 @@ #include -void asc_text_draw_lb( - AscTextNode *node, - asc_vec2i position, - unsigned max_width, - cxmutstr text) { +AscTextNode *asc_text(int x, int y, char const *text) { + AscTextNode *node = calloc(1, sizeof(AscTextNode)); + if (node == NULL) { + asc_error("Out of memory."); + return NULL; + } - // short circuit - if fully transparent, just hide it - if (asc_context.ink.alpha == 0) { - node->hidden = true; + node->position = (asc_vec2i) {x, y}; + node->font = asc_context.active_font; + node->color = asc_context.ink; + cxBufferInit(&node->text, NULL, strlen(text)+8, + cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); + cxBufferPutString(&node->text, text); + + return node; +} + +void asc_text_update(AscTextNode *node) { + // short circuit if fully transparent or hidden, we don't need anything + if (node->color.alpha == 0 || node->hidden) { return; } // Generate new texture, if required - if (node->tex_id == 0) { - glGenTextures(1, &node->tex_id); - glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); + if (node->internal.tex_id == 0) { + glGenTextures(1, &node->internal.tex_id); + glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - asc_dprintf("Generated new texture for text node: %u", node->tex_id); + asc_dprintf("Generated new texture for text node: %u", node->internal.tex_id); } + // ensure the text is zero-terminated + CxBuffer* text = &(node->text); + cxBufferMinimumCapacity(text, text->size+1); + text->space[text->size] = '\0'; + // Render text onto a surface SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped( - asc_context.active_font->ptr, - text.ptr, - (SDL_Color) { - .r = asc_context.ink.red, - .g = asc_context.ink.green, - .b = asc_context.ink.blue, - .a = asc_context.ink.alpha - }, - max_width + asc_font_cache_validate(node->font)->ptr, + text->space, + asc_col_sdl(node->color), + node->max_width ); if (surface == NULL) { asc_error(SDL_GetError()); @@ -71,13 +82,13 @@ } // Store basic node information - node->position = position; - node->dimension.width = surface->w; - node->dimension.height = surface->h; + node->position = node->position; + node->internal.dimension.width = surface->w; + node->internal.dimension.height = surface->h; // Transfer Image Data // TODO: move the image data transfer to a separate function - we will need it more often - glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); + glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel); glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, surface->w, surface->h, @@ -85,23 +96,14 @@ // Free the surface SDL_FreeSurface(surface); - - // Redraw the node - asc_text_redraw(node); } -void asc_text_draw( - AscTextNode *node, - asc_vec2i position, - cxmutstr text) { - unsigned max_width = asc_context.active_window->dimensions.width - position.width; - asc_text_draw_lb(node, position, max_width, text); -} +void asc_text_draw(AscTextNode *node) { + if (node->color.alpha == 0 || node->hidden) { + return; + } -void asc_text_redraw(AscTextNode *node) { - if (node->hidden) return; - - if (node->tex_id == 0) { + if (node->internal.tex_id == 0) { asc_error("Tried to redraw text node after destruction"); return; } @@ -110,12 +112,13 @@ // Upload projection // TODO: when we group UI draw calls, we don't need this - glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, GL_FALSE, asc_context.active_window->projection); + 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->dimension.width; - model[asc_mat4_index(1, 1)] = node->dimension.height; + 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; @@ -123,15 +126,16 @@ // Upload surface glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); + glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); glUniform1i(ASC_SHADER_FONT.surface, 0); // Draw mesh asc_primitives_draw_plane(); } -void asc_text_destroy(AscTextNode *node) { - asc_dprintf("Release text node texture: %u", node->tex_id); - glDeleteTextures(1, &node->tex_id); - node->tex_id = 0; +void asc_text_free(AscTextNode *node) { + asc_dprintf("Release text node texture: %u", node->internal.tex_id); + glDeleteTextures(1, &node->internal.tex_id); + cxBufferDestroy(&node->text); + free(node); } \ No newline at end of file