-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.c
75 lines (57 loc) · 1.23 KB
/
font.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
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
#include "muon.h"
int initFont() {
font = IMG_Load("data/gfx/font.png");
if (font == NULL) {
printf("ERROR: initFont failed: data/gfx/font.png");
return -1;
}
return 0;
}
SDL_Surface *makeStringSurface(char *str) {
int lines = 0;
int rows = 0;
int i, j = 0;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == '\n' || str[i + 1] == '\0') {
lines++;
if (rows > j)
j = rows;
if (str[i + 1] == '\0')
j += 2;
rows = 0;
} else {
rows++;
}
}
rows = j;
int surface_w = rows * 7;
int surface_h = lines * 18;
SDL_Surface *surface;
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, surface_w, surface_h, 24, 0x00, 0x00, 0x00, 0x00);
SDL_Rect clip;
clip.h = 18;
clip.w = 7;
clip.y = 0;
rows = 0;
lines = 0;
for (i = 0; str[i] != '\0'; i++) {
clip.x = clip.w * (str[i] - 33);
SDL_Rect rect;
if (str[i] == '\n') {
lines++;
rows = 0;
} else if (str[i] == ' ') {
rows++;
} else {
rows++;
rect.x = clip.w * rows;
rect.y = clip.h * lines;
rect.w = clip.w;
rect.h = clip.h;
SDL_BlitSurface(font, &clip, surface, &rect);
}
}
Uint32 colorkey = SDL_MapRGB(surface->format, 0x00, 0x00, 0x00);
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colorkey);
return surface;
}