SDL_FillRect() problem

Hello

I just wrote two functions in my new class that, init(), creates a SDL_Surface
containing a string of text using a bitmap font and, draw(), blits that surface
onto the screen at the coordinates stored in te class.

Everyhing works fine and the text looks nice, but then I decided to make a
border around the text. So I made the surface that stores the string 2px bigger
(and nudged the letters 1px) and SDL_FillRect’ed it with white, and since the
bitmap letters have a black bg, that should make only the white border show.

The problem is that it doesn’t seem to matter how big I make the surface (with
SDL_CreateRGBSurface()), only the letters will show (and they still look nice!)

If I comment out the loop that blits the letters, nothing shows on the screen.

Have I misunderstood the SDL_FillRect(), or what is happening here?

Thanks! / Johan

(In the code below, the number 10s should be 2, but I exaggerated the size to
show what I mean)

from: main.cpp
star MyStar;
MyStar.init(“TestName”);
MyStar.setXY(20,20); // sets the coords stored in the class…
MyStar.draw(screen);
SDL_Flip(screen);
end: main.cpp

from: star.cpp
void star::init(string newName)
{
name = newName;

   // Make the name a image with the bitmapfont
 SDL_Surface *bitmapFont;
 bitmapFont = SDL_LoadBMP("bitmapfont/lemyellow.bmp");

Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif

  // *** This is where I create the surface ***
nameImg = SDL_CreateRGBSurface(SDL_SWSURFACE, 
                               name.length()*BMPFONT_CHARWIDTH + 10,// <--
                               BMPFONT_CHARHEIGHT + 10,             // <--
                               32, rmask, gmask, bmask, amask);
if(nameImg == NULL) 
{
    fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
    exit(1);
}

  // *** And here I try to fill it with white ***
SDL_FillRect(nameImg, NULL, 0xFFFFFF);

SDL_Rect src, dest;
src.w = BMPFONT_CHARWIDTH;
src.h = BMPFONT_CHARHEIGHT;
src.y = 2;
dest.y = 1;

for (int i = 0; i < name.length(); i++)
{
    dest.x = 1 + i*BMPFONT_CHARWIDTH;

    if (name[i] >= 33 && name[i] <= 126)
    {
       src.x = (name[i] - 33)*(BMPFONT_CHARWIDTH+1) + 1;
    }
    else
      src.x = 847;
   
   SDL_BlitSurface(bitmapFont, &src, nameImg, &dest);
}

}

void star::draw(SDL_Surface *screen)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(nameImg, NULL, screen, &dest);
}
end: star.cpp___________________________________________________________
Yahoo! Messenger - want a free and easy way to contact your friends online? http://uk.messenger.yahoo.com

0xffffff might not be white depending on your rmask, gmask, bmask, amask.
What happens if you try
SDL_FillRect(nameImg,NULL,SDL_MapRGB(nameImg->format,255,255,255)) instead?
And for that matter, why are you creating a surface of a specific bit-depth
and parameters instead of making it the same as your video surface?On Saturday 07 May 2005 03:50, Johan Ronstr?m wrote:

Hello

I just wrote two functions in my new class that, init(), creates a
SDL_Surface containing a string of text using a bitmap font and, draw(),
blits that surface onto the screen at the coordinates stored in te class.

Everyhing works fine and the text looks nice, but then I decided to make a
border around the text. So I made the surface that stores the string 2px
bigger (and nudged the letters 1px) and SDL_FillRect’ed it with white, and
since the bitmap letters have a black bg, that should make only the white
border show.

The problem is that it doesn’t seem to matter how big I make the surface
(with SDL_CreateRGBSurface()), only the letters will show (and they still
look nice!)

If I comment out the loop that blits the letters, nothing shows on the
screen.

Have I misunderstood the SDL_FillRect(), or what is happening here?

— Tyler Montbriand wrote:> On Saturday 07 May 2005 03:50, Johan Ronstr?m wrote:

Hello

I just wrote two functions in my new class that, init(), creates a
SDL_Surface containing a string of text using a bitmap font and, draw(),
blits that surface onto the screen at the coordinates stored in te class.

Everyhing works fine and the text looks nice, but then I decided to make a
border around the text. So I made the surface that stores the string 2px
bigger (and nudged the letters 1px) and SDL_FillRect’ed it with white, and
since the bitmap letters have a black bg, that should make only the white
border show.

