DirectX:DirectDraw:Tutorials:VB:DX7:Easy Transparency

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Direct Draw Colour Transparency Made Easy

Whatever the mind can conceive and believe it can achieve.

Now, at some stage you've wanted to make something in DirectDraw transparent, normally, you'd have to make sure that something easy to colour key, like black, was your transparent colour, even getting magenta as your transparent colour is a pain.

But, there is an easier way, where you let DirectDraw do the dirty work for you.

There are two ways of implementing this solution:
1. Make the 0,0 (or any other pixel) always equal to your transparent colour
2. In whatever you use to save your sprite just add a X and Y location of a transparent pixel, your editor will have to assign values if you use this method.

Then once you've done either of these two things you just add this code in:

Dim ccGeneric As DDCOLORKEY, rectEmpty as RECT
          
        'Set the colour key
        msurfNPC(i).Lock rectEmpty, ddsdGeneric, DDLOCK_WAIT, 0
             
        'replace x,y with pixel co-ordinates of a pixel containing
        'the colour you want to make transparent
        ccGeneric.high = msurfNPC(i).GetLockedPixel(X,Y) 
        ccGeneric.low = ccGeneric.high
        msurfNPC(i).Unlock rectEmpty
        msurfNPC(i).SetColorKey DDCKEY_SRCBLT, ccGeneric

I really like this way because it will work no matter what bit-depth the program is running at, and no matter what type of graphics card your user has (some graphics cards have strange colour modes)

--IGTHORN