Blitting surface to surface with alpha blending?

Seems to me that this question has an easy answer (forgive me) but I’ve
read the docs and checked the list archives and can’t figure out the
solution.

I create a surface using SDL_SetVideoMode. Then I create another surface
usin SDL_CreateRGBSurface. Onto the latter surface I draw a
semi-transparent rectangle (using the SDL_gfxPrimitives library.) I want
to blit this second surface onto the first surface and have the rectangle
alpha blended with the contents of the first surface. When I do the
blit, though, it doesn’t work as I’d expect.

Here’s basically the code I’m trying to do. (This is a conversion of the
OCaml code I actually use into C, so ignore any obvious syntax errors
like misspellings…)

mainWindow = setVideoMode(1280, 1024, 0, SDL_FORMAT);

/* Now create a pretty background */
background = SDL_LoadBMP(“picture.bmp”);
SDL_BlitSurface(background, NULL, mainWindow, NULL);

/* Create the test surface (we’re on a big endian computer) */
testSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 200, 200, 32,
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);

/* Use an SDL_gfxPrimitives call to create the box. Arguments
are surface, x, y, w, h, red, gr, bl, alpha. */
boxRGBA(testSurface, 0, 0, 200, 200, 255, 255, 255, 128);
SDL_UpdateRect(testSurface, 0, 0, 0, 0);

/* Now blit */
SDL_BlitSurface(testSurface, NULL, mainWindow, NULL);
SDL_UpdateRect(mainWindow, 0, 0, 0, 0);

What I want to get is that rectangle with alpha of 128 to be blended over
the background image. The above doesn’t do it. I’ve messed around with
SDL_SetAlpha on both surfaces, but that doesn’t seem to do it either.

Thanks for your time!!

Walter Rader

“Walter B. Rader” wrote:

I create a surface using SDL_SetVideoMode. Then I create another surface
usin SDL_CreateRGBSurface. Onto the latter surface I draw a
semi-transparent rectangle (using the SDL_gfxPrimitives library.) I want
to blit this second surface onto the first surface and have the rectangle
alpha blended with the contents of the first surface.

for RGBA->RGB blits, all you should have to do is to make sure SDL_SRCALPHA
is set on the source surface and then call BlitSurface

SDL_UpdateRect(testSurface, 0, 0, 0, 0);

you never need to update any surfaces other than the screen surface