如何使用 SDL2 高效渲染字体和文本? [英] How to render fonts and text with SDL2 efficiently?

查看:41
本文介绍了如何使用 SDL2 高效渲染字体和文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处看到这篇关于使用 SDL_ttf 在游戏中渲染文本的帖子.然而,这种方法需要在每一帧中调用 SDL_CreateTextureFromSurface(),以及 SDL_FreeSurface() 和 SDL_DestroyTexture().

Saw this post here about using SDL_ttf to render text in a game. However that approach requires calling SDL_CreateTextureFromSurface(), along with the SDL_FreeSurface() and SDL_DestroyTexture() every single frame.

是否在每一帧都创建纹理(并且可能随后必须将它们发送到 GPU)会对我的性能产生重大影响?

Is creating textures (and probably subsequently having to send them to the GPU) every frame something that can significally impact my performance?

仅使用 SDL_ttf 来使用我的整个渲染字符集创建纹理,然后自己逐个字符地从那里逐个 blit 会更明智吗?

would it be wiser to use SDL_ttf only to create a texture with my whole rendered charset and then to blit from there myself, character by character?

编辑:我希望仅以美国英语(基本 ASCII)呈现简单的等宽字体.

Edit: I'm looking to render simple monospace fonts in US-English (basic ASCII) only.

推荐答案

是的,每帧创建纹理都会影响性能.此外,每帧将 TrueType 字体光栅化为 SDL_Surfaces(如 SDL_ttf 所做的那样)会影响性能.

Yes, creating textures every frame can affect performance. Also, rasterizing TrueType fonts to SDL_Surfaces (as SDL_ttf does) every frame can affect performance.

我推荐 SDL_FontCache(完全公开:我是作者).它使用 SDL_ttf 并将生成的字形缓存在纹理中,因此您不必自己完成所有操作:
https://github.com/grimfang4/SDL_FontCache

I recommend SDL_FontCache (full disclosure: I'm the author). It uses SDL_ttf and caches the resulting glyphs in textures so you don't have to do it all yourself:
https://github.com/grimfang4/SDL_FontCache

这篇关于如何使用 SDL2 高效渲染字体和文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