TTF_RenderText_Sold - Unevenly spaced characters

Hello Again,

Well. I am attempting to write a function ‘displayText’ that will take
a number of parameters (one of them being a text buffer), and print
the text to the screen one character at a time. This way I can account
for \n & \t if I need to (making text formatting simpler). This also
creates a cool “typed” look when the text is displayed, which is what
i am going for.

Included below is the function. The problem is that when the text is
displayed on the screen unevenly spaced between characters (something
I cannot properly show in an email, because the font is fixed width).
I’ve been trying to get it right using the text->w (width of the character)
to space the dest.x positioning, but i’m not getting it.

Please review the included function and let me know if you can determin
why the spacing between characters would be unevenly spaced.

Thanks in advance.

int displayText(SDL_Surface *screen, TTF_Font *font, SDL_Color color,
char *string) {
static int iTop = 0;
static SDL_Rect dest;
SDL_Surface *text;
int i = 0;
char c[2] = {’\0’};

if (!iTop) {
    iTop = 1;
    dest.x = 0;
    dest.y = 4;
}

while (*(string+i) != '\0') {
    switch (*(string+i)) {
    case '\n':
        dest.x = 0;
        dest.y = dest.y + 18;
        i++;
        break;
    default:
        c[0] = *(string+i);
        text = TTF_RenderText_Solid(font, c, color);
        if ( text == NULL ) {
            fprintf(stderr, "Unable to render text: %s\n", SDL_GetError());
            return(0);
        }
        printf("%s, %d\n", c, text->w);
        SDL_BlitSurface(text, NULL, screen, &dest);
        SDL_UpdateRect(screen, 0, 0, 0, 0);

        if ( SDL_SetColorKey(text, SDL_SRCCOLORKEY|SDL_RLEACCEL, 0) < 0 ) {
            fprintf(stderr, "Warning: Couldn't set text colorkey: %s\n",
                            SDL_GetError());
            return(0);
        }
        dest.x = dest.x + text->w;
        SDL_FreeSurface(text);
        i++;
        break;
    }
}
return(1);

}–
Nick Jennings

Perhaps I did not explain myself clearly enough for my question to be
answered easily. Basically, the function ‘displayText’ will iterate
through the string one character at a time, checking for \n (in wich
case it changes the dest.* to a new line). If it’s a character it
displays the character and uses the size of text->w to space dest.x
over enough for the next character.

The problem is, if I pass the string “test message” or something,
the output is unevenly spaced between characters.

t est m ess ag e

the real output is much more fine grained as the fonts are not fixed
as they are in this email. But you get the idea.

I need to figure out how to set dest.x to be in the correct position
for the next character to display, so the message comes out looking
perfectly even (as if the entire string had been displayed in one
call to ‘TTF_RenderText_Solid’.

All help appreciated.On Fri, Jan 04, 2002 at 05:23:44PM -0800, Nick Jennings wrote:

int displayText(SDL_Surface *screen, TTF_Font *font, SDL_Color color,
char *string) {
static int iTop = 0;
static SDL_Rect dest;
SDL_Surface *text;
int i = 0;
char c[2] = {’\0’};

if (!iTop) {
    iTop = 1;
    dest.x = 0;
    dest.y = 4;
}

while (*(string+i) != '\0') {
    switch (*(string+i)) {
    case '\n':
        dest.x = 0;
        dest.y = dest.y + 18;
        i++;
        break;
    default:
        c[0] = *(string+i);
        text = TTF_RenderText_Solid(font, c, color);
        if ( text == NULL ) {
            fprintf(stderr, "Unable to render text: %s\n", SDL_GetError());
            return(0);
        }
        printf("%s, %d\n", c, text->w);
        SDL_BlitSurface(text, NULL, screen, &dest);
        SDL_UpdateRect(screen, 0, 0, 0, 0);

        if ( SDL_SetColorKey(text, SDL_SRCCOLORKEY|SDL_RLEACCEL, 0) < 0 ) {
            fprintf(stderr, "Warning: Couldn't set text colorkey: %s\n",
                            SDL_GetError());
            return(0);
        }
        dest.x = dest.x + text->w;
        SDL_FreeSurface(text);
        i++;
        break;
    }
}
return(1);

}


Nick Jennings


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Nick Jennings

Nick Jennings wrote:

Perhaps I did not explain myself clearly enough for my question to be
answered easily. Basically, the function ‘displayText’ will iterate
through the string one character at a time, checking for \n (in wich
case it changes the dest.* to a new line). If it’s a character it
displays the character and uses the size of text->w to space dest.x
over enough for the next character.

the easiest solution is probably to hunt for your endline characters,
and then render each line at a time, instead of character-by-character.

Nick Jennings wrote:

Hello Again,

Well. I am attempting to write a function ‘displayText’ that will take
a number of parameters (one of them being a text buffer), and print
the text to the screen one character at a time. This way I can account
for \n & \t if I need to (making text formatting simpler). This also
creates a cool “typed” look when the text is displayed, which is what
i am going for.

I have spent months and months writting code that produces formatted
text. Dowload libksd and see src/TextBox.cpp. Doing this effeciently
and correctly is very complicated. (I don’t hit on the correctly as
well as effeciently! :slight_smile: In my implmentation, the text is broken into
chunks before hand and blited as such - no space problems.

-- David Snopek

/-- libksd –
| The C++ Cross-Platform Game Framework
| Only want to write it once??
| http://libksd.sourceforge.net
------------