How do you put an effect over an image?

I have a load of tiles displayed on the screen.

I would like to give a tile a transparrent blue colour when
selected with the mouse. Is there an easy way to do this with SDL
or do I need to make selected images of all the tiles and display those
when the mouse is pressed on a tile.

I would hope that there is a way of applying an effect to an image an
then
copying that to the surface. Replacing it with the original image after
the select has finnished.

TIA

Robert Harrison
IT Manager______________________________

Harrison Goddard Foote
Belgrave Hall
Belgrave Street
Leeds
LS2 8DD

T +44 113 233 0110
F +44 113 233 0101
M +44 774 094 7814

http://www.hgfip.com http://www.hgfip.com/

A Streetmap is available at
http://www.streetmap.co.uk/streetmap.dll?grid2map?X=430276
<http://www.streetmap.co.uk/streetmap.dll?grid2map?X=430276&Y=433962&arr
ow=Y&zoom=0> &Y=433962&arrow=Y&zoom=0

You could do something like:

SDL_Rect dest;

dest.x = X;
dest.y = Y;
dest.w = myTILE->w;
dest.h = myTILE->h;

SDL_FillRect(mySURFACE, &dest,
SDL_MapRGBA(mySURFACE->format, 0x00, 0x00, 0xFF, myALPHA));

(I think that’ll work.)

I’ve also done it like so:


SDL_SetAlpha(myBLUE_SURFACE, SDL_SRCALPHA, myALPHA);
SDL_BlitSurface(myBLUE_SURFACE, &src, myDEST_SURFACE, &dest);

-bill!On Mon, Mar 10, 2003 at 03:48:16PM -0000, Robert wrote:

I have a load of tiles displayed on the screen.


bill at newbreedsoftware.com Hire me!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/

I’m a bit crap at this stuff but could you explain the myALPHA var.

Or at least how I could create one.

TIA

Robert

Bill Kendrick wrote:>On Mon, Mar 10, 2003 at 03:48:16PM -0000, Robert wrote:

I have a load of tiles displayed on the screen.

You could do something like:

SDL_Rect dest;

dest.x = X;
dest.y = Y;
dest.w = myTILE->w;
dest.h = myTILE->h;

SDL_FillRect(mySURFACE, &dest,
SDL_MapRGBA(mySURFACE->format, 0x00, 0x00, 0xFF, myALPHA));

(I think that’ll work.)

I’ve also done it like so:


SDL_SetAlpha(myBLUE_SURFACE, SDL_SRCALPHA, myALPHA);
SDL_BlitSurface(myBLUE_SURFACE, &src, myDEST_SURFACE, &dest);

-bill!

It’s just a Uint8 (or int or unsigned char, or whatever)
between 0 and 255 :wink:

-bill!On Mon, Mar 10, 2003 at 05:08:30PM +0000, Robert Harrison wrote:

I’m a bit crap at this stuff but could you explain the myALPHA var.


bill at newbreedsoftware.com Hire me!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/

I do as sugested (Thanks) but it does not seem to affect the screen. At
least it appears not to.

I have tried with values of myALPHA set to 0 255 128 0x00 0xFF and
nothing appears to happen.

In the docs it says that if the colour value contains an alpha value
then the dest is filled with the alpha info.

Do I need another step to apply the blue.

Yours in ingnorance,

Robert

Exerpt from docs -----------------------

int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);

Description
This function performs a fast fill of the given rectangle with color. If
dstrect is NULL, the whole surface will be filled with color.

The color should be a pixel of the format used by the surface, and can
be generated by the SDL_MapRGB or SDL_MapRGBA functions. If the color
value contains an alpha value then the destination is simply "filled"
with that alpha information, no blending takes place.

Bill Kendrick wrote:>On Mon, Mar 10, 2003 at 03:48:16PM -0000, Robert wrote:

I have a load of tiles displayed on the screen.

You could do something like:

SDL_Rect dest;

dest.x = X;
dest.y = Y;
dest.w = myTILE->w;
dest.h = myTILE->h;

SDL_FillRect(mySURFACE, &dest,
SDL_MapRGBA(mySURFACE->format, 0x00, 0x00, 0xFF, myALPHA));

(I think that’ll work.)

I’ve also done it like so:


SDL_SetAlpha(myBLUE_SURFACE, SDL_SRCALPHA, myALPHA);
SDL_BlitSurface(myBLUE_SURFACE, &src, myDEST_SURFACE, &dest);

-bill!

OK ok shoot me!!!

I forgot the SDL_UpdateRect (…)

I said I was an ignorant beginner :wink:

OK Still struggling.

I know, I know, I should give up now.

I have

SDL_Rect dest;

dest.x = 100;
dest.y = 100;
dest.w = 100;
dest.h = 100;

