src/text.c

Tue, 09 Apr 2024 21:18:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 09 Apr 2024 21:18:52 +0200
changeset 50
d8d2e4817db1
parent 48
6e5b5ba2752c
child 52
6d100092fcd4
permissions
-rw-r--r--

add texture.h

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright 2023 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "ascension/ui/text.h"
#include "ascension/context.h"
#include "ascension/error.h"
#include "ascension/shader.h"

#include <GL/glew.h>

static void asc_text_draw(AscText const *node) {
    if (node->color.alpha == 0 || node->hidden) {
        return;
    }

    // Obtain shader
    AscShaderSprite *shader = &asc_context.active_window->glctx.shader.sprite;

    // Upload model matrix
    glUniformMatrix4fv(shader->base.model, 1,
                       GL_FALSE, node->base.world_transform);

    // Bind texture
    asc_texture_bind(&node->tex, shader->tex, 0);

    // Apply depth
    glUniform1f(shader->depth, (float)(node->base.depth));

    // Draw mesh
    asc_primitives_draw_plane();
}

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;
    }

    // Generate new texture, if required
    if (asc_texture_uninitialized(&node->tex)) {
        asc_texture_init_rectangle(&node->tex);
    }

    // Render text onto a surface
    SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
            asc_font_cache_validate(node->font)->ptr,
            node->text,
            asc_col_sdl(node->color),
            node->max_width
    );
    if (surface == NULL) {
        asc_error(SDL_GetError());
        return;
    }
    node->base.scale.width = (float) surface->w;
    node->base.scale.height = (float) surface->h;
    asc_update_transform((AscSceneNode *) node);

    // Transfer Image Data
    asc_texture_from_surface(&node->tex, surface);

    // Free the surface
    SDL_FreeSurface(surface);
}

AscSceneNode *asc_text(int x, int y, char const *text) {
    AscText *node = calloc(1, sizeof(AscText));

    node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND;
    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->base.position.x = (float) x;
    node->base.position.y = (float) y;
    node->font = asc_context.active_font;
    node->color = asc_context.ink;
    if (text != NULL) {
        node->text = strdup(text);
    }

    // initialize
    asc_text_update(node);

    return &node->base;
}

void asc_text_free(AscText *node) {
    asc_texture_destroy(&node->tex);
    free(node->text);
    free(node);
}

mercurial