DirectDraw™


Topics:


Setting up DirectDraw

Creating a DirectDraw object

A DirectDraw object represents the display adapter card. This is the first DirectDraw function that you will need to do in every program.

// Creating a DirectDraw object for the active display driver

LPDIRECTDRAW lpDD;
DirectDrawCreate( NULL, &lpDD, NULL );

You can call GetCaps() after this to determine to what extent this object is hardware optimized.

Running in a window or full screen

After you have a Direct Draw object, you'll want to set the cooperation level.

// Run this program in a window

lpDD->SetCooperativeLevel( hWnd, DDSCL_NORMAL );

-or-

// Run this program full screen at 640x480 with 256 colors

lpDD->SetCooperativeLevel( hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
lpDD->SetDisplayMode( 640, 480, 8 );

Fast Blting

coming soon...

Transparency

If you want to blt anything to the screen that isn't rectangular can either do some bit manipulations manually or use the built-in transparency methods.

Color keying

Color keying lets you set colors on a surface to be completely transparent. This lends itself quite nicely to sprite animation.

// Set white to be the transparent color 

DDCOLORKEY ColorKey;
ColorKey.dwColorSpaceLowValue = RGB(255,255,255);
ColorKey.dwColorSpaceHighValue = RGB(255,255,255);

surfaceMyBitmap->SetColorKey(DDCKEY_SRCBLT,&DDColorKey);

// Now we can blt the transparent surface to another surface

RECT rect;
SetRect(&rect,0,0,nBitmapWidth,nBitmapHieght);
surfacePrimary->Blt( &rect,surfaceMyBitmap, &rect, DDBLT_KEYSRC, NULL );

The DDCOLORKEY structure seems to like RGB colors and not palette indices. If you want to specify a range a transparent color (I have no idea why anyone would need to) you just set the DDCOLORKEY values the be the high and low color inclusive.

Alpha channels

Using an alpha channel allows you to make semi-transparent (blending) blts by directly storing alpha, or transparency, information in the a bitmap. This is usually done with a RGBA pixel format.

more coming soon...

Overlays

Ok, go over to you Nintendo and start playing any game. When you move around, do you notice how there are a couple of backgrounds that float around behind you, making the game appear to have a little depth? Those are overlays. Now with the proper hardware, you can do that sort of animation with DirectDraw too. But I don't have one of those cool new 3-D video cards, and until some cool reader of this page sends me one, this section is going to be a little sparse.

Clippers

The Game SDK explains exactly how to implement clippers and not in one place explains what they do. Am I the only one this has confused? Anyway clippers make it so that if a surface is under another window, the blt doesn't cover the window on top. This is important if you are not running full screen, don't you think?

// Create a clipper for the primary surface

HRESULT ddrval;

ddrval = lpDD->CreateClipper( 0, &lpClipper, NULL );

if( ddrval != DD_OK )
{
    TRACE("Cannot create clipper\n");
    return;
}

ddrval = lpClipper->SetHWnd( 0, hWnd );

if( ddrval != DD_OK )
{
    TRACE("Cannot set clipper's window\n");
    return;
}

ddrval = lpDisplaySurface->SetClipper( lpClipper );

if( ddrval != DD_OK )
{
    TRACE("Cannot attach clipper to primary surface\n");
    return;
}

Other Direct Draw Pages

Direct Hardware Control for Speedy Animation, Part I - Microsoft's excellent Direct Draw page. Includes code sample and diagrams.


Send your questions and comments to Tod Semple. Suggestions for new topics you would like to see here are greatly appreciated. Page last modified 10/09/97. This page has been accessed times.
© 1996
Tod Semple