-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText.h
127 lines (114 loc) · 2.66 KB
/
Text.h
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
#pragma once
void drawText(uint x, uint y, const char *text)
{
Rect r;
r.x = x; r.y = y;
SDL_Surface *surface = TTF_RenderText_Solid(gfx.font, text, gfx.fontColor);
SDL_Texture *texture = SDL_CreateTextureFromSurface(gfx.renderer, surface);
SDL_QueryTexture(texture, NULL, NULL, &r.w, &r.h);
SDL_RenderCopy(gfx.renderer, texture, NULL, &r);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
void drawTextCentered(uint x, uint y, const char *text)
{
Rect r;
r.x = x; r.y = y;
SDL_Surface *surface = TTF_RenderText_Solid(gfx.font, text, gfx.fontColor);
SDL_Texture *texture = SDL_CreateTextureFromSurface(gfx.renderer, surface);
SDL_QueryTexture(texture, NULL, NULL, &r.w, &r.h);
r.x-=r.w/2;
r.y-=r.h/2;
SDL_RenderCopy(gfx.renderer, texture, NULL, &r);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
typedef struct{
char* text;
Rect r;
int size;
Color textColor;
Color backColor;
}TextBox;
void setFontSize(int size)
{
if(size == gfx.fontSize)
return;
if(gfx.font != NULL)
TTF_CloseFont(gfx.font);
gfx.fontSize = size;
gfx.font = TTF_OpenFont("./LiberationMono.ttf", gfx.fontSize);
}
void setFontColor(Color c)
{
gfx.fontColor = c;
}
void TB_setText(TextBox *tb, const char *text)
{
size_t size = sizeof(char)*strlen(text)+1;
tb->text = malloc(size);
memset(tb->text, '\0', size);
memcpy(tb->text, text, size);
}
void TB_setTextSize(TextBox *tb, int size)
{
tb->size = size;
}
TextBox* TB_create(uint x, uint y, const char* text)
{
TextBox *tb = malloc(sizeof(TextBox));
TB_setText(tb, text);
tb->r.x = x;
tb->r.y = y;
tb->backColor = BLACK;
tb->textColor = GREY;
tb->size = 48;
return tb;
}
void TB_destroy(TextBox *tb)
{
if(tb){
if(tb->text){
free(tb->text);
}
free(tb);
}
}
void TB_draw(TextBox *tb)
{
setFontSize(tb->size);
SDL_Surface *surface = TTF_RenderText_Solid(
gfx.font, tb->text, tb->textColor);
SDL_Texture *texture = SDL_CreateTextureFromSurface(
gfx.renderer, surface);
SDL_QueryTexture(texture, NULL, NULL, &(tb->r.w), &(tb->r.h));
setColor(tb->backColor);
SDL_RenderFillRect(gfx.renderer, &(tb->r));
SDL_RenderCopy(gfx.renderer, texture, NULL, &(tb->r));
setColor(tb->textColor);
// SDL_RenderDrawRect(gfx.renderer, &(tb->r));
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
void text_quit(void)
{
if(gfx.font != NULL){
TTF_CloseFont(gfx.font);
}
TTF_Quit();
}
void text_init(void)
{
if(TTF_Init()){
printf("SDL_ttf borked! Error: %s\n", TTF_GetError());
exit(0);
}
gfx.fontColor = WHITE;
setFontSize(SCALE);
if(!gfx.font){
printf("Unable to open font or set font size! Error: %s\n", TTF_GetError());
exit(0);
}
printf("Adding text_quit to atexit()\n");
atexit(text_quit);
}