[SDL] Re: OT: Comic Balloons

Bill Kendrick nbs at sonic.net
Mon Mar 17 23:24:01 PST 2003


On Mon, Mar 17, 2003 at 06:38:41PM -0800, Christopher Thielen wrote:
> that's a very good question. does anybody know? i ended up having to
> write my own wrapping code, as sdl_ttf seemed to just deal w/ rendering
> and loading fonts and nothing else (which is quite reasonable as that's
> what it's meant for).

Yep.  That's all it does.  Wordwrapping is pretty trivial though.

More or less:

  void render_wordwrapped(SDL_Surface * dstsurf, char * str,
                          int x, int y, int w, TTF_Font * font,
                          Uint8 color)
  {
    char myword[128];   /* Some reasonably-large size */
    int i, len, cur_x;
    SDL_Surface * ttfsurf;
    SDL_Rect dest;

    myword[0] = '\0';
    len = 0;
    cur_x = x;
    cur_y = y;

    for (i = 0; i <= strlen(str); i++)   /* '<=' to grab that last null :^) */
    {
      if (str[i] != ' ' && str[i] != '\0')
      {
        myword[len++] = str[i];
        myword[len] = '\0'
      }
      else
      {
        /* A break between words (or end of string)!  Renderin' time ! */

        ttfsurf = TTF_RenderText_Blended(font, myword, color);

        if (cur_x + ttfsurf->w > x + w)
        {
          /* This word would go off the edge; move down a line! */

          cur_x = x;
          cur_y = cur_y + ttfsurf->h;  /* Mightn't be best way to find height */
        }

        dest.x = cur_x;
        dest.y = cur_y;

        SDL_BlitSurface(ttfsurf, NULL, dstsurf, &dest);
      }
    }


I just wrote this into my e-mail client, so I don't even know if it's
syntactically correct, but you should get the idea.


An example call to this function might be:

  render_wordwrapped(my_surf, "Hello there! This is so much fun!",
                     0, 0, 320, my_font,
                     SDL_MapRGB(my_surf->format, 0, 0, 0));  /* black */


Good luck!

-bill!

-- 
bill at newbreedsoftware.com                                            Hire me!
http://newbreedsoftware.com/bill/    http://newbreedsoftware.com/bill/resume/




More information about the SDL mailing list