[SDL] per-surface alpha and hardware surfaces?
Sean Ridenour
s_ridenour at kewlpc.org
Mon Dec 15 17:03:00 PST 2003
On Sunday 14 December 2003 7:22 pm, Warren Schwader wrote:
> Here is a simple and short little Win32 program that loads an 800 x 600
> background image and draws another image on top (movable using the mouse.)
> Then I use SetAlpha on the surface that I move with the mouse to fade it in
> and out.
>
> My question is: Is per-surface alpha supposed to work when using hardware
> surfaces? (or maybe it is related to double-buffering? - not sure.) Why
> doesn't the dSurface show? Yes I could convert all of my surfaces to
> per-alpha surfaces (and that works fine) but I'd rather not have to go that
> far.
>
> The problem is that the call to: SDL_SetAlpha(dSurface, SDL_SRCALPHA,
> alpha); makes it so that the 2nd image (dSurface) doesn't show. When this
> line is removed, everything is fine.
I don't think you can use color keying and alpha transparency at the same
time. You've got to pick one or the other, as far as I know.
-Sean Ridenour
> #include "sdl.h"
> #include "sdl_image.h"
>
> SDL_Surface* mainSurface;
> SDL_Event event;
>
> int main(int argc, char* argv[])
> {
> SDL_Init(SDL_INIT_EVERYTHING);
> mainSurface = SDL_SetVideoMode(800, 600, 32,
> SDL_SWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF);
>
> SDL_Surface* backSurface1 = IMG_Load("back.png");// load an 800 x 600
> image SDL_Surface* backSurface = SDL_DisplayFormat(backSurface1);
> SDL_FreeSurface(backSurface1);
>
> / / load a 300 x270 image. The transparent color is RGB 255, 0, 221
> SDL_Surface* dSurface1 = IMG_Load("dialogimage.png");
> SDL_Surface* dSurface = SDL_DisplayFormat(dSurface1);
> SDL_FreeSurface(dSurface1);
>
> SDL_SetColorKey(dSurface, SDL_SRCCOLORKEY,
> SDL_MapRGB(mainSurface->format, 255, 0, 221));
>
> int alpha = 0;
> int alphaAdjust = 2;
> SDL_Rect rect;
> rect.x = 0;
> rect.y = 100;
> rect.w = 300;
> rect.h = 270;
>
> while (true)
> {
> SDL_PollEvent(&event);
> if (event.type == SDL_KEYDOWN)
> break;
> if (event.type == SDL_MOUSEMOTION)
> {
> rect.x = event.motion.x;
> rect.y = event.motion.y;
> if (rect.x < 0) rect.x = 0;
> if (rect.y < 0) rect.y = 0;
> if (rect.x > 500) rect.x = 500;
> if (rect.y > 330) rect.y = 330;
> }
>
> // blit the background to the screen surface and then update the
> screen SDL_FillRect(mainSurface, NULL, SDL_MapRGB(mainSurface->format, 0,
> 0, 0)); SDL_BlitSurface(backSurface, NULL, mainSurface, NULL);
> SDL_SetAlpha(dSurface, SDL_SRCALPHA, alpha);
> SDL_BlitSurface(dSurface, NULL, mainSurface, &rect);
> SDL_UpdateRect(mainSurface, 0, 0, mainSurface->w, mainSurface->h);
> SDL_Flip(mainSurface);
>
> alpha += alphaAdjust;
> if (alpha < 0)
> {
> alpha = 0;
> alphaAdjust = 2;
> }
> if (alpha > 255)
> {
> alpha = 255;
> alphaAdjust = -2;
> }
> }
>
> SDL_FreeSurface(mainSurface);
> SDL_FreeSurface(backSurface);
> SDL_Quit();
> return(0);
> }
>
> Thanks,
> -- Warren Schwader
More information about the SDL
mailing list