src/text.c

changeset 16
c5dde81b6fb2
parent 14
9dbfc0031887
child 17
25013a35e07d
equal deleted inserted replaced
15:362b7659dc76 16:c5dde81b6fb2
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
28 #include "ascension/text.h"
29 #include "ascension/context.h"
30 #include "ascension/error.h"
31 #include "ascension/shader.h"
32
33 #include <GL/glew.h>
34
35 void asc_text_draw_lb(
36 AscTextNode *node,
37 asc_vec2i position,
38 unsigned max_width,
39 cxmutstr text) {
40
41 // Generate new texture, if required
42 if (node->tex_id == 0) {
43 glGenTextures(1, &node->tex_id);
44 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
45 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
46 glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
47 asc_dprintf("Generated new texture for text node: %u", node->tex_id);
48 }
49
50 // Render text onto a surface
51 SDL_Surface *surface = TTF_RenderText_Blended_Wrapped(
52 asc_context.active_font->ptr,
53 text.ptr,
54 (SDL_Color) {
55 .r = asc_context.ink.red,
56 .g = asc_context.ink.green,
57 .b = asc_context.ink.blue,
58 .a = asc_context.ink.alpha
59 },
60 max_width
61 );
62 if (surface == NULL) {
63 asc_error(SDL_GetError());
64 return;
65 }
66
67 // Store basic node information
68 node->position = position;
69 node->dimension.width = surface->w;
70 node->dimension.height = surface->h;
71
72 // Transfer Image Data
73 // TODO: move the image data transfer to a separate function - we will need it more often
74 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
75 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel);
76 glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
77 surface->w, surface->h,
78 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
79
80 // Free the surface
81 SDL_FreeSurface(surface);
82
83 // Redraw the node
84 asc_text_redraw(node);
85 }
86
87 void asc_text_draw(
88 AscTextNode *node,
89 asc_vec2i position,
90 cxmutstr text) {
91 unsigned max_width = asc_context.active_window->dimensions.width - position.width;
92 asc_text_draw_lb(node, position, max_width, text);
93 }
94
95 void asc_text_redraw(AscTextNode *node) {
96 if (node->tex_id == 0) {
97 asc_error("Tried to redraw text node after destruction");
98 return;
99 }
100
101 glUseProgram(ASC_SHADER_FONT.base.id);
102
103 // Upload projection
104 // TODO: when we group UI draw calls, we don't need this
105 glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, GL_FALSE, asc_context.active_window->projection);
106
107 // Upload model matrix
108 asc_mat4f model = {0};
109 model[asc_mat4_index(0, 0)] = node->dimension.width;
110 model[asc_mat4_index(1, 1)] = node->dimension.height;
111 model[asc_mat4_index(3, 0)] = node->position.x;
112 model[asc_mat4_index(3, 1)] = node->position.y;
113 model[asc_mat4_index(3, 3)] = 1;
114 glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model);
115
116 // Upload surface
117 glActiveTexture(GL_TEXTURE0);
118 glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
119 glUniform1i(ASC_SHADER_FONT.surface, 0);
120
121 // Draw mesh
122 asc_primitives_draw_plane();
123 }
124
125 void asc_text_destroy(AscTextNode *node) {
126 asc_dprintf("Release text node texture: %u", node->tex_id);
127 glDeleteTextures(1, &node->tex_id);
128 node->tex_id = 0;
129 }

mercurial