当前位置: 首页 > news >正文

0388-Raylib-加载字体文件

环境

  • Time 2024-03-01
  • Zig 0.12.0-dev.3076+6e078883e
  • WSL-Ubuntu 22.04.3 LTS
  • Raylib 5.0

前言

说明

参考资料:

  1. https://www.raylib.com/examples.html

目标

加载 TTF 字体文件并显示。

main.zig

const std = @import("std");
const ray = @import("raylib.zig");pub fn main() !void {const screenWidth: c_int = 800;const screenHeight: c_int = 450;ray.InitWindow(screenWidth, screenHeight, "raylib [texture] example");defer ray.CloseWindow();ray.SetTargetFPS(60);var parrots = ray.LoadImage("res/parrots.png"); // Load image in CPU memory (RAM)// TTF Font loading with custom generation parametersconst font = ray.LoadFontEx("resources/KAISG.ttf", 64, 0, 0);defer ray.UnloadFont(font);// Draw over image using custom fontconst size: f32 = 48;ray.ImageDrawTextEx(&parrots, font, "[Parrots font drawing]", .{ .x = 20.0, .y = 20.0 }, size, 0.0, ray.RED);const texture = ray.LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)defer ray.UnloadTexture(texture);ray.UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAMconst position = ray.Vector2{.x = @floatFromInt(screenWidth / 2 - @divTrunc(texture.width, 2)),.y = @floatFromInt(screenHeight / 2 - @divTrunc(texture.height, 2) - 20),};var showFont = false;while (!ray.WindowShouldClose()) {// UpdateshowFont = ray.IsKeyDown(ray.KEY_SPACE);// Drawray.BeginDrawing();defer ray.EndDrawing();ray.ClearBackground(ray.RAYWHITE);if (!showFont) {// Draw texture with text already drawn insideray.DrawTextureV(texture, position, ray.WHITE);// Draw text directly using sprite fontray.DrawTextEx(font, "[Parrots font drawing]", .{.x = position.x + 20,.y = position.y + 20 + 280,}, size, 0.0, ray.WHITE);} else {const x = screenWidth / 2 - @divTrunc(font.texture.width, 2);ray.DrawTexture(font.texture, x, 50, ray.BLACK);}ray.DrawText("PRESS SPACE to SHOW FONT ATLAS USED", 290, 420, 10, ray.DARKGRAY);ray.DrawFPS(10, 10);}
}

效果

加载字体文件

总结

加载 TTF 字体文件并显示。

附录

http://www.jsqmd.com/news/1360298/

相关文章:

  • VS Code启动优化:禁用Welcome页面的3种方法
  • 2026中山业主装修口碑观察:轩怡家装为什么受到关注? - 天下观知
  • 自定义统计技术方案与架构设计实践
  • 2026安徽省成人高考/高起专怎么报名? - 最新资讯
  • 假期周进度报告04
  • 免费解锁WeMod高级功能:开源补丁工具的完整指南
  • 护理考研308护综备考必看!博傲关永俊课程正规报名入口全揭秘 - 博傲教育
  • JMeter性能测试入门:从核心原理到实战安装与脚本编写
  • 人才盘点与梯队建设全拆解|从人才争夺到人才经营
  • NVIDIA Profile Inspector:5个常见显卡问题与终极解决方案
  • Unity物体跟随:矩阵变换的高精度实现与应用
  • 2026年纯玩南京二日游旅行社**,正规地接跟团游纯玩无购物品牌推荐,金牌导游一路随行 - 跟我去旅游
  • 2026蚌埠电大中专怎么报名?年满18岁全年可报,附报名流程及所需材料清单 - 最新资讯
  • 校园智能点餐系统开发:微信小程序与SpringBoot实践
  • 2026年8月武汉殡葬告别仪式策划行业研究报告出炉 - 品牌品鉴馆
  • 杭州市民卡充值卡回收:手里闲置卡券怎么变现?2026年最新变现攻略 - 沃卡回收
  • 0384-Raylib-混合模式
  • 同一股票池两端顺序不同:QMT、PTrade用稳定排序复现结果
  • 广州装修公司口碑**真实推荐 - 品牌品鉴馆
  • 暗黑破坏神2存档编辑器终极指南:5分钟快速修改角色装备属性
  • 2026 年采购钢格栅为什么推荐丹姐《振邦源头厂家》 - 优企甄选
  • 5个目标管理模型,把想法变成现实
  • 如何永久解锁Wand专业版:5分钟解决2小时限制的完整指南
  • 国产大模型实战指南:Qwen、DeepSeek、GLM选型与集成开发
  • Vue+Spring Boot构建高效在线书城系统实践
  • 2026年河北实验室蠕动泵厂家盘点:齐力产品优势及选购要点汇总 - 董不懂啊
  • 2026东莞装修高口碑推荐丨从施工到售后,轩怡家装如何提升东莞业主装修体验? - 天下观知
  • 树形DP解决括号匹配问题:CSP-S2019括号树题解
  • PL-2303驱动终极解决方案:让老旧工业设备在Windows 10系统重获新生
  • 2026培根选购避坑指南|从配料表到烹饪效果,实测教你选对好培根 - 产品评测官