[1.2+OpenGL]-SDL_TTF-Display (C) & (R) Characters Linux?

[1.2+OpenGL]-SDL_TTF-Display © & ® Characters Linux?

Hi,

I am trying to display © and ® characters.
I am using SDL1.2+OpenGL and SDL_TTF on Linux.

When I try to print © and ® characters,
I get a strange character right before the © or ® character.

Here is screenshot:
[Image: http://16bitsoft.com/images/LF2-CopyTradeMark-Issue/SDL_TTF-CopyTradeMark-Issue.png ]

Text draw function:

Code:
//-------------------------------------------------------------------------------------------------
int Visuals::NextPowerOfTwo(int x)
{
double logbase2 = log(x) / log(2);
return (int)round(pow(2,ceil(logbase2)));
}
//-------------------------------------------------------------------------------------------------
void Visuals::DrawTextOntoScreenBuffer(const char *textToDisplay, TTF_Font *font, GLfloat posX, GLfloat posY,
int XJustification, Uint8 textRed, Uint8 textGreen, Uint8 textBlue,
Uint8 outlineRed, Uint8 outlineGreen, Uint8 outlineBlue)
{
SDL_Color textColor;
SDL_Color outlineColor;
SDL_Surface *text;
SDL_Surface *textOutline;
SDL_Surface *textAndOutline;
int w,h;
SDL_Rect rect;
GLuint textTexture;

textColor.r = textRed;
textColor.g = textGreen;
textColor.b = textBlue;

outlineColor.r = outlineRed;
outlineColor.g = outlineGreen;
outlineColor.b = outlineBlue;

text = TTF_RenderText_Blended(font, textToDisplay, textColor);
textOutline = TTF_RenderText_Solid(font, textToDisplay, outlineColor);

if (XJustification == JustifyLeft)
{
}
else if (XJustification == JustifyCenter)
{
    posX = (640 / 2) - (text->w / 2) - 2;
}
else if (XJustification == JustifyRight)
{
    posX = (640 - posX) - text->w - 2;
}
else if (XJustification == JustifyCenterOnPoint)
{
    posX = posX - (text->w / 2) - 2;
}

w = NextPowerOfTwo(3+text->w);
h = NextPowerOfTwo(3+text->h);

textAndOutline = SDL_CreateRGBSurface(0, w, h, 32, R_Mask, G_Mask, B_Mask, A_Mask);

for (Sint16 y = 0; y < 5; y+=1)
{
    for (Sint16 x = 0; x < 5; x+=1)
    {
        rect.x = x; rect.y = y;
        SDL_BlitSurface(textOutline, 0, textAndOutline, &rect);
    }
}

rect.x = 2;
rect.y = 2;
SDL_BlitSurface(text, 0, textAndOutline, &rect);

glPushMatrix();

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);

glGenTextures(1, &textTexture);
glBindTexture(GL_TEXTURE_2D, textTexture);
if (ColorIsRGBA == true)
    glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA,
    GL_UNSIGNED_BYTE, textAndOutline->pixels );
else
    glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
    GL_UNSIGNED_BYTE, textAndOutline->pixels );

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textTexture);
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);
    glVertex2f(posX    , posY + h);

    glTexCoord2f(1.0f, 1.0f);
    glVertex2f(posX + w, posY + h);

    glTexCoord2f(1.0f, 0.0f);
    glVertex2f(posX + w, posY    );

    glTexCoord2f(0.0f, 0.0f);
    glVertex2f(posX    , posY    );
glEnd();

glPushMatrix();

SDL_FreeSurface(text);
SDL_FreeSurface(textOutline);
SDL_FreeSurface(textAndOutline);
glDeleteTextures(1, &textTexture);

}
//-------------------------------------------------------------------------------------------------

Call to draw text:

Code:
visuals->DrawTextOntoScreenBuffer("?opyright 2010, By 16BitSoft?", visuals->FontDefault[0]
, 0, 420, JustifyCenter, 255, 255, 255, 90, 90, 90);

Thanks!

Jesse

Try using UTF-8-enabled rendering, e.g. TTF_RenderUTF8_Blended()

