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

0410-Raylib-显示非英文字符

环境

  • Time 2024-03-06
  • Zig 0.12.0-dev.3152+90c1a2c41
  • WSL-Ubuntu 22.04.3 LTS
  • Raylib 5.0

前言

说明

参考资料:

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

目标

显示非英文字符,并且将重复的字符去除。

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 [text] example");defer ray.CloseWindow();ray.SetTargetFPS(60);const text = "いろはにほへと ちりぬるを\nわかよたれそ つねならむ\nうゐのおくやま けふこえて\nあさきゆめみし ゑひもせす";// Get codepoints from textvar count: c_int = 0;const loadedCodepoints = ray.LoadCodepoints(text, &count);const codepoints: []c_int = loadedCodepoints[0..@intCast(count)];// Removed duplicate codepoints to generate smaller font atlasconst allocator = std.heap.c_allocator;const codes = try removeDuplicates(allocator, codepoints);allocator.free(codes);ray.UnloadCodepoints(loadedCodepoints);// Load font containing all the provided codepoint glyphs// A texture font atlas is automatically generatedconst font = ray.LoadFontEx("res/DotGothic16-Regular.ttf", 48, loadedCodepoints, count);// Set bilinear scale filter for better font scalingray.SetTextureFilter(font.texture, ray.TEXTURE_FILTER_BILINEAR);ray.SetTextLineSpacing(54); // Set line spacing for multiline text (when line breaks are included '\n')// Free codepoints, atlas has already been generatedvar showFontAtlas = false;var codepointSize: c_int = 0;var ptr = @intFromPtr(text);while (!ray.WindowShouldClose()) {// Updateif (ray.IsKeyPressed(ray.KEY_SPACE)) showFontAtlas = !showFontAtlas;// Testing code: getting next and previous codepoints on provided textif (ray.IsKeyPressed(ray.KEY_RIGHT)) {// Get next codepoint in string and move pointer_ = ray.GetCodepointNext(ptr, &codepointSize);ptr += @intCast(codepointSize);} else if (ray.IsKeyPressed(ray.KEY_LEFT)) {// Get previous codepoint in string and move pointer_ = ray.GetCodepointPrevious(ptr, &codepointSize);ptr -= @intCast(codepointSize);}// Drawray.BeginDrawing();defer ray.EndDrawing();ray.ClearBackground(ray.RAYWHITE);ray.DrawRectangle(0, 0, ray.GetScreenWidth(), 70, ray.BLACK);ray.DrawText(ray.TextFormat("Total codepoints contained in provided text: %i", count), 10, 10, 20, ray.GREEN);ray.DrawText(ray.TextFormat("Total codepoints required for font atlas (duplicates excluded): %i", codes.len), 10, 40, 20, ray.GREEN);if (showFontAtlas) {// Draw generated font texture atlas containing provided codepointsray.DrawTexture(font.texture, 150, 100, ray.BLACK);ray.DrawRectangleLines(150, 100, font.texture.width, font.texture.height, ray.BLACK);} else {// Draw provided text with laoded font, containing all required codepoint glyphsray.DrawTextEx(font, text, .{ .x = 160, .y = 110 }, 48, 5, ray.BLACK);}ray.DrawText("Press SPACE to toggle font atlas view!", 10, ray.GetScreenHeight() - 30, 20, ray.GRAY);ray.DrawFPS(screenWidth - 100, 10);}
}// Remove codepoint duplicates if requested
// WARNING: This process could be a bit slow if there text to process is very long
fn removeDuplicates(allocator: std.mem.Allocator, codepoints: []c_int) ![]c_int {var codepointsNoDupsCount = codepoints.len;var codepointsNoDups = try allocator.alloc(c_int, codepoints.len);@memcpy(codepointsNoDups, codepoints);// Remove duplicatesfor (0..codepointsNoDupsCount) |i| {var j = i + 1;while (j < codepointsNoDupsCount) : (j += 1) {if (codepointsNoDups[i] == codepointsNoDups[j]) {for (j..codepointsNoDupsCount - 1) |k|codepointsNoDups[k] = codepointsNoDups[k + 1];codepointsNoDupsCount -= 1;j -= 1;}}}// NOTE: The size of codepointsNoDups is the same as original array but// only required positions are filled (codepointsNoDupsCount)return codepointsNoDups[0..codepointsNoDupsCount];
}

效果

非英文字符

总结

显示非英文字符,并且将重复的字符去除。

附录

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

相关文章:

  • 云平台存储路线比较分析:存储虚拟化、分布式存储、网络共享文件存储
  • 基于智能步进电机驱动的视觉追踪云台:从算法到平稳执行
  • 魔兽争霸3现代优化全攻略:告别卡顿和画面变形,3个步骤让经典游戏焕发新生
  • XT系列大电流连接器供应商出片品质哪家高,深度测评所见即所得不踩雷 - mypinpai
  • 不会写代码搭建企业知识库问答AI-Agent
  • Godot 4地形插件MTerrain完整指南:从安装到性能优化
  • 2026 年合肥全屋定制品牌甄选:拾光里本土实力全解析 - 装修大知识
  • 双福靠谱装修公司推荐!先装后付款,全程透明无套路 - 林州鸿途网络
  • 太原买乐器全品类选购指南|品类齐全、高性价比购琴攻略 - 收录优先
  • net start mysql80无法启动mysql服务解决思路
  • Nintendo Switch NAND管理工具:3步掌握你的游戏数据安全
  • RTX 5060 Ti上Whisper模型部署优化:从基础转录到高效字幕生产
  • LCA算法详解:从暴力法到倍增、Tarjan与RMQ的高效实现
  • FSR压力传感器的温度漂移补偿方法对比
  • 2026年郑州触摸屏公司如何选?七大企业同台比拼 - 兔兔不是荼荼
  • 毕业证遗失线上登报挂失攻略,异地可办 - 实用干货补给站
  • LED筒灯启动故障排查:美标电感镇流器与驱动电源兼容性解决方案
  • 对接合肥市优秀脆皮烤鸭厂家联系方式看安徽臻鲜食品科技有限公司(合肥市营销部) - 品牌优推
  • GEO 生成式引擎优化:面向北海本地实体的 AI 收录落地实践方案
  • 办理AAA级企业信用证书有什么好处?需要什么材料? - 实用干货补给站
  • 宜兴装修怎么选?本土靠谱家装公司服务解析 - 国麟测评
  • Flutter树形结构可视化在鸿蒙终端的适配与优化
  • 卫辉门窗选购指南:封阳台、旧房换窗避坑,浅谈圣希尔门窗本地化服务优势 - 国麟测评
  • 大型赛事投票选什么小程序?云众评选实测,高并发投票稳定性对比 - 微信投票小程序
  • Godot 2D几何破坏系统:轻量级多边形切割与碎裂实现
  • 校园外卖订单拦截系统开发架构剖析
  • 儿童英语口语外教课哪个平台好?专攻开口提升,家长经验之谈!
  • 郑州漏水检测怎么选正规公司避坑指南推荐上门服务电话24小时 - 知途管道科技
  • 如何用一款免费工具轻松解决Windows和Office激活难题?
  • Unity项目从Built-in升级到URP:系统化解决材质丢失问题