src/text.c

Wed, 10 Apr 2024 19:29:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Apr 2024 19:29:09 +0200
changeset 54
cb94d983e3dd
parent 53
19faf91d43d7
child 57
7daec2f067a9
permissions
-rw-r--r--

remove check for alpha == zero because that will almost never happen

     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 <GL/glew.h>
    35 static void asc_text_draw(AscText const *node) {
    36     // Obtain shader
    37     AscShaderSprite *shader = &asc_context.active_window->glctx.shader.sprite;
    39     // Upload model matrix
    40     glUniformMatrix4fv(shader->base.model, 1,
    41                        GL_FALSE, node->base.world_transform);
    43     // Bind texture
    44     asc_texture_bind(&node->tex, shader->tex, 0);
    46     // Apply depth
    47     glUniform1f(shader->depth, (float)(node->base.depth));
    49     // Draw mesh
    50     asc_primitives_draw_plane();
    51 }
    53 static void asc_text_update(AscText *node) {
    54     // Generate new texture, if required
    55     if (asc_texture_uninitialized(&node->tex)) {
    56         asc_texture_init_rectangle(&node->tex);
    57     }
    59     // Render text onto a surface
    60     SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
    61             asc_font_cache_validate(node->font)->ptr,
    62             node->text,
    63             asc_col_sdl(node->color),
    64             node->max_width
    65     );
    66     if (surface == NULL) {
    67         asc_error(SDL_GetError());
    68         return;
    69     }
    70     asc_set_scale2d(&node->base, surface->w, surface->h);
    71     asc_update_transform((AscSceneNode *) node);
    73     // Transfer Image Data
    74     asc_texture_from_surface(&node->tex, surface);
    76     // Free the surface
    77     SDL_FreeSurface(surface);
    78 }
    80 AscSceneNode *asc_text(int x, int y, char const *text) {
    81     AscText *node = calloc(1, sizeof(AscText));
    83     node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND;
    84     node->base.free_func = (asc_scene_free_func) asc_text_free;
    85     node->base.update_func = (asc_scene_update_func) asc_text_update;
    86     node->base.draw_func = (asc_scene_draw_func) asc_text_draw;
    88     node->base.position.x = (float) x;
    89     node->base.position.y = (float) y;
    90     node->font = asc_context.active_font;
    91     node->color = asc_context.ink;
    92     if (text != NULL) {
    93         node->text = strdup(text);
    94     }
    96     // initialize
    97     asc_text_update(node);
    99     return &node->base;
   100 }
   102 void asc_text_free(AscText *node) {
   103     asc_texture_destroy(&node->tex);
   104     free(node->text);
   105     free(node);
   106 }

mercurial