🎯 raylib 完全可以使用系统字体(Windows 自带字体)

而且非常简单,只需要:

  1. 找到系统字体路径
  2. LoadFont()LoadFontEx() 加载
  3. DrawTextEx() 绘制

一个完整、可直接运行的 C 语言示例。

🪟 Windows 系统字体路径

Windows 字体都在:

1
C:\Windows\Fonts\

常见字体:

字体名 文件名
微软雅黑 msyahei.ttc / msyh.ttc
宋体 simsun.ttc
黑体 simhei.ttf
Consolas consolas.ttf
Arial arial.ttf

可以直接加载这些字体文件。

📝 示例:使用 Windows 系统字体(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
#include "raylib.h"

int main(void)
{
InitWindow(800, 450, "raylib system font example");
SetTargetFPS(60);

// 加载 Windows 系统字体(微软雅黑)
Font font = LoadFontEx("C:/Windows/Fonts/msyh.ttc", 32, NULL, 0);

while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(RAYWHITE);

DrawTextEx(font, "使用系统字体:微软雅黑", (Vector2){20, 40}, 32, 2, BLACK);
DrawTextEx(font, "Hello, raylib with system font!", (Vector2){20, 100}, 32, 2, DARKBLUE);

EndDrawing();
}

UnloadFont(font);
CloseWindow();
return 0;
}

运行后会看到:

  • 中文显示正常
  • 字体变得现代、清晰
  • 完全摆脱默认字体的“像素风”

🎨 可以加载任何系统字体

例如:

1
Font font = LoadFontEx("C:/Windows/Fonts/simhei.ttf", 40, NULL, 0);

或者:

1
Font font = LoadFontEx("C:/Windows/Fonts/consola.ttf", 28, NULL, 0);

甚至可以加载:

  • 仿宋
  • 楷体
  • 黑体
  • Arial
  • Times New Roman
  • Consolas

只要是 TTF/OTF 都能加载。

🧠 为什么 raylib 默认字体不好看?

因为它是:

  • 内置的
  • 5x8 像素点阵
  • 只支持 ASCII
  • 不能显示中文
  • 主要用于调试和示例

所以做正式项目时一定要换字体。

🎯 最佳实践(你做 Termi2D / Termi3D 时推荐)

✔ 1. 使用系统字体(最简单)

1
LoadFontEx("C:/Windows/Fonts/msyh.ttc", 32, NULL, 0);

✔ 2. 把字体复制到你的项目 assets 目录(最专业)

1
2
assets/fonts/msyh.ttc
assets/fonts/consola.ttf

这样:

  • 不依赖用户系统字体
  • 不依赖 Windows
  • 跨平台(Linux / macOS)也能用

✔ 3. 使用 DrawTextEx() 而不是 DrawText()

因为:

  • DrawText() 只能用默认字体
  • DrawTextEx() 才能用自定义字体