(This is from my experience with straight SDL + SDL_ttf or
SDL + SDL_Pango, with no OpenGL involved… but I assume that
shouldn’t matter :slight_smile: )

-bill!On Fri, May 28, 2010 at 06:57:45AM -0700, JeZ-l-Lee wrote:

When I try to print © and ® characters,
I get a strange character right before the © or ® character.

Hi,

Thanks for the suggestion, but it did not solve the problem.

Still have problem drawing © and ® characters
using SDL1.2+OpenGL+SDL_TTF

Anyone have any ideas?

JesseOn Fri, 2010-05-28 at 15:43 -0700, Bill Kendrick wrote:

On Fri, May 28, 2010 at 06:57:45AM -0700, JeZ-l-Lee wrote:

When I try to print © and ® characters,
I get a strange character right before the © or ® character.

Try using UTF-8-enabled rendering, e.g. TTF_RenderUTF8_Blended()

(This is from my experience with straight SDL + SDL_ttf or
SDL + SDL_Pango, with no OpenGL involved… but I assume that
shouldn’t matter :slight_smile: )

-bill!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

It looks like your source code is saved UTF-8 encoding. If you open it
in a hex editor I think you’ll see that the extra characters you’re
complaining about are present in the file. Saving the file in a
different encoding, like ISO 8859-1 (latin-1) would fix it.
Alternatively, keep your text pure ASCII and insert special symbols
such as these via printf using the %c conversion and their character
codes (0xA9 and 0xAE).

Andy Hefner wrote:

…keep your text pure ASCII and insert special symbols
such as these via printf using the %c conversion and their character
codes (0xA9 and 0xAE).
Hi,

I tried what you suggested, but it is not working for me.
The source compiles fine, but when I run it it crashes:

Code:
char *copy = char(NULL);
char *reg = char(NULL);
sprintf(copy, “%c”, 0xA9);
sprintf(reg, “%c”, 0xAE);
strcpy(visuals->VariableText, copy);
strcat(visuals->VariableText, “opyright 2010, By 16BitSoft”);
strcat(visuals->VariableText, reg);
visuals->DrawTextOntoScreenBuffer(visuals->VariableText, visuals->FontDefault[0]
, 0, 420, JustifyCenter, 255, 255, 255, 90, 90, 90);

How I fix this problem, thanks!

Jesse

Andy Hefner wrote:

…keep your text pure ASCII and insert special symbols
such as these via printf using the %c conversion and their character
codes (0xA9 and 0xAE).
Hi,

I tried what you suggested, but it is not working for me.
The source compiles fine, but when I run it it crashes:

Code:
char *copy = char(NULL);
char *reg = char(NULL);

You’re not allocating memory for these strings. Of course, you don’t need
to do it that way at all. You can create a string this way instead:

char copy[] = “\xA9”;

sprintf(copy, “%c”, 0xA9);
sprintf(reg, “%c”, 0xAE);

This dereferences null pointers, which crashes.

strcpy(visuals->VariableText, copy);
strcat(visuals->VariableText, “opyright 2010, By 16BitSoft”);
strcat(visuals->VariableText, reg);

How about:

strcpy(visuals->VariableText, “\xA9opyright 2010, by 16BitSoft\xAE”);

(Or strncpy or something safer with the proper sizes, but you get the
idea. I don’t actually know the definition of visuals, here, since it’s
not a complete code sample.)

visuals->DrawTextOntoScreenBuffer(visuals->VariableText, visuals->FontDefault[0]
, 0, 420, JustifyCenter, 255, 255, 255, 90, 90, 90);

-CrystalOn Sat, 29 May 2010, JeZ-l-Lee wrote:

Try something like this:

char message[256];
sprintf(message, “%copyright 2010, By 16BitSoft%c”, 0xA9, 0xAE);
visuals->DrawTextOntoScreenBuffer(message, visuals->FontDefault[0], 0,
420, JustifyCenter, 255, 255, 255, 90, 90, 90);

Normally I’d use snprintf, but Visual C++ renames it to _snprintf or
something, so I stuck with sprintf which is available everywhere.

If really need a copy in visuals->VariableText, add:
visuals->VariableText = strdup(message);