SDL_ttf and opengl

I’m wondering if there’s an easy way to use SDL_ttf for rendering fonts to a
surface, then using that surface in an opengl window. Would it be smarter to
generate glyphs one by one and store them as an opengl texture, or would it
be smarter to generate (when i need it) a sentence at a time and then map
the surface pixels to a quad?

Also, if the surface isn’t a power of 2 I cannot use it in opengl, so I’m
wondering if you guys have any techniques for modifying that ability a bit
to make it easy to use SDL_ttf with opengl.

Sean Kerr wrote:

I’m wondering if there’s an easy way to use SDL_ttf for rendering fonts to a
surface, then using that surface in an opengl window. Would it be smarter to
generate glyphs one by one and store them as an opengl texture, or would it
be smarter to generate (when i need it) a sentence at a time and then map
the surface pixels to a quad?

Generate the glyphs and convert them to textures (or one texture). You don’t
want to convert between a surface and texture in real time. I also don’t think
it’s prudent to generate surfaces/textures for series of letters (words,
sentences) because that simply wastes memory while saving only a little bit of
CPU overhead of individual glyph based rendering.

Also, if the surface isn’t a power of 2 I cannot use it in opengl, so I’m
wondering if you guys have any techniques for modifying that ability a bit
to make it easy to use SDL_ttf with opengl.

There are a couple of options. You can create a texture of nearest power of 2
for each glyph and store each glyph separately. This has the disadvantage of
wasting some of the texture space, but I don’t know how big of a deal that is
(whether GL will compress it for you or not). You can also create one large
texture to store all the glyphs, and copy glyphs from it at draw time. This
potentially can be a problem if you use a very large texture size. Although this
may not be as much of a problem with modern video cards as it used to be.

Personally I generate a nearest power of 2 texture for each glyph. It has worked
well for me.

Ilya,

Thanks. This is great information and has solidified my original thought.
Render to individual glyph and reuse them individually. I will most likely
be generating only a-z, A-Z, 0-9 and possibly some punctuation characters.
That shouldn’t be too bad on the texture memory usage should it?

Could you give me a few tips on how I might generate multiple lines of
SDL_ttf words to a single surface in SDL? Is that possible, or am I stuck
somehow generating to multiple surfaces first and joining them somehow, and
then extracting the pixel data? I was thinking generating a surface like
this and loading it into texture memory:

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
.!@#$%^&*

How will I get 4 lines of SDL_ttf data to a single surface like that?

Thanks again!On 10/30/07, Ilya Olevsky wrote:

Sean Kerr wrote:

I’m wondering if there’s an easy way to use SDL_ttf for rendering fonts
to a
surface, then using that surface in an opengl window. Would it be
smarter to
generate glyphs one by one and store them as an opengl texture, or would
it
be smarter to generate (when i need it) a sentence at a time and then
map
the surface pixels to a quad?

Generate the glyphs and convert them to textures (or one texture). You
don’t
want to convert between a surface and texture in real time. I also don’t
think
it’s prudent to generate surfaces/textures for series of letters (words,
sentences) because that simply wastes memory while saving only a little
bit of
CPU overhead of individual glyph based rendering.

Also, if the surface isn’t a power of 2 I cannot use it in opengl, so
I’m
wondering if you guys have any techniques for modifying that ability a
bit
to make it easy to use SDL_ttf with opengl.

There are a couple of options. You can create a texture of nearest power
of 2
for each glyph and store each glyph separately. This has the disadvantage
of
wasting some of the texture space, but I don’t know how big of a deal that
is
(whether GL will compress it for you or not). You can also create one
large
texture to store all the glyphs, and copy glyphs from it at draw time.
This
potentially can be a problem if you use a very large texture size.
Although this
may not be as much of a problem with modern video cards as it used to be.

Personally I generate a nearest power of 2 texture for each glyph. It has
worked
well for me.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I’m not sure that more detail will help anything. I use something like:

g_pTextSurface=TTF_RenderText_Blended(g_pFont,(char*)cpp_cl_17.c_str(),black1);
rcSrc.x=rcSrc.y=0; rcSrc.w=g_pTextSurface->w; rcSrc.h=g_pTextSurface->h;
rcDst=rcSrc; rcDst.x=chat_x;
rcDst.y=chatline17_y_offset;
SDL_BlitSurface(g_pTextSurface,&rcSrc,screen,&rcDst);
SDL_FreeSurface(g_pTextSurface);

