Advertisement

Wrapping text with SDL_ttf, etc

Started by December 16, 2004 10:30 PM
4 comments, last by ImperfectFreak 19 years, 9 months ago
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)
Nope all SDL_TTF does is print characters to a surface, you have to write the rest yourself, it gets very easy after a while, a hint use std::vector. . .
Advertisement
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.
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 );    }    vLines.push_back(strSub);        // 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,       sDest->format->BitsPerPixel,       sDest->format->Rmask,      sDest->format->Gmask,      sDest->format->Bmask,      sDest->format->Amask  );  // (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.
In that case, is there any way to find the width of a string?
int w, h;
std::string mystring;
TTF_SizeText( font, mystring.c_str(), &w, &h);

This topic is closed to new replies.

Advertisement