如何使用SDL2有效地渲染字体和文本? [英] How to render fonts and text with SDL2 efficiently?
问题描述
在此处看到有关使用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来创建具有整个渲染字符集的纹理,然后自己逐个字符从那里进行散布,会更明智吗?
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屋!