You can do this lots of times with no real performance hit but I suppose you could use one additional surface and draw to that rather than the screen and then draw that to the screen. I think that would just be wasted overhead though. If you break it down to each letter it’s like you are bitmap blitting and the point of a tt font would be to avoid the hassle right?

---- Sean Kerr wrote:> Ilya,

Thanks. This is great information and has solidified my original thought.
Render to individual glyph and reuse them individually. I will most likely
be generating only a-z, A-Z, 0-9 and possibly some punctuation characters.
That shouldn’t be too bad on the texture memory usage should it?

Could you give me a few tips on how I might generate multiple lines of
SDL_ttf words to a single surface in SDL? Is that possible, or am I stuck
somehow generating to multiple surfaces first and joining them somehow, and
then extracting the pixel data? I was thinking generating a surface like
this and loading it into texture memory:

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
.!@#$%^&*

How will I get 4 lines of SDL_ttf data to a single surface like that?

Thanks again!

On 10/30/07, Ilya Olevsky wrote:

Sean Kerr wrote:

I’m wondering if there’s an easy way to use SDL_ttf for rendering fonts
to a
surface, then using that surface in an opengl window. Would it be
smarter to
generate glyphs one by one and store them as an opengl texture, or would
it
be smarter to generate (when i need it) a sentence at a time and then
map
the surface pixels to a quad?

Generate the glyphs and convert them to textures (or one texture). You
don’t
want to convert between a surface and texture in real time. I also don’t
think
it’s prudent to generate surfaces/textures for series of letters (words,
sentences) because that simply wastes memory while saving only a little
bit of
CPU overhead of individual glyph based rendering.

Also, if the surface isn’t a power of 2 I cannot use it in opengl, so
I’m
wondering if you guys have any techniques for modifying that ability a
bit
to make it easy to use SDL_ttf with opengl.

There are a couple of options. You can create a texture of nearest power
of 2
for each glyph and store each glyph separately. This has the disadvantage
of
wasting some of the texture space, but I don’t know how big of a deal that
is
(whether GL will compress it for you or not). You can also create one
large
texture to store all the glyphs, and copy glyphs from it at draw time.
This
potentially can be a problem if you use a very large texture size.
Although this
may not be as much of a problem with modern video cards as it used to be.

Personally I generate a nearest power of 2 texture for each glyph. It has
worked
well for me.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Ilya,

Thanks. This is great information and has solidified my original
thought. Render to individual glyph and reuse them individually. I
will most likely be generating only a-z, A-Z, 0-9 and possibly some
punctuation characters. That shouldn’t be too bad on the texture
memory usage should it?

Could you give me a few tips on how I might generate multiple lines of
SDL_ttf words to a single surface in SDL? Is that possible, or am I
stuck somehow generating to multiple surfaces first and joining them
somehow, and then extracting the pixel data? I was thinking generating
a surface like this and loading it into texture memory:

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
.!@#$%^&*

And what about i18n? Why are you using SDL_ttf at all and not bitmap
fonts or SDL_font?Am Dienstag, den 30.10.2007, 20:53 -0400 schrieb Sean Kerr:

How will I get 4 lines of SDL_ttf data to a single surface like that?

Thanks again!

On 10/30/07, Ilya Olevsky wrote:
Sean Kerr wrote:
> I’m wondering if there’s an easy way to use SDL_ttf for
rendering fonts to a
> surface, then using that surface in an opengl window. Would
it be smarter to
> generate glyphs one by one and store them as an opengl
texture, or would it
> be smarter to generate (when i need it) a sentence at a time
and then map
> the surface pixels to a quad?

    Generate the glyphs and convert them to textures (or one
    texture). You don't
    want to convert between a surface and texture in real time. I
    also don't think 
    it's prudent to generate surfaces/textures for series of
    letters (words,
    sentences) because that simply wastes memory while saving only
    a little bit of
    CPU overhead of individual glyph based rendering.
    
    > Also, if the surface isn't a power of 2 I cannot use it in
    opengl, so I'm
    > wondering if you guys have any techniques for modifying that
    ability a bit
    > to make it easy to use SDL_ttf with opengl. 
    
    There are a couple of options. You can create a texture of
    nearest power of 2
    for each glyph and store each glyph separately. This has the
    disadvantage of
    wasting some of the texture space, but I don't know how big of
    a deal that is 
    (whether GL will compress it for you or not). You can also
    create one large
    texture to store all the glyphs, and copy glyphs from it at
    draw time. This
    potentially can be a problem if you use a very large texture
    size. Although this 
    may not be as much of a problem with modern video cards as it
    used to be.
    
    Personally I generate a nearest power of 2 texture for each
    glyph. It has worked
    well for me.
    
    _______________________________________________ 
    SDL mailing list
    SDL at lists.libsdl.org
    http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hey,

