Jump to content
  • Advertisement
Sign in to follow this  

Wrapping text with SDL_ttf, etc

This topic is 5084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can SDL_ttf wrap text? And will \n produce a new line? I was hoping to use a line of text: "safsa d d sfd s f sdf fds fds sfd fd dsf\ndfsdsf fds df\ndsda sd sda" And write it out like so: safsa d d sfd s f sdf fds fds sfd fd dsf dfsdsf fds df dsda sd sda Also, is it possible to justify sdl_ttf to, given a top left corner and top right corner Finally, can text size (preferably justified) be dependant on a rectangle location (x1,y1),(x1,y1)

Share this post

Link to post
Share on other sites
Write a function that counts the number of \n's, converts them to \0's, and goes through and renders each line in place up to the number of \n counted earlier.

Works simple enough.

Share this post

Link to post
Share on other sites
There's article here on GameDev which covers using OpenGL with SDL_TTF and as a bonus, it includes this "tiny" funtion:

void PrintStrings( SDL_Surface *sDest, TTF_Font *fnt, String str,
SDL_Rect &rc, SDL_Color clrFg, SDL_Color clrBg ) {
int lineSkip = TTF_FontLineSkip( font ); // Get the line-skip value.
int width = 0, height = 10;
std::vector<String> vLines; // these are the individual lines.

// Break the String into its lines:
int n = 0;
while( n != -1 ) {
// Get until either '\n' or '\0':
String strSub;
n = str.find( '\n', 0 ); // Find the next '\n'
strSub = str.substr( 0,n );
if( n != -1 ) {
str = str.substr( n+1, -1 );

// Get the size of the rendered text:
int w = 0;
TTF_SizeText( font, strSub.c_str(), &w,&height );
if( w > width ) { width = w; }
// (really, we just want to see how wide this is going to be)

// Since the width was determined earlier, get the height:
// (vLines.size() == Number of Lines)
height = (vLines.size()-1) * lineSkip + height; // plus the first line
// (we assume that height is the same for all lines.)

// Make the surface to which to blit the text:
sText = SDL_CreateRGBSurface( SDL_SWSURFACE, width,height,
// (it's compatible with the destination Surface

// Now, fill it with the background color:
SDL_FillRect( sText,NULL, clrBg );

// Actually render the text:
SDL_Surface *sTemp = NULL;
for( int i = 0; i < vLines.size(); i++ ) {
// The rendered text:
sTemp = TTF_RenderText_Solid( font, vLines.c_str(), clrFg );

// Put it on the surface (sText):
SDL_BlitSurface( sTemp,NULL,
sText,&(SDL_Rect)newSDL_Rect(0,i*lineSkip,0,0) );
// Clean up:
SDL_FreeSurface( sTemp );
// So, now we have a nice bitmap (sText) that has all the text on it.

// Draw the text on the destination:
SDL_BlitSurface( sText,NULL, dest,&rc );
SDL_FreeSurface( sText );

Remember that should be considered to be only a sketch of algorithm, because it's much too slow to use it in main loop of any real time game, there's good thread on OpenGL forum where were discussed various SDL_TTF + OGL optimizing techniques.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!