SDL_FillRect (screen, &dest,
SDL_MapRGBA(screen->format,0xFF,0x00,0xFF,myALPHA));

SDL_UpdateRect (screen, 100,100,100,100);

This gives me a lovely solid pink colour no matter what value I set
myALPHA to.
any ideas anyone.

Do I need to set the screen somehow to allow alpha filling to work?

Bill Kendrick wrote:>On Mon, Mar 10, 2003 at 03:48:16PM -0000, Robert wrote:

I have a load of tiles displayed on the screen.

You could do something like:

SDL_Rect dest;

dest.x = X;
dest.y = Y;
dest.w = myTILE->w;
dest.h = myTILE->h;

SDL_FillRect(mySURFACE, &dest,
SDL_MapRGBA(mySURFACE->format, 0x00, 0x00, 0xFF, myALPHA));

(I think that’ll work.)

I’ve also done it like so:


SDL_SetAlpha(myBLUE_SURFACE, SDL_SRCALPHA, myALPHA);
SDL_BlitSurface(myBLUE_SURFACE, &src, myDEST_SURFACE, &dest);

-bill!

I think what you need to do really (what I did) is create an SDL_Surface of
the required size (code for doing this is in the docs, I believe). For
example 32 x 32

After you had created the surface, fill it entirely with the colour you want
(for example red - 0xff, 0x00, 0x00).

You should now have a Surface of 32 * 32 size of a solid red colour

Finally, set the surface’s alpha value using SDL_SetAlpha()

You’ll now have a transparent red surface of 32 x 32 which you can put over
your tile when it is clicked.

You’ll have to be using a graphics depth of more than 8 bits to get alpha
blending to work properly.

I’ve got a function for my game that will do all the above for me and return
the surface. I just give the function a width and height and the colour I
want the surface to be.

Hope that’s what you’re looking to do… :slight_smile:

actually… here’s the two functions I have… hack them up as you need :slight_smile:

SDL_Surface *createSurface(int width, int height)
{
SDL_Surface *surface, *newImage;
Uint32 rmask, gmask, bmask, amask;

/* SDL interprets each pixel as a 32-bit number, so our masks must

depend
on the endianness (byte order) of the machine */
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif

surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,

rmask, gmask, bmask, amask);

if (surface == NULL) {
	fprintf(stderr, "CreateRGBSurface failed: %s\n",

SDL_GetError());
exit(1);
}

newImage = SDL_DisplayFormat(surface);

SDL_FreeSurface(surface);

return newImage;

}

SDL_Surface *alphaRect(int width, int height, Uint8 red, Uint8 green, Uint8
blue)
{
SDL_Surface *surface = createSurface(width, height);

SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, red, green,

blue));

SDL_SetAlpha(surface, SDL_SRCALPHA|SDL_RLEACCEL, 128);

return surface;

}> -----Original Message-----

From: Robert Harrison [SMTP:rharrison at hgfip.com]
Sent: Tuesday, March 11, 2003 10:55 AM
To: sdl at libsdl.org
Subject: Re: [SDL] How do you put an effect over an image?

OK Still struggling.

I know, I know, I should give up now.

I have

SDL_Rect dest;

dest.x = 100;
dest.y = 100;
dest.w = 100;
dest.h = 100;

SDL_FillRect (screen, &dest,
SDL_MapRGBA(screen->format,0xFF,0x00,0xFF,myALPHA));

SDL_UpdateRect (screen, 100,100,100,100);

This gives me a lovely solid pink colour no matter what value I set
myALPHA to.
any ideas anyone.

Do I need to set the screen somehow to allow alpha filling to work?

Bill Kendrick wrote:

On Mon, Mar 10, 2003 at 03:48:16PM -0000, Robert wrote:

I have a load of tiles displayed on the screen.

You could do something like:

SDL_Rect dest;

dest.x = X;
dest.y = Y;
dest.w = myTILE->w;
dest.h = myTILE->h;

SDL_FillRect(mySURFACE, &dest,
SDL_MapRGBA(mySURFACE->format, 0x00, 0x00, 0xFF,
myALPHA));

(I think that’ll work.)

I’ve also done it like so:


SDL_SetAlpha(myBLUE_SURFACE, SDL_SRCALPHA, myALPHA);
SDL_BlitSurface(myBLUE_SURFACE, &src, myDEST_SURFACE, &dest);

-bill!


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

.sophos.3.65.03.11.

Thankyou all.

I got a bit confused along the way.

The way to do it as explained in above posts

have a surface the size of the tile

fill it with a colour (blue)

set the alpha on this 128 (good choice as it is fast)

blit surface to screen

update rect

Thanks everyone.

Now I can get on …