Transparent Blitting, DirectDraw, Part 5

All this while, we have been blitting the spaceship image:

onto a black surface:

giving an image which looks like:

Thus giving an illusion that only the spaceship has been Blt on to the screen. But if our background was anything but black, things would look a lot different. For e.g. if our background was blue:

the resulting Blt() would have looked like this:

which, as you can see, is not very appealing to the eyes. Enter the concept of transparent blitting!

 

With transparent blitting, you can specify any arbitrary color, on the source surface or the destination surface or both, as a transparent color. So for e.g. in the above case we would specify the color BLACK on the source surface (the spaceship) as the transparent color. Therefore, when the spaceship is being Blt onto the destination surface, the color Black will be treated as transparent and the background will be preserved. So in short whenever the color black is encountered while Blting, its not Blt onto the destination surface, instead the Blue background color is displayed.

 

Color keys are used to specify which color you want to be treated as transparent. You can specify a range of colors to be transparent but, color ranges are not supported by HEL (Hardware Emulation Layer). Color keys can be specified:



1) While creating the surface

2) Calling SetColorKey() function explicitely on a surface

3) While Blting the surface

Color Keys are specified using the DDCOLORKEY structure. This structure has two DWORD members, dwColorSpaceLowValue and dwColorSpaceHighValue. Unless you are specifying a range of colors, both these values must be the same. If a surface is in palettized format then the color is specified as an index or a range of indices. If the surface’s pixel format is specified using a code then the color keys have to be specified using appropriate means like RGB(r, g, b) etc.





Color Keys while creating a surface

Assign the appropriate color values to ddckCKSrcBlt or ddckCKDestBlt members of the DDSURFACEDESC structure. Also, the dwFlags member must include DDSD_CKSRCBLT or DDSD_CKDESTBLT or both depending on whther you want to specify source color key, destination color key or both.



Using SetColorKey()

Another way to specify color keys is by calling SetColorKey() function on a surface directly. This function takes a pointer to DDCOLORKEY structure where you specify your color keys. In the dwFlags parameter you set either DDCKEY_SRCBLT or DDCKEY_DESTBLT or both to indicate whether you are setting a source color key or a destination color key or both.



While Bltting the surface

If the color keys for the surfaces you are bltting was already specified using one of the methods above then you just need to set the dwFlags parameter to DDBLT_KEYSRC or DDBLT_KEYDEST or both. Alternatively, if you want to override the color keys while blitting then you can specify the source and destination color keys by setting the ddckDestColorKey and ddckSrcColorKey members of the DDBLTFX structure, you will also need to specify DBLT_KEYSRCOVERRIDE or DBLT_KEYDESTOVERRIDE flags in the dwFlags parameter of the DDBLTFX structure.

 

In our UpdateFrame() function specify the color keys as:



    ddbltfx.ddckSrcColorkey.dwColorSpaceLowValue = RGB(0, 0, 0);

    ddbltfx.ddckSrcColorkey.dwColorSpaceHighValue = RGB(0, 0, 0);

Note that both the values are same. Here, the color black is specified as the source color key.

And the blitting,


        hRet = g_pDDSBack->Blt(&dest, g_pDDSOne, NULL, DDBLT_ROP | DDBLT_KEYSRCOVERRIDE, &ddbltfx);


The DDBLT_KEYSRCOVERRIDE flag is specified to use the color key from the ddbltfx structure.

 

So now when I Blit:

onto the same Blue surface, the resultant image is:

BallOnBlue

For more information on Transparent Blitting, take a look at MSDN.

Leave a Reply

Your email address will not be published. Required fields are marked *