src/text.c

Wed, 06 Mar 2024 23:13:06 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Mar 2024 23:13:06 +0100
changeset 35
674eb53c12f3
parent 33
e7ddb52facd3
child 36
e26b4ac1661c
permissions
-rw-r--r--

fix name of demo binary

/*
 * 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/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 || node->internal.tex_id == 0) {
        return;
    }

    // TODO: when we group draw calls, we don't need to activate shader here
    glUseProgram(ASC_SHADER_FONT.base.id);

    // TODO: when we group UI draw calls, we don't need to upload matrices here
    // Upload matrices
    glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
                       GL_FALSE, asc_context.active_window->projection);
    glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1,
                       GL_FALSE, node->base.transform);

    // Upload surface
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
    glUniform1i(ASC_SHADER_FONT.surface, 0);

    // 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 (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->internal.tex_id);
    }

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

    // Transform
    asc_transform_scale(node->base.transform, (float) surface->w, (float) surface->h, 0);
    asc_transform_translate2i(node->base.transform, node->position);

    // Transfer Image Data
    // TODO: move the image data transfer to a separate function - we will need it more often
    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,
                 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);

    // Free the surface
    SDL_FreeSurface(surface);
}

AscText *asc_text(int x, int y, char const *text) {
    AscText *node = calloc(1, sizeof(AscText));
    if (node == NULL) {
        asc_error("Out of memory.");
        return NULL;
    }

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

    asc_scene_add(&asc_context.active_window->ui, &node->base);

    return node;
}

void asc_text_free(AscText *node) {
    asc_dprintf("Release text node texture: %u", node->internal.tex_id);
    glDeleteTextures(1, &node->internal.tex_id);
    free(node->text);
    free(node);
}

mercurial