src/text.c

Mon, 18 Dec 2023 19:05:30 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 19:05:30 +0100
changeset 22
30b12e88fd84
parent 19
d0e88022e209
child 23
ab07757004b4
permissions
-rw-r--r--

use new cxBufferReset function

3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 * Copyright 2023 Mike Becker. All rights reserved.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * modification, are permitted provided that the following conditions are met:
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * POSSIBILITY OF SUCH DAMAGE.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 */
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
28 #include "ascension/text.h"
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
29 #include "ascension/context.h"
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
30 #include "ascension/error.h"
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
31 #include "ascension/shader.h"
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
32
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
33 #include <GL/glew.h>
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
35 AscTextNode *asc_text(int x, int y, char const *text) {
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
36 AscTextNode *node = calloc(1, sizeof(AscTextNode));
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
37 if (node == NULL) {
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
38 asc_error("Out of memory.");
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
39 return NULL;
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
40 }
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
41
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
42 node->position = (asc_vec2i) {x, y};
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
43 node->font = asc_context.active_font;
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
44 node->color = asc_context.ink;
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
45 cxBufferInit(&node->text, NULL, strlen(text)+8,
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
46 cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
47 cxBufferPutString(&node->text, text);
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
48
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
49 return node;
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
50 }
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
51
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
52 void asc_text_update(AscTextNode *node) {
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
53 // short circuit if fully transparent or hidden, we don't need anything
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
54 if (node->color.alpha == 0 || node->hidden) {
17
25013a35e07d fix text rendering for alpha==0
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
55 return;
25013a35e07d fix text rendering for alpha==0
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
56 }
25013a35e07d fix text rendering for alpha==0
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
57
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
58 // Generate new texture, if required
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
59 if (node->internal.tex_id == 0) {
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
60 glGenTextures(1, &node->internal.tex_id);
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
61 glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
62 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
63 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
64 asc_dprintf("Generated new texture for text node: %u", node->internal.tex_id);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
65 }
4
b7acda6a4476 add simple color data types
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
66
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
67 // ensure the text is zero-terminated
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
68 CxBuffer* text = &(node->text);
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
69 cxBufferMinimumCapacity(text, text->size+1);
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
70 text->space[text->size] = '\0';
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
71
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
72 // Render text onto a surface
18
00c0632f0f40 fix missing UTF8 support for text rendering
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
73 SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
74 asc_font_cache_validate(node->font)->ptr,
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
75 text->space,
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
76 asc_col_sdl(node->color),
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
77 node->max_width
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
78 );
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
79 if (surface == NULL) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
80 asc_error(SDL_GetError());
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
81 return;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
82 }
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
84 // Store basic node information
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
85 node->position = node->position;
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
86 node->internal.dimension.width = surface->w;
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
87 node->internal.dimension.height = surface->h;
4
b7acda6a4476 add simple color data types
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
88
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
89 // Transfer Image Data
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
90 // TODO: move the image data transfer to a separate function - we will need it more often
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
91 glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
92 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
93 glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
94 surface->w, surface->h,
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
95 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
96
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
97 // Free the surface
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
98 SDL_FreeSurface(surface);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
99 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
100
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
101 void asc_text_draw(AscTextNode *node) {
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
102 if (node->color.alpha == 0 || node->hidden) {
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
103 return;
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
104 }
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
105
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
106 if (node->internal.tex_id == 0) {
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
107 asc_error("Tried to redraw text node after destruction");
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
108 return;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
109 }
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
110
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
111 glUseProgram(ASC_SHADER_FONT.base.id);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
112
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
113 // Upload projection
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
114 // TODO: when we group UI draw calls, we don't need this
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
115 glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
116 GL_FALSE, asc_context.active_window->projection);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
117
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
118 // Upload model matrix
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
119 asc_mat4f model = {0};
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
120 model[asc_mat4_index(0, 0)] = node->internal.dimension.width;
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
121 model[asc_mat4_index(1, 1)] = node->internal.dimension.height;
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
122 model[asc_mat4_index(3, 0)] = node->position.x;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
123 model[asc_mat4_index(3, 1)] = node->position.y;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
124 model[asc_mat4_index(3, 3)] = 1;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
125 glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model);
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
126
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
127 // Upload surface
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
128 glActiveTexture(GL_TEXTURE0);
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
129 glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
130 glUniform1i(ASC_SHADER_FONT.surface, 0);
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
132 // Draw mesh
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
133 asc_primitives_draw_plane();
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
134 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
135
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
136 void asc_text_free(AscTextNode *node) {
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
137 asc_dprintf("Release text node texture: %u", node->internal.tex_id);
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
138 glDeleteTextures(1, &node->internal.tex_id);
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
139 cxBufferDestroy(&node->text);
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
140 free(node);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
141 }

mercurial