forked from libsdl-org/SDL_ttf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Atlas.cpp
194 lines (162 loc) · 6.6 KB
/
Atlas.cpp
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
#include <stdio.h>
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <unordered_map>
SDL_bool useDark, forceDark = 0;
int renderStyle = 0;
SDL_Texture*fontTexture;
SDL_Renderer*renderer;
std::unordered_map<Uint64, TTF_AtlasEntry*> table; //It's more readable to use TTF_AtlasEntry* than mapping to an index
int spaceXAdv16, baseline16, lineHeight16;
int spaceXAdv20, baseline20, lineHeight20;
static void DrawWindow(int textureWidth, int textureHeight);
static void DrawString(const char*text, int x, int y, int spaceXAdv, int baseline, int lineHeight, Uint64 xorID);
static Uint64 AtlasPred(void*user, int glyph) { return glyph ^ (Uint64)user; }
int main(int argc, char *argv[])
{
char buffer[4096];
const char*filename=0;
for(int i=1; i<argc; i++) {
if (argv[i][0] == '-') {
//Ignore flags
} else {
filename = argv[i];
break;
}
}
#ifdef SDL_PLATFORM_WINDOWS
if (!filename) {
filename="C:\\Windows\\Fonts\\arial.ttf";
}
#elif defined(SDL_PLATFORM_MACOS) or defined(SDL_PLATFORM_LINUX)
if (!filename) {
buffer[0] = 0;
FILE*f = popen("fc-match -f %{file}", "r");
if (f) {
int len = fread(buffer, 1, sizeof(buffer), f);
pclose(f);
buffer[len] = 0;
filename = buffer;
}
}
#else
#error "Unsupported Platform"
#endif
TTF_Init();
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL init failed\n");
return 1;
}
useDark = SDL_GetSystemTheme() == SDL_SYSTEM_THEME_DARK;
useDark |= forceDark;
SDL_Window*window = SDL_CreateWindow("Atlas Test", 1280, 720, SDL_WINDOW_RESIZABLE);
if (window == 0) {
fprintf(stderr, "SDL_CreateWindow failed\n");
return 1;
}
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_PRESENTVSYNC);
TTF_AtlasInfo atlas;
Uint64 tag16 = 16ULL << 32;
Uint64 tag20 = 20ULL << 32;
auto atlasState = TTF_AtlasInit(&atlas, 0);
//auto pixels = SDL_malloc(1024*1024*4);
//auto atlasState = TTF_AtlasInitFixed(&atlas, 1024, 1024, 1024*4, pixels, SDL_PIXELFORMAT_ARGB8888);
//auto atlasState = TTF_AtlasInitFixed(&atlas, 1024, 1024, 0, 0, SDL_PIXELFORMAT_UNKNOWN);
//spaceXAdv16 = TTF_AtlasLoadPredicate(filename, 16, atlasState, AtlasPred, (void*)tag16); //This works too
spaceXAdv16 = TTF_AtlasLoad(filename, 16, atlasState, 0, 0, 0, tag16); //null range and null list means load everything
//We can use these functions to reserve space for us to draw in
int reservePairs[] = {10, 4, 8, 16}; //First icon is 10pixels wide, 4 tall, Second is 8 wide 16 tall
int entryIndex = TTF_AtlasReserveEntry2(atlasState, reservePairs, 4, reservePairs); //last param overwrites reserveTwoIcons
int reserveTriples[] = {10, 4, 99, 8, 16, 98};
entryIndex = TTF_AtlasReserveEntry3(atlasState, reserveTriples, 6, 0);
int range20[]={'a', 'z'};
//Y is intentionally missing to show we didn't load everything
spaceXAdv20 = TTF_AtlasLoad(filename, 20, atlasState, range20, 2, "TE\uFFFD", tag20);
/*
TTF_AtlasDeinitWithSurface(atlasState);
fontTexture = SDL_CreateTextureFromSurface(renderer, atlas.surface); //*/
fontTexture = TTF_AtlasDeinitState(atlasState, renderer);
if (!fontTexture) {
fprintf(stderr, "Failed to create font texture. Error code %d\n", atlas.errorCode);
return 1;
}
for(int i = 0; i < atlas.entryLength; i++) {
//We should use a hash function since sequential keys are not terrific
table.insert({atlas.ids[i], &atlas.entries[i]});
}
//We choose the baseline and line height here
//E is a tall common letter, q is a common letter with pixels under the baseline (descent)
//Some letters might be taller than E or go further down than q but that's fine
//We choose the baseline as the ascent of E plus some padding from the top of the line
//Typically the descent is a negative value because it's below the baseline
//We choose lineHeight by using baseline (which has padding) subtract the (negative) descent value giving us a larger positive value
{
baseline16 = table['E' | tag16]->ascent + 2;
auto q = table['q' | tag16];
lineHeight16 = baseline16 - (q->ascent - q->height);
baseline20 = table['E' | tag20]->ascent + 2;
auto q20 = table['q' | tag20];
lineHeight20 = baseline20 - (q20->ascent - q20->height);
}
DrawWindow(atlas.width, atlas.height);
SDL_bool wantQuit = 0;
SDL_Event event;
while (!wantQuit) {
if (!SDL_WaitEvent(&event)) { wantQuit = 1; break; }
switch (event.type) {
case SDL_EVENT_QUIT: { wantQuit = 1; } break;
case SDL_EVENT_KEY_DOWN: {
if (event.key.keysym.sym == SDLK_ESCAPE) { wantQuit = 1; break; }
} break;
}
DrawWindow(atlas.width, atlas.height);
}
TTF_AtlasDeinitInfo(&atlas);
TTF_Quit();
return 0;
}
static void DrawWindow(int textureWidth, int textureHeight) {
//Choose background and text color of canvas, canvas BG is later
if (useDark) {
SDL_SetRenderDrawColor(renderer, 0x10, 0x10, 0x10, 255);
SDL_SetTextureColorMod(fontTexture, 0xE0, 0xE0, 0xE0);
} else {
SDL_SetRenderDrawColor(renderer, 0xF0, 0xF0, 0xF0, 255);
SDL_SetTextureColorMod(fontTexture, 0x10, 0x10, 0x10);
}
SDL_RenderClear(renderer);
//Choose canvas background and draw our texture on top
SDL_FRect r = {0, 0, textureWidth, textureHeight};
if (useDark) SDL_SetRenderDrawColor(renderer, 0, 0, 0x40, 255); else { SDL_SetRenderDrawColor(renderer, 0xC0, 0xC0, 0xFF, 255); }
SDL_RenderFillRect(renderer, &r);
SDL_RenderTexture(renderer, fontTexture, &r, &r);
// To test missing characters we replace ~ and ` in our draw function
const char *testString = "Test string pq ~ `\nYou may choose the line\nheight however you like\n\nThe quick brown fox\njumped over the lazy dog";
DrawString(testString, 1024+4, 20, spaceXAdv16, baseline16, lineHeight16, 16ULL<<32);
SDL_SetTextureColorMod(fontTexture, 0x40, 0x80, 0x40);
DrawString(testString, 1024+4, 240, spaceXAdv20, baseline20, lineHeight20, 20ULL<<32); //Y is intentionally missing to show we didn't load everything
SDL_RenderPresent(renderer);
}
static void DrawString(const char*text, int x, int y, int spaceXAdv, int baseline, int lineHeight, Uint64 xorID) {
if (!text) return;
const char*p = text;
int startX = x;
while (*p) {
Sint32 glyph = *p++;
if (glyph == '~') { glyph = 0x2603; } //2603 is snowman ☃ which is likely missing in the choosen font
if (glyph == '`') { glyph = 0xEC08; } //Private font
if (glyph == ' ') {
x += spaceXAdv;
continue;
} else if (glyph == '\n') {
x = startX;
y += lineHeight;
continue;
}
TTF_AtlasEntry*e = table[ (table.contains(glyph ^ xorID) ? glyph : 0xFFFD) ^ xorID ];
SDL_FRect srcRect = {e->canvasX, e->canvasY, e->width, e->height};
SDL_FRect dstRect = {x + e->left, y + baseline - e->ascent, e->width, e->height};
x += e->xAdv;
SDL_RenderTexture(renderer, fontTexture, &srcRect, &dstRect);
}
}