src/text.c

Fri, 15 Mar 2024 00:06:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 15 Mar 2024 00:06:59 +0100
changeset 37
8a8cc6725b48
parent 36
e26b4ac1661c
child 38
6e5629ea4c5c
permissions
-rw-r--r--

add camera and render groups

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
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
35 static void asc_text_draw(AscText const *node) {
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
36 if (node->color.alpha == 0 || node->hidden || node->tex_id == 0) {
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
37 return;
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
38 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
39
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
40 // Upload model matrix
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
41 glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1,
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
42 GL_FALSE, node->base.transform);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
43
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
44 // Upload surface
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
45 glActiveTexture(GL_TEXTURE0);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
46 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
47 glUniform1i(ASC_SHADER_FONT.surface, 0);
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
48
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
49 // Draw mesh
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
50 asc_primitives_draw_plane();
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
51 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
52
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
53 static void asc_text_update_transform(AscText *node) {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
54 asc_transform_scale(node->base.transform, (float) node->dimension.width, (float) node->dimension.height, 0);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
55 asc_transform_translate2i(node->base.transform, node->position);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
56 }
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
57
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
58 static void asc_text_update(AscText *node) {
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
59 // 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
60 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
61 return;
25013a35e07d fix text rendering for alpha==0
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
62 }
25013a35e07d fix text rendering for alpha==0
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
63
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
64 // Generate new texture, if required
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
65 if (node->tex_id == 0) {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
66 glGenTextures(1, &node->tex_id);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
67 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
68 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
69 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
70 asc_dprintf("Generated new texture for text node: %u", node->tex_id);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
71 }
4
b7acda6a4476 add simple color data types
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
72
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
73 // Render text onto a surface
18
00c0632f0f40 fix missing UTF8 support for text rendering
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
74 SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
75 asc_font_cache_validate(node->font)->ptr,
23
ab07757004b4 don't force the use of CxBuffer in AscTextNode
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
76 node->text,
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
77 asc_col_sdl(node->color),
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
78 node->max_width
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
79 );
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
80 if (surface == NULL) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
81 asc_error(SDL_GetError());
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
82 return;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
83 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
84 node->dimension.width = surface->w;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
85 node->dimension.height = surface->h;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
86 asc_node_update_transform(node);
4
b7acda6a4476 add simple color data types
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
87
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
88 // Transfer Image Data
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
89 // TODO: move the image data transfer to a separate function - we will need it more often
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
90 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
91 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
92 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
93 surface->w, surface->h,
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
94 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
95
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
96 // Free the surface
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
97 SDL_FreeSurface(surface);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
98 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
99
36
e26b4ac1661c invert the logic of converting between specialized nodes and the generic interface
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
100 AscSceneNode *asc_text(int x, int y, char const *text) {
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
101 AscText *node = calloc(1, sizeof(AscText));
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
102 if (node == NULL) {
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
103 asc_error("Out of memory.");
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
104 return NULL;
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
105 }
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
106
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
107 node->base.render_group = ASC_RENDER_GROUP_FONTS;
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
108 node->base.free_func = (asc_scene_free_func) asc_text_free;
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
109 node->base.update_func = (asc_scene_update_func) asc_text_update;
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
110 node->base.transform_update_func = (asc_scene_update_func) asc_text_update_transform;
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
111 node->base.draw_func = (asc_scene_draw_func) asc_text_draw;
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
112
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
113 node->position.x = x;
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
114 node->position.y = y;
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
115 node->font = asc_context.active_font;
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
116 node->color = asc_context.ink;
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
117 if (text != NULL) {
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
118 node->text = strdup(text);
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
119 }
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
120
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
121 // initialize
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
122 asc_text_update(node);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
123 asc_text_update_transform(node);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
124
36
e26b4ac1661c invert the logic of converting between specialized nodes and the generic interface
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
125 return &node->base;
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
126 }
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
127
25
601b3f4e17a2 rename AscTextNode to just AscText
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
128 void asc_text_free(AscText *node) {
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
129 asc_dprintf("Release text node texture: %u", node->tex_id);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
130 glDeleteTextures(1, &node->tex_id);
23
ab07757004b4 don't force the use of CxBuffer in AscTextNode
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
131 free(node->text);
19
d0e88022e209 improve text node API
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
132 free(node);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
133 }

mercurial