[SDL] How to gray out a button image?
Ken Rogoway
Ken at HomebrewSoftware.com
Tue Apr 8 17:41:36 PDT 2008
The easiest way is to keep track of the button state and if it is disabled,
you draw a grayed out version of the button. If you are using C++ you can
write a simple button class to handle tracking the state info, but if you
are using C you can still do the same thing but the data will need to be
stored someplace else.
Below is an example. This is not meant to be a complete solution, but
rather enough for you to understand the approach I am suggesting:
// Simple C++ button draw. All vars used are class members. You can also
// get the values to use in the dstRect directly from the SDL_Surface, but
// that is beyond the scope of this example.
void Button::Draw()
{
SDL_Rect dstRect;
dstRect.x = btnX; // Class member for button X
dstRect.y = btnY; // Class member for button Y
dstRect.w = btnW; // Class member for button Width
dstRect.h = btnH; // Class member for button Height
switch( btnState )
{
case eButtonUp:
case eButtonDown:
case eButtonDisabled:
SDL_BlitSurface( btnImages[btnState],NULL,screen,&dstRect );
break;
default:
assert( false );
}
}
// Simple C version of button draw.
// Array of button images is passed in along with the state and X, Y
location
// You can also get the values to use in the dstRect directly from the
// SDL_Surface, but that is beyond the scope of this example.
void DrawButton( SDL_Surface *btnImages,int btnX,int btnY,int btnState )
{
SDL_Rect dstRect;
dstRect.x = btnX;
dstRect.y = btnY;
dstRect.w = 64; // Hard coded, you would either pass this in or get
it from the image
dstRect.h = 32; // Hard coded, you would either pass this in or get
it from the image
switch( btnState )
{
case eButtonUp:
case eButtonDown:
case eButtonDisabled:
SDL_BlitSurface( btnImages[btnState],NULL,screen,&dstRect );
break;
default:
assert( false );
}
}
I didn't compile any of this so if there are typos I apologize. I hope this
helps you.
Ken Rogoway
Homebrew Software
HYPERLINK "http://www.homebrewsoftware.com/"http://www.homebrewsoftware.com/
-----Original Message-----
From: sdl-bounces at lists.libsdl.org [HYPERLINK
"mailto:sdl-bounces at lists.libsdl.org"mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Erik
Sent: Tuesday, April 08, 2008 6:27 PM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: [SDL] How to gray out a button image?
We need to gray out[1] buttons with images on in our toolkit. I just wonder
what is the best way to gray out the image. Has anyone done that before?
Does it need SDL_gfx or just SDL?
[1] [HYPERLINK
"http://en.wikipedia.org/wiki/Grayed_out"http://en.wikipedia.org/wiki/Grayed
_out]
_______________________________________________
SDL mailing list
SDL at lists.libsdl.org
HYPERLINK
"http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org"http://lists.libsdl.org
/listinfo.cgi/sdl-libsdl.org
No virus found in this incoming message.
Checked by AVG.
Version: 7.5.519 / Virus Database: 269.22.9/1365 - Release Date: 4/8/2008
7:30 AM
No virus found in this outgoing message.
Checked by AVG.
Version: 7.5.519 / Virus Database: 269.22.9/1365 - Release Date: 4/8/2008
7:30 AM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080408/72678de2/attachment.html
More information about the SDL
mailing list