src/text.c

Thu, 18 Apr 2024 21:53:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Apr 2024 21:53:53 +0200
changeset 64
f18dc427f86f
parent 59
764fbb013252
child 65
9c44c55d327a
permissions
-rw-r--r--

make use of the asc_window_active macro

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

mercurial