Thu, 18 Jan 2024 21:53:16 +0100
don't force the use of CxBuffer in AscTextNode
3 | 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 | */ | |
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 | 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 | 34 | |
19 | 35 | AscTextNode *asc_text(int x, int y, char const *text) { |
36 | AscTextNode *node = calloc(1, sizeof(AscTextNode)); | |
37 | if (node == NULL) { | |
38 | asc_error("Out of memory."); | |
39 | return NULL; | |
40 | } | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
41 | |
19 | 42 | node->position = (asc_vec2i) {x, y}; |
43 | node->font = asc_context.active_font; | |
44 | node->color = asc_context.ink; | |
23
ab07757004b4
don't force the use of CxBuffer in AscTextNode
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
45 | node->text = strdup(text); |
19 | 46 | |
47 | return node; | |
48 | } | |
49 | ||
50 | void asc_text_update(AscTextNode *node) { | |
51 | // short circuit if fully transparent or hidden, we don't need anything | |
52 | 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
|
53 | return; |
25013a35e07d
fix text rendering for alpha==0
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
54 | } |
25013a35e07d
fix text rendering for alpha==0
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
55 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
56 | // Generate new texture, if required |
19 | 57 | if (node->internal.tex_id == 0) { |
58 | glGenTextures(1, &node->internal.tex_id); | |
59 | 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
|
60 | 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
|
61 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
19 | 62 | 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
|
63 | } |
4
b7acda6a4476
add simple color data types
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
64 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
65 | // Render text onto a surface |
18
00c0632f0f40
fix missing UTF8 support for text rendering
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
66 | SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped( |
19 | 67 | 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
|
68 | node->text, |
19 | 69 | asc_col_sdl(node->color), |
70 | node->max_width | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
71 | ); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
72 | if (surface == NULL) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
73 | asc_error(SDL_GetError()); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
74 | return; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
75 | } |
3 | 76 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
77 | // Store basic node information |
19 | 78 | node->position = node->position; |
79 | node->internal.dimension.width = surface->w; | |
80 | node->internal.dimension.height = surface->h; | |
4
b7acda6a4476
add simple color data types
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
81 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
82 | // Transfer Image Data |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
83 | // TODO: move the image data transfer to a separate function - we will need it more often |
19 | 84 | 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
|
85 | 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
|
86 | 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
|
87 | surface->w, surface->h, |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
88 | 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
|
89 | |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
90 | // Free the surface |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
91 | SDL_FreeSurface(surface); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
92 | } |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
93 | |
19 | 94 | void asc_text_draw(AscTextNode *node) { |
95 | if (node->color.alpha == 0 || node->hidden) { | |
96 | return; | |
97 | } | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
98 | |
19 | 99 | 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
|
100 | 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
|
101 | return; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
102 | } |
11 | 103 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
104 | glUseProgram(ASC_SHADER_FONT.base.id); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
105 | |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
106 | // Upload projection |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
107 | // TODO: when we group UI draw calls, we don't need this |
19 | 108 | glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, |
109 | 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
|
110 | |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
111 | // Upload model matrix |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
112 | asc_mat4f model = {0}; |
19 | 113 | model[asc_mat4_index(0, 0)] = node->internal.dimension.width; |
114 | 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
|
115 | 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
|
116 | 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
|
117 | 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
|
118 | glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model); |
11 | 119 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
120 | // Upload surface |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
121 | glActiveTexture(GL_TEXTURE0); |
19 | 122 | 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
|
123 | glUniform1i(ASC_SHADER_FONT.surface, 0); |
3 | 124 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
125 | // Draw mesh |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
126 | asc_primitives_draw_plane(); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
127 | } |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
128 | |
19 | 129 | void asc_text_free(AscTextNode *node) { |
130 | asc_dprintf("Release text node texture: %u", node->internal.tex_id); | |
131 | glDeleteTextures(1, &node->internal.tex_id); | |
23
ab07757004b4
don't force the use of CxBuffer in AscTextNode
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
132 | free(node->text); |
19 | 133 | free(node); |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
134 | } |