src/text.c

Thu, 18 Apr 2024 22:53:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Apr 2024 22:53:55 +0200
changeset 65
9c44c55d327a
parent 64
f18dc427f86f
child 66
8297afa1c29c
permissions
-rw-r--r--

consistently refer to windows by ID - fixes #381

This change discovered that the font cache is completely broken. We created issue #387 for this.

universe@3 1 /*
universe@3 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@3 3 * Copyright 2023 Mike Becker. All rights reserved.
universe@3 4 *
universe@3 5 * Redistribution and use in source and binary forms, with or without
universe@3 6 * modification, are permitted provided that the following conditions are met:
universe@3 7 *
universe@3 8 * 1. Redistributions of source code must retain the above copyright
universe@3 9 * notice, this list of conditions and the following disclaimer.
universe@3 10 *
universe@3 11 * 2. Redistributions in binary form must reproduce the above copyright
universe@3 12 * notice, this list of conditions and the following disclaimer in the
universe@3 13 * documentation and/or other materials provided with the distribution.
universe@3 14 *
universe@3 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@3 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@3 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@3 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@3 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@3 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@3 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@3 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@3 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@3 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@3 25 * POSSIBILITY OF SUCH DAMAGE.
universe@3 26 */
universe@3 27
universe@48 28 #include "ascension/ui/text.h"
universe@16 29 #include "ascension/context.h"
universe@16 30 #include "ascension/error.h"
universe@16 31 #include "ascension/shader.h"
universe@3 32
universe@58 33 #include <cx/printf.h>
universe@58 34
universe@16 35 #include <GL/glew.h>
universe@11 36
universe@29 37 static void asc_text_draw(AscText const *node) {
universe@44 38 // Obtain shader
universe@65 39 AscShaderSprite *shader = &asc_active_window->glctx.shader.sprite;
universe@44 40
universe@37 41 // Upload model matrix
universe@44 42 glUniformMatrix4fv(shader->base.model, 1,
universe@45 43 GL_FALSE, node->base.world_transform);
universe@29 44
universe@50 45 // Bind texture
universe@50 46 asc_texture_bind(&node->tex, shader->tex, 0);
universe@29 47
universe@41 48 // Apply depth
universe@44 49 glUniform1f(shader->depth, (float)(node->base.depth));
universe@41 50
universe@29 51 // Draw mesh
universe@29 52 asc_primitives_draw_plane();
universe@29 53 }
universe@29 54
universe@32 55 static void asc_text_update(AscText *node) {
universe@16 56 // Generate new texture, if required
universe@50 57 if (asc_texture_uninitialized(&node->tex)) {
universe@50 58 asc_texture_init_rectangle(&node->tex);
universe@16 59 }
universe@4 60
universe@16 61 // Render text onto a surface
universe@65 62 // TODO: obtain TTF_Font from font cache once it is repaired
universe@65 63 TTF_Font *font = node->font.ptr;
universe@58 64 static int alignments[] = {
universe@58 65 TTF_WRAPPED_ALIGN_LEFT,
universe@58 66 TTF_WRAPPED_ALIGN_CENTER,
universe@58 67 TTF_WRAPPED_ALIGN_RIGHT
universe@58 68 };
universe@57 69 TTF_SetFontWrappedAlign(
universe@58 70 font, alignments[node->base.flags & ASC_TEXT_ALIGNMENT_MASK]);
universe@18 71 SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
universe@58 72 font, node->text.ptr, asc_col_sdl(node->color), node->max_width
universe@16 73 );
universe@16 74 if (surface == NULL) {
universe@16 75 asc_error(SDL_GetError());
universe@16 76 return;
universe@16 77 }
universe@52 78 asc_set_scale2d(&node->base, surface->w, surface->h);
universe@58 79 if (asc_test_flag(node->base.flags, ASC_TEXT_CENTERED_FLAG)) {
universe@58 80 unsigned short newoffx = surface->w / 2;
universe@58 81 asc_vec2i pos = asc_get_position2d(&node->base);
universe@58 82 asc_set_position2d(&node->base, pos.x + node->offx - newoffx, pos.y);
universe@58 83 node->offx = newoffx;
universe@58 84 }
universe@11 85
universe@16 86 // Transfer Image Data
universe@50 87 asc_texture_from_surface(&node->tex, surface);
universe@11 88
universe@16 89 // Free the surface
universe@16 90 SDL_FreeSurface(surface);
universe@16 91 }
universe@16 92
universe@59 93 AscSceneNode *asc_text_create(struct asc_text_create_args args) {
universe@32 94 AscText *node = calloc(1, sizeof(AscText));
universe@32 95
universe@41 96 node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND;
universe@32 97 node->base.free_func = (asc_scene_free_func) asc_text_free;
universe@32 98 node->base.update_func = (asc_scene_update_func) asc_text_update;
universe@32 99 node->base.draw_func = (asc_scene_draw_func) asc_text_draw;
universe@32 100
universe@59 101 node->base.flags = args.alignment;
universe@59 102 node->base.position.x = (float) args.x;
universe@59 103 node->base.position.y = (float) args.y;
universe@59 104 node->max_width = args.max_width;
universe@65 105 node->font = *asc_active_font;
universe@32 106 node->color = asc_context.ink;
universe@59 107 if (args.text == NULL) {
universe@58 108 node->text = cx_mutstr(strdup(" "));
universe@58 109 } else {
universe@59 110 node->text = cx_mutstr(strdup(args.text));
universe@32 111 }
universe@32 112
universe@37 113 // initialize
universe@37 114 asc_text_update(node);
universe@37 115
universe@36 116 return &node->base;
universe@32 117 }
universe@32 118
universe@25 119 void asc_text_free(AscText *node) {
universe@50 120 asc_texture_destroy(&node->tex);
universe@58 121 cx_strfree(&node->text);
universe@19 122 free(node);
universe@58 123 }
universe@58 124
universe@58 125 void asc_text_printf(
universe@58 126 AscSceneNode *node,
universe@58 127 char const* format,
universe@58 128 ...
universe@58 129 ) {
universe@58 130 cxmutstr text = ((AscText*)node)->text;
universe@58 131 va_list ap;
universe@58 132 va_start(ap, format);
universe@58 133 cx_vsprintf(&text.ptr, &text.length, format, ap);
universe@58 134 va_end(ap);
universe@58 135 asc_node_update(node);
universe@58 136 }

mercurial