A question about SDL_ttf

Hi All,
I’m using SDL_ttf to draw texts. It works perfect, but I don’t know how
to show multiple lines of texts.
For example, the text is “This is a sample \nJust a test”. I expect it
shows the texts with two lines, but it just shows a blank for the ‘\n’. Does
SDL_ttf support showing a string with multiple lines?
If no, and if I want to do this, what I think I can do is that, split
the string to several strings, create several SDL_Surface, and draw them at
the correct position. Is there any better way to implement this?
Thanks.–
!!!

Hi All,
I’m using SDL_ttf to draw texts. It works perfect, but I don’t know how
to show multiple lines of texts.
For example, the text is “This is a sample \nJust a test”. I expect it
shows the texts with two lines, but it just shows a blank for the ‘\n’. Does
SDL_ttf support showing a string with multiple lines?
If no, and if I want to do this, what I think I can do is that, split
the string to several strings, create several SDL_Surface, and draw them at
the correct position. Is there any better way to implement this?

That’s the way to do it. :slight_smile:

Tux Paint was recently updated to use SDL_Pango for most text rendering,
which handles RtoL, complex scripts (like Arabic), word-wrapping and
multiple lines pretty transparently. If you can afford all of IT’S
dependencies, it might be worth switching (esp. if you plan much in the way
of i18n in your project/product).

Good luck,On Sun, Sep 09, 2007 at 01:10:28PM +0800, Mine wrote:


-bill!
bill at newbreedsoftware.com
http://www.newbreedsoftware.com/

The simplest way is to write a wrapper for the SDL_ttf function you are
using. All you need to do is to parse the string for the \n character (you
can use strchr()) and output each line, then bump the Y position to write
the text for the next line. You are basically splitting it on the fly (as
you need it) so you don’t need any additional storage to break the string
up.

Something like:

// Draw text starting at x,y and moving down a “line” if a \n is found
// You will probably also pass in your TTF_Font * unless you have that
// in a class and this function is a method of that class.
void DrawMultilineText( char * pText,int x,int y )
{
// Just use a const height for now, but in reality you would
// use TTF_SizeText( pFont,pText,&nDummy,&nTextHeight );
int nTextHeight = 16;

while( 1 )
{
    char * pLineFeed = strchr( pText,'\n' );
    
    if ( pLineFeed )
    {
        // Null terminate string (in-place)
        *pLineFeed = '\0';
        
        // Call a routine to output text using TTF (you will need a

TTF_Font *)
// This would probably be a call to TTF_RenderText_Blended()
// or TTF_RenderText_Shaded().
DrawText( pText,x,y );
y += nTextHeight;

        // Restore the linefeed so we aren't damaging the original

string
*pLineFeed = ‘\n’;

        // Skip to the start of the next line
        pText = pLineFeed+1;
    }
    else
    {
        // Call a routine to output text using TTF (you will need a

TTF_Font *)
// This would probably be a call to TTF_RenderText_Blended()
// or TTF_RenderText_Shaded().
DrawText( pText,x,y );

        // We are done with all the text so break out of the loop
        break;
    }
}

}

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/________________________________

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Mine
Sent: Sunday, September 09, 2007 12:10 AM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: [SDL] A question about SDL_ttf

Hi All,
I’m using SDL_ttf to draw texts. It works perfect, but I don’t know how
to show multiple lines of texts.
For example, the text is “This is a sample \nJust a test”. I expect it
shows the texts with two lines, but it just shows a blank for the ‘\n’. Does
SDL_ttf support showing a string with multiple lines?
If no, and if I want to do this, what I think I can do is that, split
the string to several strings, create several SDL_Surface, and draw them at
the correct position. Is there any better way to implement this?
Thanks.

!!!

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.485 / Virus Database: 269.13.10/995 - Release Date: 9/8/2007 1:
24 PM

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.485 / Virus Database: 269.13.10/995 - Release Date: 9/8/2007 1:
24 PM

Sorry for the poor formatting of my last email. I have attached the sample.
Hopefully it will be more readable. The sample is just quick code fragment,
but it should show you what I was trying to suggest.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.485 / Virus Database: 269.13.10/995 - Release Date: 9/8/2007
1:24 PM

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: DrawMultilineText.cpp
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070909/6ee2cd0c/attachment.txt

Thanks a lot :-)On 9/9/07, Ken Rogoway wrote:

Sorry for the poor formatting of my last email. I have attached the
sample.
Hopefully it will be more readable. The sample is just quick code
fragment,
but it should show you what I was trying to suggest.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.485 / Virus Database: 269.13.10/995 - Release Date: 9/8/2007
1:24 PM


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


!!!

Hey,

You could also try my C++ font class here. However, it uses bitmaps, so it’s not TrueType. It supports animation though.

Jonny DDate: Sun, 9 Sep 2007 13:10:28 +0800From: mine260309 at gmail.comTo: sdl at lists.libsdl.orgSubject: [SDL] A question about SDL_ttfHi All, I’m using SDL_ttf to draw texts. It works perfect, but I don’t know how to show multiple lines of texts. For example, the text is “This is a sample \nJust a test”. I expect it shows the texts with two lines, but it just shows a blank for the ‘\n’. Does SDL_ttf support showing a string with multiple lines? If no, and if I want to do this, what I think I can do is that, split the string to several strings, create several SDL_Surface, and draw them at the correct position. Is there any better way to implement this? Thanks.-- !!!


Kick back and relax with hot games and cool activities at the Messenger Caf??.
http://www.cafemessenger.com?ocid=TXT_TAGLM_SeptWLtagline