Thu, 21 Mar 2024 20:34:33 +0100
minor improvements
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/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 if (node->color.alpha == 0 || node->hidden || node->tex_id == 0) {
37 return;
38 }
40 // Upload model matrix
41 glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1,
42 GL_FALSE, node->base.final_transform);
44 // Upload surface
45 glActiveTexture(GL_TEXTURE0);
46 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
47 glUniform1i(ASC_SHADER_FONT.surface, 0);
49 // Draw mesh
50 asc_primitives_draw_plane();
51 }
53 static void asc_text_update_transform(AscText *node) {
54 asc_transform_scale2i(node->base.local_transform, node->dimension);
55 asc_transform_translate2i(node->base.world_transform, node->position);
56 }
58 static void asc_text_update(AscText *node) {
59 // short circuit if fully transparent or hidden, we don't need anything
60 if (node->color.alpha == 0 || node->hidden) {
61 return;
62 }
64 // Generate new texture, if required
65 if (node->tex_id == 0) {
66 glGenTextures(1, &node->tex_id);
67 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
68 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
69 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
70 asc_dprintf("Generated new texture for text node: %u", node->tex_id);
71 }
73 // Render text onto a surface
74 SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
75 asc_font_cache_validate(node->font)->ptr,
76 node->text,
77 asc_col_sdl(node->color),
78 node->max_width
79 );
80 if (surface == NULL) {
81 asc_error(SDL_GetError());
82 return;
83 }
84 node->dimension.width = surface->w;
85 node->dimension.height = surface->h;
86 asc_node_update_transform(node);
88 // Transfer Image Data
89 // TODO: move the image data transfer to a separate function - we will need it more often
90 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
91 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel);
92 glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
93 surface->w, surface->h,
94 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
96 // Free the surface
97 SDL_FreeSurface(surface);
98 }
100 AscSceneNode *asc_text(int x, int y, char const *text) {
101 AscText *node = calloc(1, sizeof(AscText));
102 if (node == NULL) {
103 asc_error("Out of memory.");
104 return NULL;
105 }
107 node->base.render_group = ASC_RENDER_GROUP_FONTS;
108 node->base.free_func = (asc_scene_free_func) asc_text_free;
109 node->base.update_func = (asc_scene_update_func) asc_text_update;
110 node->base.transform_update_func = (asc_scene_update_func) asc_text_update_transform;
111 node->base.draw_func = (asc_scene_draw_func) asc_text_draw;
113 node->position.x = x;
114 node->position.y = y;
115 node->font = asc_context.active_font;
116 node->color = asc_context.ink;
117 if (text != NULL) {
118 node->text = strdup(text);
119 }
121 // initialize
122 asc_text_update(node);
123 asc_text_update_transform(node);
125 return &node->base;
126 }
128 void asc_text_free(AscText *node) {
129 asc_dprintf("Release text node texture: %u", node->tex_id);
130 glDeleteTextures(1, &node->tex_id);
131 free(node->text);
132 free(node);
133 }