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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
#include "font.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <SDL3_image/SDL_image.h>
#define FILE_NAME_SIZE 300
void free_sprite_font(sprite_font* font) {
free(font->characters);
SDL_DestroyTexture(font->spritesheet);
memset(font, 0, sizeof(sprite_font));
}
int get_string_parameter(const char* line, const char* parameter, const char** start) {
*start = strstr(line, parameter);
if ((*start) == NULL)
return -1;
*start += strlen(parameter);
if ((*start)[0] != '\"') {
return -1;
}
int length = 0;
while ((*start)[length+1] != '\0' && (*start)[length+1] != '\"') length++;
(*start)++;
return length;
}
unsigned char parse_character_tag(const char* character_tag) {
if (strncmp(character_tag, "space", 5) == 0)
return ' ';
return character_tag[0];
}
void extract_dir_name(const char* filename, char dir_name_buffer[FILE_NAME_SIZE]) {
strcpy(dir_name_buffer, "");
const char* head = filename;
const char* next;
while ((next = strchr(head+1, '/')) != NULL) {
head=next;
}
int dir_name_length = SDL_min(head - filename, FILE_NAME_SIZE-1);
strncpy(dir_name_buffer, filename, dir_name_length);
dir_name_buffer[dir_name_length] = '\0';
}
bool get_int_parameter(const char* line, const char* parameter, int* value) {
const char* start = strstr(line, parameter);
if (start == NULL) {
return false;
}
start += strlen(parameter);
int c = 0;
bool negative = false;
if (start[0] == '-') {
negative = true;
start++;
}
for(int i = 0; start[i] >= '0' && start[i] <= '9'; i++) {
c *= 10; c += start[i] - '0';
}
*value = negative ? -c : c;
return true;
}
bool load_sprite_font(SDL_Renderer* renderer, const char* filename, sprite_font* font) {
if (font->characters || font->spritesheet) {
SDL_Log("Trying to reallocate font causing memory leak, exitting!");
exit(EXIT_FAILURE);
}
FILE* f = fopen(filename, "r");
if (f == NULL) {
SDL_Log("Failed to open sprite font file %s, error: %s!",
filename,
strerror(errno)
);
return false;
}
char line[PARSER_LINE_SIZE];
if (fgets(line, sizeof(line), f) == NULL ||
strncmp(line, "info", 4) != 0) {
SDL_Log("File %s is not a valid font file!",
filename);
fclose(f);
return false;
}
char font_name[FONT_NAME_SIZE];
const char* font_name_start;
int font_name_length =
SDL_min(get_string_parameter(line, "face=", &font_name_start),
FONT_NAME_SIZE-1
);
strncpy(font_name, font_name_start, font_name_length);
font_name[font_name_length] = '\0';
int char_size = 0, char_count = 0;
font_char* characters_unarranged = NULL;
unsigned char smallest_char = UCHAR_MAX;
unsigned char largest_char = 0;
char sprite_file_name[FILE_NAME_SIZE] = "";
while (fgets(line, PARSER_LINE_SIZE, f) != NULL) {
font_char* temp;
if (strncmp(line, "chars", 5) == 0) {
int total_char_count;
if(!get_int_parameter(line, "count=", &total_char_count))
continue;
char_size = total_char_count;
temp =
realloc(characters_unarranged, char_size * sizeof(font_char));
if (temp == NULL) {
SDL_Log("Failed to allocate space while loading font %s, error %s",
filename,
strerror(errno));
if (characters_unarranged != NULL) {
free(characters_unarranged);
}
fclose(f);
return false;
}
characters_unarranged = temp;
}
//Only one page supported
else if (strcmp(sprite_file_name, "") == 0 && strncmp(line, "page", 4) == 0) {
const char* sprite_fname_start;
int sprite_fname_length =
SDL_min(get_string_parameter(line, "file=", &sprite_fname_start),
FONT_NAME_SIZE-1
);
if (sprite_fname_length <= 0) {
SDL_Log("Malformed spritesheet file name in font file %s",
filename);
continue;
}
strncpy(sprite_file_name, sprite_fname_start, sprite_fname_length);
font_name[sprite_fname_length] = '\0';
}
else if (strncmp(line, "char", 4) == 0) {
font_char new_font_char;
new_font_char.active = true;
int val;
if (!get_int_parameter(line, "x=", &val))
continue;
new_font_char.x = val;
if (!get_int_parameter(line, "y=", &val))
continue;
new_font_char.y = val;
if (!get_int_parameter(line, "width=", &val))
continue;
new_font_char.width = val;
if (!get_int_parameter(line, "height=", &val))
continue;
new_font_char.height = val;
if (!get_int_parameter(line, "xoffset=", &val))
continue;
new_font_char.x_offset = val;
if (!get_int_parameter(line, "yoffset=", &val))
continue;
new_font_char.y_offset = val;
if (!get_int_parameter(line, "xadvance=", &val))
continue;
new_font_char.x_advance = val;
const char* character_tag;
if (get_string_parameter(line, "letter=", &character_tag) <= 0)
continue;
new_font_char.character = parse_character_tag(character_tag);
if (new_font_char.character < smallest_char) {
smallest_char = new_font_char.character;
}
if (new_font_char.character > largest_char) {
largest_char = new_font_char.character;
}
if (char_count >= char_size) {
char_size = char_count+1;
temp =
realloc(characters_unarranged, char_size * sizeof(font_char));
if (temp == NULL) {
SDL_Log("Failed to allocate space while loading font %s, error %s",
filename,
strerror(errno));
if (characters_unarranged != NULL) {
free(characters_unarranged);
}
fclose(f);
return false;
}
characters_unarranged = temp;
}
characters_unarranged[char_count] = new_font_char;
char_count++;
}
}
if (char_count <= 0) {
SDL_Log("Font %s defines no characters!", filename);
if (characters_unarranged != NULL) {
free(characters_unarranged);
}
fclose(f);
return false;
}
if (strcmp(sprite_file_name, "") == 0) {
SDL_Log("Font file %s has no spritesheet defined!",
filename
);
if (characters_unarranged != NULL) {
free(characters_unarranged);
}
fclose(f);
return false;
}
char font_dir_name[FILE_NAME_SIZE];
extract_dir_name(filename, font_dir_name);
char font_sprite_fname_full[FILE_NAME_SIZE*2];
snprintf(font_sprite_fname_full,
FILE_NAME_SIZE*2,
"%s/%s",
strcmp(font_dir_name, "") == 0 ? "." : font_dir_name,
sprite_file_name
);
SDL_Texture* font_spritesheet =
IMG_LoadTexture(renderer, font_sprite_fname_full);
if (font_spritesheet == NULL) {
SDL_Log("Failed to load texture %s as spritesheet for font %s!",
font_sprite_fname_full,
filename);
if (characters_unarranged != NULL) {
free(characters_unarranged);
}
fclose(f);
return false;
}
font_char* characters_arranged =
malloc((largest_char-smallest_char+1) * sizeof(font_char));
memset(characters_arranged, 0, (largest_char-smallest_char+1) * sizeof(font_char));
if (characters_arranged == NULL) {
SDL_Log("Failed to allocate space while loading font %s, error %s",
filename,
strerror(errno));
if (characters_unarranged != NULL) {
free(characters_unarranged);
}
SDL_DestroyTexture(font_spritesheet);
fclose(f);
return false;
}
for (int i = 0; i < char_count; i++) {
font_char* f = &characters_unarranged[i];
characters_arranged[f->character - smallest_char] = characters_unarranged[i];
}
*font = (sprite_font) {
.smallest_character = smallest_char,
.largest_character = largest_char,
.characters = characters_arranged,
.spritesheet = font_spritesheet
};
strncpy(font->name, font_name, FONT_NAME_SIZE-1);
font_name[FONT_NAME_SIZE-1] = '\0';
free(characters_unarranged);
fclose(f);
return true;
}
void draw_text_with_spritefont_extended
(
SDL_Renderer* renderer,
sprite_font* font,
const unsigned char* text,
float x_position, float y_position,
float scale
) {
while (*text) {
const font_char* fc = &font->characters[*text-font->smallest_character];
if (!fc->active) {
text++;
continue;
}
SDL_RenderTexture(renderer,
font->spritesheet,
&(SDL_FRect){
.x = fc->x,
.y = fc->y,
.w = fc->width,
.h = fc->height
},
&(SDL_FRect){
.x = x_position + fc->x_offset*scale,
.y = (y_position + fc->y_offset) * scale,
.w = fc->width * scale,
.h = fc->height * scale
});
x_position += fc->x_advance * scale;
text++;
}
}
void draw_text_with_spritefont
(
SDL_Renderer* renderer,
sprite_font* font,
const unsigned char* text,
float x_position, float y_position
) {
draw_text_with_spritefont_extended(renderer,
font,
text,
x_position,
y_position,
1.0
);
}
|