1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#define ENGINE_INTERNALS
#include <engine/engine.h>
#include <engine/rendering.h>
extern Platform *GLOBAL_PLATFORM;
extern const char *uitype_str[];
void render_uitree(Window *w, UITree *t) {
switch (t->type) {
case uitype_container:
render_container(w, &t->container);
break;
case uitype_button:
render_button(w, &t->button);
break;
case uitype_title:
render_title(w, &t->title);
break;
case uitype_text:
render_text(w, &t->text);
break;
default:
if (t->type >= uitype_MAX) {
ERROR("Unknown uitype: %d", t->type);
} else {
WARN("Rendering not implemented for %s", uitype_str[t->type]);
}
break;
}
}
void render_container(Window *w, UITree_container *t) {
SDL_Rect r = {
.x = t->x,
.y = t->y,
.w = t->w,
.h = t->h
};
SDL_SetRenderDrawColor(w->renderer,
t->bg.r,
t->bg.g,
t->bg.b,
t->bg.a);
SDL_RenderFillRect(w->renderer, &r);
SDL_SetRenderDrawColor(w->renderer,
t->fg.r,
t->fg.g,
t->fg.b,
t->fg.a);
SDL_RenderDrawRect(w->renderer, &r);
#ifdef DAW_BUILD_DEBUG
r.x += t->padding;
r.y += t->padding;
r.w -= t->padding * 2;
r.h -= t->padding * 2;
SDL_SetRenderDrawColor(w->renderer,
0xFF,
0xFF,
0xFF,
0x7A);
SDL_RenderDrawRect(w->renderer, &r);
#endif
if (t->children != NULL && t->children_len > 0) {
for (usize i = 0; i < t->children_len; i++) {
render_uitree(w, t->children[i]);
}
}
}
void render_button(Window *w, UITree_button *t) {
SDL_Rect inner_r = {
.x = t->x + t->padding + (( t->w - t->texture_size.x) / 2),
.y = t->y + t->padding + (( t->h - t->texture_size.y) / 2),
.w = t->texture_size.x,
.h = t->texture_size.y,
};
SDL_Rect outer_r = {
.x = t->x,
.y = t->y,
.w = t->w + t->padding * 2,
.h = t->h + t->padding * 2,
};
SDL_Color fg = {
t->fg.r,
t->fg.g,
t->fg.b,
t->fg.a,
};
if (t->enabled == false) {
fg.r /= 2;
fg.g /= 2;
fg.b /= 2;
}
SDL_SetRenderDrawColor(w->renderer,
t->bg.r,
t->bg.g,
t->bg.b,
t->bg.a);
SDL_RenderFillRect(w->renderer, &outer_r);
SDL_SetRenderDrawColor(w->renderer,
fg.r,
fg.g,
fg.b,
fg.a);
SDL_RenderDrawRect(w->renderer, &outer_r);
SDL_Texture *texture = ((struct Resources*)GLOBAL_PLATFORM->data)
->textures[t->text_texture_index]->texture;
SDL_RenderCopy(
w->renderer,
texture,
NULL,
&inner_r);
}
void render_title(Window *w, UITree_title *t) {
SDL_Rect r = {
.x = t->x,
.y = t->y,
.w = t->texture_size.x,
.h = t->texture_size.y,
};
SDL_SetRenderDrawColor(w->renderer,
t->fg.r,
t->fg.g,
t->fg.b,
t->fg.a);
SDL_Texture *texture = ((struct Resources*)GLOBAL_PLATFORM->data)
->textures[t->text_texture_index]->texture;
SDL_RenderCopy(
w->renderer,
texture,
NULL,
&r);
#ifdef DAW_BUILD_DEBUG
SDL_SetRenderDrawColor(w->renderer,
0xFF,
0xFF,
0xFF,
0x7A);
SDL_RenderDrawRect(w->renderer, &r);
#endif
}
void render_text(Window *w, UITree_text *t) {
SDL_Rect r = {
.x = t->x,
.y = t->y,
.w = t->texture_size.x,
.h = t->texture_size.y,
};
SDL_SetRenderDrawColor(w->renderer,
t->fg.r,
t->fg.g,
t->fg.b,
t->fg.a);
SDL_Texture *texture = ((struct Resources*)GLOBAL_PLATFORM->data)
->textures[t->text_texture_index]->texture;
SDL_RenderCopy(
w->renderer,
texture,
NULL,
&r);
#ifdef DAW_BUILD_DEBUG
SDL_SetRenderDrawColor(w->renderer,
0xFF,
0xFF,
0xFF,
0x7A);
SDL_RenderDrawRect(w->renderer, &r);
#endif
}
i64 add_texture(struct Resources *resptr, Texture *t) {
if (NULL == resptr
|| NULL == t) return -1;
if (resptr->textures == NULL) {
resptr->textures = malloc(sizeof(Texture*) * TEXTURES_INCREMENT);
} else if (resptr->textures_len + 1 >= resptr->textures_size) {
resptr->textures_size += TEXTURES_INCREMENT;
resptr->textures = realloc(resptr->textures, sizeof(Texture*) * resptr->textures_size);
memset(resptr->textures + resptr->textures_size - TEXTURES_INCREMENT, 0, TEXTURES_INCREMENT);
}
resptr->textures[resptr->textures_len] = t;
return resptr->textures_len++;
}
i64 engine_render_text(i32 font_id, Engine_color fg, char *text, v2_i32 *size_out, bool wrapped) {
const SDL_Color sdl_fg = {.r = fg.r, .g = fg.g, .b = fg.b, .a = fg.a};
Texture *t = malloc(sizeof(Texture));
struct Resources *r;
if (t == NULL) {
ERROR("Failed to allocate memory for texture!\n");
exit(EXIT_FAILURE);
}
if (GLOBAL_PLATFORM == NULL) {
ERROR("Platform is uninitialized.\n");
exit(EXIT_FAILURE);
}
r = (struct Resources*)GLOBAL_PLATFORM->data;
if (r == NULL) {
ERROR("Resources not loaded!\n");
exit(EXIT_FAILURE);
}
if (r->fonts == NULL) {
ERROR("No fonts are initialized!\n");
exit(EXIT_FAILURE);
}
if ((usize)font_id >= r->fonts_len) {
ERROR("font-id \"%d\" is out of bounds!\n", font_id);
ERROR("Couldn't render text \"%s\"\n", text);
}
SDL_Surface *s = NULL;
if (wrapped) {
s = TTF_RenderUTF8_Solid_Wrapped(r->fonts[font_id], text, sdl_fg, size_out->x);
if (s == NULL) {
ERROR("Failed call to TTF_RenderUTF8_Solid_Wrapped: %s\n", TTF_GetError());
exit(EXIT_FAILURE);
}
} else {
s = TTF_RenderUTF8_Solid(r->fonts[font_id], text, sdl_fg);
if (s == NULL) {
ERROR("Failed call to TTF_RenderUTF8_Solid: %s\n", TTF_GetError());
exit(EXIT_FAILURE);
}
}
t->texture = SDL_CreateTextureFromSurface(GLOBAL_PLATFORM->window->renderer, s);
if (t->texture == NULL) {
ERROR("Failed call to SDL_CreateTextureFromSurface: %s\n", SDL_GetError());
INFO("FontID: %d", font_id);
INFO("Text: \"%s\" [%lu]", text, strlen(text));
exit(EXIT_FAILURE);
}
*(i32*)&t->width = s->w;
*(i32*)&t->height = s->h;
size_out->x = s->w;
size_out->y = s->h;
SDL_FreeSurface(s);
return add_texture(r, t);
}
|