FontLoadCached 智能加载:优先使用 XRF 缓存,不存在时从 TTF 生成并缓存。
| 函数 | 说明 |
|---|---|
int xgeFontLoadCached(xge_font pFont, const char* sTTFPath, const char* sXRFPath, float fSize, uint32_t iFirstCodepoint, uint32_t iCount) | 缓存加载 |
gcc -O2 -I. -o ch83.exe ch83_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch83.exe
// 首次运行:从 TTF 生成 XRF 缓存
// 后续运行:直接加载 XRF(快 10 倍+)
xge_font font = NULL;
xgeFontLoadCached(&font,
"fonts/NotoSansSC.ttf", // TTF 源
"cache/NotoSans24.xrf", // XRF 缓存路径
24.0f, // 字号
0x20, 95); // ASCII 范围
// 加载逻辑:
// 1. 检查 XRF 是否存在且版本匹配
// 2. 存在 → 直接加载 XRF
// 3. 不存在 → 加载 TTF → 生成 XRF → 保存
// 适合中文字体:首次慢,后续秒开
xge_font cnFont = NULL;
xgeFontLoadCached(&cnFont,
"fonts/NotoSansSC.ttf",
"cache/NotoSansSC16.xrf",
16.0f,
0x4E00, 3000); // 常用汉字将以下代码保存为 ch83_main.c,编译后运行即可看到缓存加载效果(首次运行生成 XRF,后续秒开):
#include "xge.h"
static xge_font g_font = NULL;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
xgeTextDraw(g_font, "Cached Font Load!", 100, 250, 0xFFFFFFFF)
xgeTextDraw(g_font, "First run: slow, Next: fast", 100, 310, 0x4FD8C2FF)
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch83 - Font Cache";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
// 智能缓存加载
xgeFontLoadCached(&g_font,
"fonts/NotoSansSC.ttf",
"cache/NotoSans24.xrf",
24.0f, 0x20, 95);
xgeRun(onFrame, NULL);
xgeFontFree(g_font);
xgeUnit();
return 0;
}