[SDL] SDL_ttf: Proposed extension for precreated surfaces

Bob Pendleton bob at pendleton.com
Wed Dec 8 10:12:29 PST 2004


On Wed, 2004-12-08 at 11:47, Koshmaar wrote:
>  > For outline fonts I usually render each glyph/size/color once when it is
> > first used by the program and convert it to a texture at that point.
> > Then I draw text by texture mapping the rectangles for each individual
> > glyph in a string.
> 
> How do you manage rendering fonts with high sizes and many glyphs? Eg. lately I've run into problem that in my card max. texture size is 2048, so anything above it will generate errors (actually it occured with GUI code for edit control, which supports typing very long text in very short field by rendering all string in one texture, then sliding that texture left and using glScissor to show only part of it, but theoretically it could occur in such font pre rendering).
> 
> ...
> 
> Errr at this point I realized that you don't render all your glyphs in one big texture but rather in small, single glyph textures. At the link in the end of mail, there's discussion that similiar approach would fragment your memory, so it would be better to store them in one place and then just create quads and give them appropriate texture coordinates. But there's that max texture size problem :-/
> Generally, wchich one is better to use(considering ease of implementation, speed of rendering, memory usage and maybe other factors)? I'm going to reimplement my font handling manager, so any good answers would help me much (and others too ;-)).

The answer is "that depends". The way I describe doesn't have any
visible effect on runtime performance and only loads glyphs that you
actually use. But, as you point out, it might cause memory fragmentation
problems. I have never seen that problem, and there are several reasons
for that. 1) I always set a video ram budget and stick to it. The budget
is set at a percentage, usually 70% of the size of the memory on the
video cards I'm targeting. 2) Programs follow a fixed pattern in
allocating memory, first allocate all rendering buffers, second allocate
textures and such, then allocate glyphs as they are needed. By
allocating the big stuff first you avoid memory fragmentation. You only
get into problems when you free a bunch of textures and load new ones.
Not usually a problem. 3) I keep a copy of the glyph in ram so I can
free all the texture glyphs and reload them without having to rerender
them.

OTOH, It is not hard to render all of the glyphs of a font into a set
(not one, but several) textures in advance. Just write, or find, a
utility to do it for you. Then keep font data around that has the size,
texture number, and location within a texture for each glyph in the
font. Then you load the textures and use the font. This approach has
very little run time overhead, but it can use a *lot* of memory for
large fonts (serious problem for CJK fonts) and loads lots of glyphs
that you will never use. But, it might be the best way in memory tight
situations because the memory usage is always 100% predictable in
advance. Which make sticking to a memory budget a lot easier even if it
does use up memory you may never need to use.

You also have to take into account the kind of application you are
writing. If you are writing a typical, you don't have a lot of text and
don't use a lot of glyphs. If you are writing a GUI for a word processor
you are going to use a lot of glyphs.

As I said, it depends, use your best judgment based on the application
you are currently building. There is no one answer that is always better
than all the others. And, when using your judgment remember that
tomorrow memory will be cheaper and video cards will be faster. 

		Bob Pendleton  

> 
> > Doing that spreads the time cost of rendering strings
> > out so you don't have the long pause caused by rendering every glyph in
> > a font. It also means you only have a texture for each glyph that has
> > been used. It is surprising how few glyphs are ever used. Then to, it
> > saves time by never rendering a glyph/size/color more than one.
> > Typically, if you are rendering a number of strings and you render all
> > of each string using SDL_ttf you will render the same glyph/size/color
> > many times. It seems to be a good way to render antialiased text.
> 
> Good idea, I will think about implementing sth similiar.
> 
> 
> Benjamin Deutsch: for some more "SDL_TTF & OpenGL fast text rendering" you can take a look here:
> 
> http://www.gamedev.net/community/forums/topic.asp?topic_id=286310&whichpage=1
> 
> 
> 
> Koshmaar
> 
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
-- 
+--------------------------------------+
+ Bob Pendleton: writer and programmer +
+ email: Bob at Pendleton.com             +
+ blog:  www.Stonewolf.net             +
+ web:   www.GameProgrammer.com        +
+--------------------------------------+





More information about the SDL mailing list