从文件或内存加载 TTF/OTF 字体,创建字体实例用于文本渲染。
| 函数 | 说明 |
|---|---|
int xgeFontFaceLoad(xge_font_face* ppFace, const char* sPath, const xge_font_face_desc_t* pDesc) | 加载字体文件 |
int xgeFontFaceLoadMemory(xge_font_face* ppFace, const void* pData, int iSize, const xge_font_face_desc_t* pDesc) | 从内存加载 |
int xgeFontFaceAddRef(xge_font_face pFace) | 增加引用 |
void xgeFontFaceFree(xge_font_face pFace) | 释放字体 |
int xgeFontCreate(xge_font pFont, xge_font_face pFace, const xge_font_instance_desc_t* pDesc) | 创建字体实例 |
int xgeFontLoad(xge_font pFont, const char* sPath, float fSize) | 快捷加载(文件) |
int xgeFontLoadMemory(xge_font pFont, const void* pData, int iSize, float fSize) | 快捷加载(内存) |
int xgeFontAddRef(xge_font pFont) | 增加引用 |
void xgeFontFree(xge_font pFont) | 释放实例 |
gcc -O2 -I. -o ch81.exe ch81_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch81.exe
// 方式 1:快捷加载(推荐)
xge_font font = NULL;
xgeFontLoad(&font, "fonts/NotoSansSC.ttf", 24.0f);
// 方式 2:分步加载(更灵活)
xge_font_face face = NULL;
xgeFontFaceLoad(&face, "fonts/Roboto.ttf", NULL);
xge_font_instance_desc_t desc = { 0 };
desc.fSize = 32.0f;
xge_font font2 = NULL;
xgeFontCreate(&font2, face, &desc);
xgeFontFaceFree(face); // Font 内部已 AddRef
// 方式 3:从内存加载(嵌入资源)
extern const uint8_t fontData[];
extern const int fontDataSize;
xge_font font3 = NULL;
xgeFontLoadMemory(&font3, fontData, fontDataSize, 18.0f);
// 释放
xgeFontFree(font);
xgeFontFree(font2);
xgeFontFree(font3);将以下代码保存为 ch81_main.c,编译后运行即可看到 TTF 字体加载与绘制效果:
#include "xge.h"
static xge_font g_font = NULL;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
// 绘制文字
xgeTextDraw(g_font, "Hello XGE Font!", 100, 200, 0xFFFFFFFF)
xgeTextDraw(g_font, "XGE 字体渲染测试", 100, 280, 0x4FD8C2FF)
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch81 - TTF Font Load";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
// 快捷加载 TTF 字体
xgeFontLoad(&g_font, "fonts/NotoSansSC.ttf", 32.0f);
xgeRun(onFrame, NULL);
xgeFontFree(g_font);
xgeUnit();
return 0;
}