How to use SDL_ttf to show the text in different lines

I use the TTF_RenderUNICODE_Shaded()to show a text in a screen, it works well, but the text only show in one line.
So any one know how to let the text automatically change line and show all the text?

Say thank you so much,
Edy Li

I haven’t used SDL_ttf too much, but the equivalent text APIs in Asphyre, which I have used, has a string-width function. With that and a trivial parser, it’s not too difficult to implement your own word wrapping. Does SDL_ttf have a string-width function?________________________________
From: peng6sl@hotmail.com (peng6sl)
Subject: [SDL] How to use SDL_ttf to show the text in different lines

I use the TTF_RenderUNICODE_Shaded()to show a text in a screen, it works well, but the text only show in one line.
So any one know how to let the text automatically change line and show all the text?

Mason Wheeler wrote:

I haven’t used SDL_ttf too much, but the equivalent text APIs in
Asphyre, which I have used, has a string-width function. With that and a
trivial parser, it’s not too difficult to implement your own word
wrapping. Does SDL_ttf have a string-width function?----------------------------------------------------------------------

From: peng6sl
Subject: [SDL] How to use SDL_ttf to show the text in different lines
I use the TTF_RenderUNICODE_Shaded()to show a text in a screen, it works
well, but the text only show in one line.
So any one know how to let the text automatically change line and show
all the text?


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

It does. I’ve hit this myself… Here’s a tiny code snippet :

TTF_SizeText(Font, TheString, &W, &H);
// Is line-wrap required?
if (W > (Right-Left))
{
// Line wrap stuff here.
}

It does. I’ve hit this myself… Here’s a tiny code snippet :

TTF_SizeText(Font, TheString, &W, &H);
// Is line-wrap required?
if (W > (Right-Left))
{
// Line wrap stuff here.
}

Thank you guys,but after calculate the width, how to do the line wrap?

Figure out how much width you have to work with, then split your big
string into multiple strings of less than or equal to that width, then
render the smaller strings with whatever vertical coordinate you think
looks good.On Tue, Feb 2, 2010 at 9:29 PM, peng6sl wrote:

It does. I’ve hit this myself… Here’s a tiny code snippet :

TTF_SizeText(Font, TheString, &W, &H);
// Is line-wrap required?
if (W > (Right-Left))
{
// Line wrap stuff here.
}

Thank you guys,but after calculate the width, how to do the line wrap?


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

One of these days I’ll get back to working on this (the work was lost when my laptop spectacularly called it quits), but for my last game I had heavily modified SDL_TTF. I didn’t have the word wrap capability you asked for, but it did do multiline rendering as well as rudimentary rich text support, so you could have bold, italic and even multicolored text all in one call to TTFRender*.

I also wrote additional calls that took a pre-existing surface so you could render directly to a surface (say, containing a background image). This was useful for blending in text on ANY kind of background. I wrote this functionality because I found if you render to a surface than blit that to another, sometimes the text got “thinned out.”

http://www.dcemu.co.uk/vbulletin/showthread.php?t=179309

If you look at the screen shot here, the bottom line with the stats are all one surface created by a single call to TTF_RenderUnicode, showing light rich text handling, and the dialog is one call as well, demonstrating multiline text.

Sigh, I still haven’t learned backups, backups, BACKUPS!

One day I’ll sit with it again, as I feel these features could really benefit SDL_TTF.–
Anthony T.

I’ve done line wrap before (and I’ll need it again real soon), so here’s the
(C++) code I used a long time ago. I think it works, but I haven’t tested
it in a while, and it might not be the best way to do it. I’m using NFont,
which can load using SDL_ttf and has newline support.

char* wrapText(NFont& font, const char* Text, unsigned int width)
{
if (Text == NULL)
return NULL;

string result, text, line, word;

line = "";
result = "";
text = Text;

while(text.size() > 0)
{
    int nextSpace = text.find(' ');
    if(nextSpace == string::npos)
    {
        nextSpace = text.size();
        word = text.substr(0, nextSpace);
        text = "";
    }
    else
    {
        word = text.substr(0, nextSpace+1);
        text.erase(0, nextSpace + 1);
    }
    string temp = word + line;

    if(font.getWidth(temp.c_str()) <= width)
    {
        line += word;
    }
    else
    {
        line += '\n';
        result += line;
        line = word;
    }

}
result += line;

return copyString(result);

}

Jonny DOn Wed, Feb 3, 2010 at 7:33 AM, wrote:

One of these days I’ll get back to working on this (the work was lost when
my laptop spectacularly called it quits), but for my last game I had heavily
modified SDL_TTF. I didn’t have the word wrap capability you asked for, but
it did do multiline rendering as well as rudimentary rich text support, so
you could have bold, italic and even multicolored text all in one call to
TTFRender*.

I also wrote additional calls that took a pre-existing surface so you could
render directly to a surface (say, containing a background image). This was
useful for blending in text on ANY kind of background. I wrote this
functionality because I found if you render to a surface than blit that to
another, sometimes the text got “thinned out.”

http://www.dcemu.co.uk/vbulletin/showthread.php?t=179309

If you look at the screen shot here, the bottom line with the stats are all
one surface created by a single call to TTF_RenderUnicode, showing light
rich text handling, and the dialog is one call as well, demonstrating
multiline text.

Sigh, I still haven’t learned backups, backups, BACKUPS!

One day I’ll sit with it again, as I feel these features could really
benefit SDL_TTF.


Anthony T.


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