To put these onto a single surface, you should probably blit each line to a new surface. Try something like this:

int drawTTFSolid(TTF_Font font, const char text, SDL_Surface dest, SDL_Rect drect, SDL_Color fg)
{
int h;
SDL_Surface* textSurf = TTF_RenderText_Solid(font, text, fg);
SDL_BlitSurface(textSurf, NULL, dest, &drect);
h = textSurf->h;
SDL_FreeSurface(textSurf);
return h;
}
SDL_Surface* result = SDL_CreateRGBSurface(Fill this in…);SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.y += drawTTFSolid(somefont, “abcdefghijklmnopqrstuvwxyz”, result, &rect, somecolor);
rect.y += drawTTFSolid(somefont, “ABCDEFGHIJKLMNOPQRSTUVWXYZ”, result, &rect, somecolor);
rect.y += drawTTFSolid(somefont, “0123456789”, result, &rect, somecolor);
rect.y += drawTTFSolid(somefont, “.!@#$%^&*”, result, &rect, somecolor);
// Now result should have all my text!

That should just about do it. Now, I haven’t ever used SDL_ttf, so I’m wary of those weird symbols in the last string.

-Jonny D

Sean Kerr wrote:> I’m wondering if there’s an easy way to use SDL_ttf for rendering fonts to a> surface, then using that surface in an opengl window. Would it be smarter to> generate glyphs one by one and store them as an opengl texture, or would it > be smarter to generate (when i need it) a sentence at a time and then map> the surface pixels to a quad?Generate the glyphs and convert them to textures (or one texture). You don’twant to convert between a surface and texture in real time. I also don’t think it’s prudent to generate surfaces/textures for series of letters (words,sentences) because that simply wastes memory while saving only a little bit ofCPU overhead of individual glyph based rendering.> Also, if the surface isn’t a power of 2 I cannot use it in opengl, so I’m> wondering if you guys have any techniques for modifying that ability a bit> to make it easy to use SDL_ttf with opengl. There are a couple of options. You can create a texture of nearest power of 2for each glyph and store each glyph separately. This has the disadvantage ofwasting some of the texture space, but I don’t know how big of a deal that is (whether GL will compress it for you or not). You can also create one largetexture to store all the glyphs, and copy glyphs from it at draw time. Thispotentially can be a problem if you use a very large texture size. Although this may not be as much of a problem with modern video cards as it used to be.Personally I generate a nearest power of 2 texture for each glyph. It has workedwell for me._______________________________________________ SDL mailing listSDL at lists.libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.orgDate: Tue, 30 Oct 2007 20:53:36 -0400From: sean at code-box.orgTo: sdl at lists.libsdl.orgSubject: Re: [SDL] SDL_ttf and openglIlya,Thanks. This is great information and has solidified my original thought. Render to individual glyph and reuse them individually. I will most likely be generating only a-z, A-Z, 0-9 and possibly some punctuation characters. That shouldn’t be too bad on the texture memory usage should it? Could you give me a few tips on how I might generate multiple lines of SDL_ttf words to a single surface in SDL? Is that possible, or am I stuck somehow generating to multiple surfaces first and joining them somehow, and then extracting the pixel data? I was thinking generating a surface like this and loading it into texture memory: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.!@#$%^&*How will I get 4 lines of SDL_ttf data to a single surface like that?Thanks again!
On 10/30/07, Ilya Olevsky wrote:


Climb to the top of the charts!? Play Star Shuffle:? the word scramble challenge with star power.
http://club.live.com/star_shuffle.aspx?icid=starshuffle_wlmailtextlink_oct

Sean Kerr wrote:

Thanks. This is great information and has solidified my original thought.
Render to individual glyph and reuse them individually. I will most likely
be generating only a-z, A-Z, 0-9 and possibly some punctuation characters.
That shouldn’t be too bad on the texture memory usage should it?

If you’re doing only ASCII, you should generate the codes from 32 to 126. Then
you can map a char directly to a glyph.

Could you give me a few tips on how I might generate multiple lines of
SDL_ttf words to a single surface in SDL? Is that possible, or am I stuck
somehow generating to multiple surfaces first and joining them somehow, and
then extracting the pixel data? I was thinking generating a surface like
this and loading it into texture memory:

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
.!@#$%^&*

How will I get 4 lines of SDL_ttf data to a single surface like that?

You’ll have to pretty much create a separate surface and blit individual SDL_ttf
generated lines to it.