[SDL] A question about SDL_ttf

Ken Rogoway Ken at HomebrewSoftware.com
Sat Sep 8 22:33:03 PDT 2007


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 at 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
 



More information about the SDL mailing list