SDL_TTF question

Hello,

I am using the SDL_TTF library and I have noticed something strange:
In my source, I declare a variable like this

SDL_Color color = { 0xFF, 0xDB, 0x0A, 0 };

and I am using the TTF_RenderText_Shaded in order to print out
a string on the screen surface.

When using the TTF_RenderText_Blended in order to print out
the same string, the color is different, and I must invert
the red and blue component in order to obtain the correct result.

In order not to modify my color variable, should I check
the PixelFormat of my back buffer surface and eventually
convert it to the screen surface before blitting it or is it
a normal behavior ??

I am using SDL 1.2.1 and SDL_TTF 2.0.3 on top of XFree 4.1.0.

Thanks in advance,

Damien.

On Sun Jul 22, 2001 at 10:40:42PM +0200, the boisterous
Damien Mascr?
wrote to me:

SDL_Color color = { 0xFF, 0xDB, 0x0A, 0 };

When using the TTF_RenderText_Blended in order to print out
the same string, the color is different, and I must invert
the red and blue component in order to obtain the correct result.

I am using SDL 1.2.1 and SDL_TTF 2.0.3 on top of XFree 4.1.0.

There’s a bug in SDL_ttf 2.0.x that prevents you from getting the right
color values. Here’s a quick patch for 2.0.3.

To the ttf maintainer: please fix this behaviour in SDL_ttf. This bug is
known since 15 May 2001 and while trying to patch SDL_ttf, nobody seems
to include it (maybe a bug in my patch) in the official version. This patch
works perfect on an ia32 with linux and various freetype2 version.

so long
Krenni–
___ Obviously we do not want to leave zombies around.
/\ - W. Richard Stevens
( ^ >
/ \ Thomas Krennwallner
(
/) Fingerprint: 9484 D99D 2E1E 4E02 5446 DAD9 FF58 4E59 67A1 DA7B
-------------- next part --------------
diff -Naur SDL_ttf-2.0.3.orig/SDL_ttf.c SDL_ttf-2.0.3/SDL_ttf.c
— SDL_ttf-2.0.3.orig/SDL_ttf.c Wed May 23 02:46:51 2001
+++ SDL_ttf-2.0.3/SDL_ttf.c Wed Jun 20 15:44:41 2001
@@ -1097,7 +1097,6 @@
const Uint16 *ch;
Uint8 *src;
Uint32 *dst;

  • Uint32 Rmask, Gmask, Bmask, Amask;
    int row, col;
    c_glyph *glyph;
    FT_Error error;
    @@ -1109,20 +1108,8 @@
    }
    height = font->height;

  • /* Create the target surface, 32-bit ARGB format */

  • if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {

  •   Rmask = 0x000000FF;
    
  •   Gmask = 0x0000FF00;
    
  •   Bmask = 0x00FF0000;
    
  •   Amask = 0xFF000000;
    
  • } else {

  •   Rmask = 0xFF000000;
    
  •   Gmask = 0x00FF0000;
    
  •   Bmask = 0x0000FF00;
    
  •   Amask = 0x000000FF;
    
  • }
    textbuf = SDL_AllocSurface(SDL_SWSURFACE, width, height, 32,

  •                          Rmask, Gmask, Bmask, Amask);
    
  •              0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
    
    if ( textbuf == NULL ) {
    return(NULL);
    }
    @@ -1163,7 +1150,7 @@
    row = (textbuf->h-1) - font->underline_height;
    }
    dst = (Uint32 *)textbuf->pixels + row * textbuf->pitch/4;
  •   pixel |= Amask;
    
  •   pixel |= 0xFF000000; /* Amask */
      for ( row=font->underline_height; row>0; --row ) {
      	for ( col=0; col < textbuf->w; ++col ) {
      		dst[col] = pixel;
    

@@ -1181,7 +1168,6 @@
Uint32 pixel;
Uint8 *src;
Uint32 *dst;

  • Uint32 Rmask, Gmask, Bmask, Amask;
    int row, col;
    FT_Error error;
    c_glyph *glyph;
    @@ -1193,21 +1179,9 @@
    }
    glyph = font->current;

  • /* Create the target surface, 32-bit ARGB format */

  • if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {

  •   Rmask = 0x000000FF;
    
  •   Gmask = 0x0000FF00;
    
  •   Bmask = 0x00FF0000;
    
  •   Amask = 0xFF000000;
    
  • } else {

  •   Rmask = 0xFF000000;
    
  •   Gmask = 0x00FF0000;
    
  •   Bmask = 0x0000FF00;
    
  •   Amask = 0x000000FF;
    
  • }
    textbuf = SDL_CreateRGBSurface(SDL_SWSURFACE,
    glyph->pixmap.width, glyph->pixmap.rows, 32,

  •                          Rmask, Gmask, Bmask, Amask);
    
  •              0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
    
    if ( ! textbuf ) {
    return(NULL);
    }
    @@ -1230,7 +1204,7 @@
    row = (textbuf->h-1) - font->underline_height;
    }
    dst = (Uint32 *)textbuf->pixels + row * textbuf->pitch/4;
  •   pixel |= Amask;
    
  •   pixel |= 0xFF000000; /* Amask */
      for ( row=font->underline_height; row>0; --row ) {
      	for ( col=0; col < textbuf->w; ++col ) {
      		dst[col] = pixel;

To the ttf maintainer: please fix this behaviour in SDL_ttf. This bug is
known since 15 May 2001 and while trying to patch SDL_ttf, nobody seems
to include it (maybe a bug in my patch) in the official version. This patch
works perfect on an ia32 with linux and various freetype2 version.

I’d say it would be better to change the values in [RGBA]mask instead of
hardcoding values; if I read your patch right, the current SDL_ttf code
has the wrong color mask order, but your patch will break on bigendian
machines like the PowerPC.

Perhaps the attached patch is better?

–ryan.

-------------- next part --------------
diff -ur SDL_ttf-2.0.3-virgin/SDL_ttf.c SDL_ttf-2.0.3/SDL_ttf.c
— SDL_ttf-2.0.3-virgin/SDL_ttf.c Wed Jun 20 22:22:00 2001
+++ SDL_ttf-2.0.3/SDL_ttf.c Sun Jul 22 23:49:10 2001
@@ -1111,15 +1111,15 @@

/* Create the target surface, 32-bit ARGB format */
if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
  •   Rmask = 0x000000FF;
    
  •   Gmask = 0x0000FF00;
    
  •   Bmask = 0x00FF0000;
      Amask = 0xFF000000;
    
  •   Rmask = 0x00FF0000;
    
  •   Gmask = 0x0000FF00;
    
  •   Bmask = 0x000000FF;
    
    } else {
  •   Rmask = 0xFF000000;
    
  •   Gmask = 0x00FF0000;
    
  •   Bmask = 0x0000FF00;
      Amask = 0x000000FF;
    
  •   Rmask = 0x0000FF00;
    
  •   Gmask = 0x00FF0000;
    
  •   Bmask = 0xFF000000;
    

    }
    textbuf = SDL_AllocSurface(SDL_SWSURFACE, width, height, 32,
    Rmask, Gmask, Bmask, Amask);
    @@ -1195,16 +1195,17 @@

    /* Create the target surface, 32-bit ARGB format */
    if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {

  •   Rmask = 0x000000FF;
    
  •   Gmask = 0x0000FF00;
    
  •   Bmask = 0x00FF0000;
      Amask = 0xFF000000;
    
  •   Rmask = 0x00FF0000;
    
  •   Gmask = 0x0000FF00;
    
  •   Bmask = 0x000000FF;
    
    } else {
  •   Rmask = 0xFF000000;
    
  •   Gmask = 0x00FF0000;
    
  •   Bmask = 0x0000FF00;
      Amask = 0x000000FF;
    
  •   Rmask = 0x0000FF00;
    
  •   Gmask = 0x00FF0000;
    
  •   Bmask = 0xFF000000;
    
    }+
    textbuf = SDL_CreateRGBSurface(SDL_SWSURFACE,
    glyph->pixmap.width, glyph->pixmap.rows, 32,
    Rmask, Gmask, Bmask, Amask);

There’s a bug in SDL_ttf 2.0.x that prevents you from getting the right
color values. Here’s a quick patch for 2.0.3.

To the ttf maintainer: please fix this behaviour in SDL_ttf. This bug is
known since 15 May 2001 and while trying to patch SDL_ttf, nobody seems
to include it (maybe a bug in my patch) in the official version. This patch
works perfect on an ia32 with linux and various freetype2 version.

Yeah, sorry about that.
I’ve included the patch in CVS, which can be downloaded from:
http://www.libsdl.org/cvs/SDL_ttf-2.0.4-CVS.tar.gz

BTW, Ryan, the mask hard coding is fine, because of the way the color
pixel value is calculated.

See ya!
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

How can I go about getting SDL_TTF to print an integer/long to the screen? Let’s
say I have a game where a score is tallied every time you hit something, and
this score is stored as an int. I want to display this score on screen.

The problem is this. SD_TTF’s TTF_RenderText_Solid() looks like this
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg)

