How do you clear a surface?

Hi Guys
I was playing with some surfaces last night and came up with a
question i would like to ask. How do you clear a surface? i know that you
can use SDL_FillRect to fill it to a colour, but that’s not what i mean! i
want to clear the whole surface so i can blit many images to make a
composite image with a transparent background, so i build the image once and
blit many times. clear the image and change the image and blit again many
times.

I don’t want to use convert surface, as creating and freeing surfaces will
just munch its way through memory, so filling with 0x0FF000000 creates a
black image, filling with 0x000000000 makes its so nothing can be blitted to
the image, or am i missing something?

Trish

PS on the poll of what is SDL missing, i think it should have a function
like SDL_MakeGame(Game GameType, Players NPlayers); where GameType would be
’Shoot Em Up’ or ‘Platformer’ or ‘Puzzle’ :p-------------------------------------
if it’s not broken don’t fix it’

Hi Guys

     I was playing with some surfaces last night and came up with a

question i would like to ask. How do you clear a surface? i know that you
can use SDL_FillRect to fill it to a colour, but that’s not what i mean! i
want to clear the whole surface so i can blit many images to make a
composite image with a transparent background, so i build the image once and
blit many times. clear the image and change the image and blit again many
times.

I am not sure I follow you. You say you don?t want to use SDL_FillRect(),
but that is the simplest (and fastest) way to take your initial surface and
clear it to your transparent background color. SDL_FillRect() does a direct
memory copy of the pixel value you pass in so it should do what you want.
You can then blit all of your other images to that surface and will be left
with an image that has transparency everywhere it should be and your other
pieces on top of that. If I have misunderstood then please explain rather
than bash me for being dense.

You can always look at SDL_pixels.c in the SDL-1.2.11\src\video directory to
see how Sam implemented his SDL_FillRect() routine.

If you want a non-platform independent implementation that you can play
around with and optimize for your particular platform you can get it here:

HYPERLINK
"http://www.homebrewsoftware.com/CodeSamples/ClearSurface.cpp"http://www.hom
ebrewsoftware.com/CodeSamples/ClearSurface.cpp

Ken RogowayHomebrew Software–
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.13/726 - Release Date: 3/18/2007
3:34 PM

Patricia Curtis escreveu:

i
want to clear the whole surface so i can blit many images to make a
composite image with a transparent background, so i build the image
once and
blit many times. clear the image and change the image and blit again many
times.

The first step to transparency is the alpha channel; then you have to
choose what happens when you blend two images with alpha channels. There
are more than one way to do this, and SDL has some of them hard-coded:
http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSetAlpha
See the third note, in the end, about RGBA->RGBA (with SDL_SRCALPHA).

For example, in OpenGL you have a finer control over how the alpha
channel is used. High-level drawing API also usually have advanced
compositing functions.

So my suggestion is to either:

  • write your own blending routine that does whatever you think is right.

  • use a higher-level library (possibly over SDL), like AGG[2D], for
    composing the images.—
    Daniel K. O.

Hi Guys
I was playing with some surfaces last night and came up with a
question i would like to ask. How do you clear a surface? i know that you
can use SDL_FillRect to fill it to a colour, but that’s not what i mean! i
want to clear the whole surface so i can blit many images to make a
composite image with a transparent background.

I have only started using SDL two weeks ago or so, and I ended up creating a
set of background functions, which allows me to display layered backgrounds,
not unlike what you try to do I think?

I think you are getting confused with the way transparent pixels work. In my
code, all my backgrounds are SDL_Surfaces with a specific transparent color.
The final Screen surface however does not have a transparent color
specified, and so there is no need to worry about anything.

And even if the final screen had a transparent color specified, it would
still fill the screen up with that particular value using SDL_FillRect.

Cheers,
Peter

Patricia Curtis <patricia.curtis gmail.com> writes:

Hi Guys
I was playing with some surfaces last night and came up with a
question i would like to ask. How do you clear a surface? i know that you can
use SDL_FillRect to fill it to a colour, but that’s not what i mean! i want to
clear the whole surface so i can blit many images to make a composite image with
a transparent background, so i build the image once and blit many times. clear
the image and change the image and blit again many times.

I don’t want to use convert surface, as creating and freeing surfaces will
just munch its way through memory, so filling with 0x0FF000000 creates a black
image, filling with 0x000000000 makes its so nothing can be blitted to the
image, or am i missing something?

Trish

I think you are referring to per-image alpha blending. Look at
http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSetAlpha to see the documentation
for SDL_SetAlpha() and see if that is what you’re looking for.