The problem is that it doesn’t seem to matter how big I make the surface
(with SDL_CreateRGBSurface()), only the letters will show (and they still
look nice!)

If I comment out the loop that blits the letters, nothing shows on the
screen.

Have I misunderstood the SDL_FillRect(), or what is happening here?
0xffffff might not be white depending on your rmask, gmask, bmask, amask.
What happens if you try
SDL_FillRect(nameImg,NULL,SDL_MapRGB(nameImg->format,255,255,255)) instead?
And for that matter, why are you creating a surface of a specific bit-depth
and parameters instead of making it the same as your video surface?

I’m still on the level where I copy and paste from SDL tutorials… (Though my
general programming skills are more advanced).

It worked! Now it’s white (I still don’t understand how SDL_FillRect could make
it transparent before, though…)

Anyway, what would be the easiest way to make the new surface with the same
settings as the video surface?

Thanks for the help! / Johan


Yahoo! Messenger - want a free and easy way to contact your friends online? http://uk.messenger.yahoo.com

I’m still on the level where I copy and paste from SDL tutorials… (Though
my general programming skills are more advanced).

It worked! Now it’s white (I still don’t understand how SDL_FillRect could
make it transparent before, though…)
I don’t know either. Maybye it wasn’t transparent, just matching whatever’s
behind it instead.

Anyway, what would be the easiest way to make the new surface with the same
settings as the video surface?

SDL_Surface *nameImg2=SDL_DisplayFormat(nameImg);
SDL_FreeSurface(nameImg);
nameImg=nameImg2;

This will make blits to the display from this surface much faster since SDL
only converts it once, instead of every time it’s blit to the video surface.
In general SDL will do a lot of sanity checking and such for you but
depending on it comes with a performance cost.On Sunday 08 May 2005 03:16, Johan Ronstr?m wrote:

— Tyler Montbriand wrote:> On Sunday 08 May 2005 03:16, Johan Ronstr?m wrote:

I’m still on the level where I copy and paste from SDL tutorials… (Though
my general programming skills are more advanced).

It worked! Now it’s white (I still don’t understand how SDL_FillRect could
make it transparent before, though…)
I don’t know either. Maybye it wasn’t transparent, just matching whatever’s
behind it instead.

Anyway, what would be the easiest way to make the new surface with the same
settings as the video surface?

SDL_Surface *nameImg2=SDL_DisplayFormat(nameImg);
SDL_FreeSurface(nameImg);
nameImg=nameImg2;

This will make blits to the display from this surface much faster since SDL
only converts it once, instead of every time it’s blit to the video surface.

In general SDL will do a lot of sanity checking and such for you but
depending on it comes with a performance cost.

That worked, but isn’t there any way to make the surface of the right format in
the SDL_CreateRGBSurface? I have no reason for specifying the bit depth (or
anything other than the size), so maybe you can tip me on how you would have
done it?

All my Surfaces blit to eachother an then to the screen, but I don’t know what
is the best way to create/convert them.

Thanks! / Johan


How much free photo storage do you get? Store your holiday
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com

You asked for the easiest way. :wink: The best way would be this:

SDL_Surface *screen=SDL_GetVideoSurface();
nameImg=SDL_CreateRGBSurface(SDL_SWSURFACE,
? ? ?name.length()*BMPFONT_CHARWIDTH + 10,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?BMPFONT_CHARHEIGHT + 10,
screen->format->BitsPerPixel,
screen->format->Rmask,
screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask);On Sunday 08 May 2005 11:40, Johan Ronstr?m wrote:

— Tyler Montbriand <@Tyler_Montbriand> wrote:

SDL_Surface *nameImg2=SDL_DisplayFormat(nameImg);
SDL_FreeSurface(nameImg);
nameImg=nameImg2;

This will make blits to the display from this surface much faster since
SDL only converts it once, instead of every time it’s blit to the video
surface.

In general SDL will do a lot of sanity checking and such for you but
depending on it comes with a performance cost.

That worked, but isn’t there any way to make the surface of the right
format in the SDL_CreateRGBSurface? I have no reason for specifying the bit
depth (or anything other than the size), so maybe you can tip me on how you
would have done it?