-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext.c
More file actions
79 lines (72 loc) · 1.5 KB
/
text.c
File metadata and controls
79 lines (72 loc) · 1.5 KB
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
#include <SDL2/SDL_image.h>
#include <stdbool.h>
SDL_Texture * font_small;
SDL_Texture * font_medium;
SDL_Texture * font_large;
enum Align {
left,
right,
center
};
enum AlignVert {
top,
bottom,
middle
};
enum FontSize {
small = 8,
medium = 16,
large = 32
};
//Must be used before text drawing functions or you will segfault probably.
bool
load_fonts(SDL_Renderer *renderer) {
bool success = true;
IMG_Init(IMG_INIT_PNG);
if(!(font_small = IMG_LoadTexture(renderer, "font_small.png"))) {
success = false;
}
if(!(font_medium = IMG_LoadTexture(renderer, "font_medium.png"))) {
success = false;
}
IMG_Quit();
return success;
}
void
draw_string(SDL_Renderer * renderer, char * string, int x, int y, enum FontSize font_size, enum Align align, enum AlignVert align_vert) {
SDL_Texture *font;
switch(font_size) {
case small:
font = font_small;
break;
case medium:
font = font_medium;
break;
case large:
font = font_large;
break;
}
if(align != left) {
int num_chars = 0;
while(string[num_chars]) {
num_chars++;
}
if(align == right) {
x -= num_chars*font_size;
} else if(align == center) {
x -= num_chars*font_size/2;
}
}
if(align_vert == top) {
y -= font_size;
} else if(align_vert == middle) {
y -= font_size/2;
}
int i = 0;
while(string[i]) {
SDL_Rect src = (SDL_Rect){(string[i] - 32)*font_size, 0, font_size, font_size};
SDL_Rect dest = (SDL_Rect){x + i*font_size, y, font_size, font_size};
SDL_RenderCopy(renderer, font, &src, &dest);
i++;
}
}