Thu, 22 Feb 2024 22:34:50 +0100
apply new CxTreeIterator from ucx 3.1 preview
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->internal.tex_id == 0) {
37 return;
38 }
40 glUseProgram(ASC_SHADER_FONT.base.id);
42 // Upload projection
43 // TODO: when we group UI draw calls, we don't need this
44 glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
45 GL_FALSE, asc_context.active_window->projection);
47 // Upload model matrix
48 asc_mat4f model = {0};
49 model[asc_mat4_index(0, 0)] = node->internal.dimension.width;
50 model[asc_mat4_index(1, 1)] = node->internal.dimension.height;
51 model[asc_mat4_index(3, 0)] = node->position.x;
52 model[asc_mat4_index(3, 1)] = node->position.y;
53 model[asc_mat4_index(3, 3)] = 1;
54 glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model);
56 // Upload surface
57 glActiveTexture(GL_TEXTURE0);
58 glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
59 glUniform1i(ASC_SHADER_FONT.surface, 0);
61 // Draw mesh
62 asc_primitives_draw_plane();
63 }
65 AscText *asc_text(int x, int y, char const *text) {
66 AscText *node = calloc(1, sizeof(AscText));
67 if (node == NULL) {
68 asc_error("Out of memory.");
69 return NULL;
70 }
72 node->node.free_func = (asc_scene_free_func) asc_text_free;
73 node->node.draw_func = (asc_scene_draw_func) asc_text_draw;
75 node->position = (asc_vec2i) {x, y};
76 node->font = asc_context.active_font;
77 node->color = asc_context.ink;
78 if (text != NULL) {
79 node->text = strdup(text);
80 }
82 return node;
83 }
85 void asc_text_update(AscText *node) {
86 // short circuit if fully transparent or hidden, we don't need anything
87 if (node->color.alpha == 0 || node->hidden) {
88 return;
89 }
91 // Generate new texture, if required
92 if (node->internal.tex_id == 0) {
93 glGenTextures(1, &node->internal.tex_id);
94 glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
95 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
96 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
97 asc_dprintf("Generated new texture for text node: %u", node->internal.tex_id);
98 }
100 // Render text onto a surface
101 SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
102 asc_font_cache_validate(node->font)->ptr,
103 node->text,
104 asc_col_sdl(node->color),
105 node->max_width
106 );
107 if (surface == NULL) {
108 asc_error(SDL_GetError());
109 return;
110 }
112 // Store basic node information
113 node->position = node->position;
114 node->internal.dimension.width = surface->w;
115 node->internal.dimension.height = surface->h;
117 // Transfer Image Data
118 // TODO: move the image data transfer to a separate function - we will need it more often
119 glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
120 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel);
121 glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
122 surface->w, surface->h,
123 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
125 // Free the surface
126 SDL_FreeSurface(surface);
127 }
129 void asc_text_free(AscText *node) {
130 asc_dprintf("Release text node texture: %u", node->internal.tex_id);
131 glDeleteTextures(1, &node->internal.tex_id);
132 free(node->text);
133 free(node);
134 }