-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_text.c
40 lines (33 loc) · 1.31 KB
/
menu_text.c
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
#include "menu_text.h"
#include "utils.h"
Text *create_text(SDL_Renderer *renderer, SDL_Rect area, char *text, TTF_Font *textFont, Uint32 textColor) {
Text *txt = (Text *) malloc(sizeof(Text));
txt->textFont = textFont;
txt->texture = NULL;
set_color(textColor, &txt->textColor);
edit_text(renderer, txt, area, text);
return txt;
}
void edit_text(SDL_Renderer *renderer, Text *text, SDL_Rect area, char *newText) {
SDL_Surface *surface = TTF_RenderUTF8_Blended(text->textFont, newText, text->textColor);
SDL_Texture *surface_texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if (surface == NULL) {
SDL_Rect txtArea = {0, 0, 0, 0};
text->area = txtArea;
} else {
SDL_Rect txtArea = {area.x + area.w / 2 - surface->w / 2, area.y + area.h / 2 - surface->h / 2, surface->w, surface->h};
text->area = txtArea;
}
text->text = newText;
SDL_DestroyTexture(text->texture);
text->texture = surface_texture;
}
void draw_text(SDL_Renderer *renderer, Text *text, Vector2s offset) {
SDL_Rect dst = {offset.x + text->area.x, offset.y + text->area.y, text->area.w, text->area.h};
SDL_RenderCopy(renderer, text->texture, NULL, &dst);
}
void free_text(Text *text) {
SDL_DestroyTexture(text->texture);
free(text);
}