but for it to work for me, I need it to look like
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, int myScore, SDL_Color fg)

How do I go about getting this to work?

no idea how this got posted twice, sorry. :frowning:

Try using itoa() for a quick solution.

http://www.cplusplus.com/ref/cstdlib/itoa.html

HTH.On Mar 27, 2005, at 3:58 AM, James OMeara wrote:

How can I go about getting SDL_TTF to print an integer/long to the
screen? Let’s
say I have a game where a score is tallied every time you hit
something, and
this score is stored as an int. I want to display this score on screen.

The problem is this. SD_TTF’s TTF_RenderText_Solid() looks like this
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text,
SDL_Color fg)

but for it to work for me, I need it to look like
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, int myScore,
SDL_Color fg)

How do I go about getting this to work?


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

Quoth Jericho Hasselbush <j.seven at gmail.com>, on 2005-03-27 17:11:53 -0500:

Try using itoa() for a quick solution.

http://www.cplusplus.com/ref/cstdlib/itoa.html

On that very page:

Portability.
Not defined in ANSI-C. Supported by some compilers.

Don’t use itoa. Use sprintf, which is part of ISO C89, or snprintf,
which is part of ISO C99. snprintf has the advantage that it cannot
overflow your output buffer. 64 bits take at most 64 bytes to
represent in any integer base >= 2, and I doubt any current compilers
use larger than 64-bit values for int or long, so allocating 128 or
256 bytes on the stack should work well enough:

char output[256];
snprintf(output, 256, “%d”, number);
/* use output as the string */

If you need bases other than decimal, hexadecimal, and octal, you can
write your own conversion routine; it’s not difficult.

If you’re using C++, I believe you can create a string ostream, write
the number to it, and then pull the string out of the stream object
somehow, then use .c_str() to obtain a C string from that, but I don’t
know exactly how that would be implemented.

But this is getting off-topic…

—> Drake Wilson
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050327/1b0b46c5/attachment.pgp