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
|
#define String(s) (s8){(u8*)s, lengthof(s)}
typedef struct {
u8 *buf;
size len;
} s8;
#define MAX_FONT_SIZE 8
size txt_length(const char *text)
{
size length = 0;
if(text != NULL){
while (*text++) length++;
}
return length;
}
s8 create_s8(const size sz)
{
return (s8){(u8*)RL_MALLOC(sz), sz};
}
void free_s8(s8* str)
{
RL_FREE(str->buf);
}
s8 cstr_to_s8(const char* str)
{
size sz = txt_length(str) + 1;
s8 final = (s8){(u8*)RL_MALLOC(sz), sz - 1};
memcpy(final.buf, str, sz);
return final;
}
b32 equal_s8(const s8 one, const s8 other)
{
// Get OUTTA here
if(one.len != other.len)
return false;
for(i32 i = 0; i < one.len; ++i){
if(one.buf[i] != other.buf[i])
return false;
}
return true;
}
void add_s8(s8 dst, const s8 src, size* pos)
{
memcpy(dst.buf + *pos, src.buf, src.len);
*pos += src.len;
}
s8 load_file_text(const s8 path)
{
char* shr = LoadFileText((const char*)path.buf);
s8 file = cstr_to_s8(shr);
UnloadFileText(shr);
return file;
}
// Exposed Functions //
static Font font_slots[MAX_FONT_SIZE] = {0};
static s8 font_paths[MAX_FONT_SIZE] = {0};
i32 load_font(const char* path)
{
s8 path_str = cstr_to_s8(path);
i32 current_idx = BLANK_DEFAULT;
for(i32 i = 0; i < MAX_FONT_SIZE; ++i){
if(equal_s8(path_str, font_paths[current_idx])){
free_s8(&path_str);
return i;
}
if(font_slots[i].texture.id <= 0){
current_idx = i;
font_slots[current_idx] = LoadFont((const char*)path_str.buf);
font_paths[current_idx] = path_str;
break;
}
}
//TODO: Handle slots a bit better.
assert(current_idx != BLANK_DEFAULT);
return current_idx;
}
void draw_text_font(i32 idx, const char* text, Vector2 pos, f32 size, Color col)
{
DrawTextEx(font_slots[idx], text, pos, size, 0.5f, col);
}
void draw_text_font_centered(i32 idx, const char* text, f32 y_offset, f32 size, Color col)
{
Vector2 vec = MeasureTextEx(font_slots[idx], text, size, 0.5f);
Vector2 final_pos = (Vector2){(320.0f - vec.x) / 2.f, y_offset};
DrawTextEx(font_slots[idx], text, final_pos, size, 0.5f, col);
DrawLineV((Vector2){final_pos.x, y_offset + vec.y - 2.f}, (Vector2){final_pos.x + vec.x, y_offset + vec.y - 2.f}, col);
}
void unload_font(i32 idx)
{
UnloadFont(font_slots[idx]);
free_s8(&font_paths[idx]);
}
void unload_all_fonts(void)
{
for(int i = 0; i < MAX_FONT_SIZE; ++i){
if(font_slots[i].texture.id > 0)
unload_font(i);
}
}
|