高效地使用SDL2渲染字体和文本 [英] Rendering fonts and text with SDL2 efficiently

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

问题描述

在这里关于使用SDL_ttf在游戏中呈现文本。然而,这种方法需要调用SDL_CreateTextureFromSurface(),以及每一帧SDL_FreeSurface()和SDL_DestroyTexture()。

创建纹理(可能随后不得不将它们发送到GPU)每一帧都会对我的表现产生重大影响?

如果用SDL_ttf只用我的整个渲染字符集来创建一个纹理,然后从这里自己逐个字符地更新呢?
$ b

编辑:我正在寻找仅用美英(简单的ASCII)渲染简单的等宽字体。

它使用SDL_ttf并将结果字形缓存在纹理中,因此您不必自己完成所有的工作:

https://github.com/grimfang4/SDL_FontCache


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.

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

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?

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

解决方案

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

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天全站免登陆