DirectX SDK |
This section pertains only to application development in Visual Basic. See DirectDraw C/C++ Tutorials.
This step involves creating and initializing two more surfaces, one for the background image and one for the sprite image. Additionally, we will specify the transparent color key to the sprite surface.
Both surfaces are off-screen surfaces created with a call to the DirectDraw7.CreateSurfaceFromFile method. The first surface created, objDDLakeSurf, is for the background image and has the same height and width as the Picture1 picture box control on the form. The second surface, objDDSpriteSurf, contains the sprite image which will eventually be composed with the background image on the back buffer. These surfaces are created with the following statements:
'Load a background image of the lake 'Indicate that we want to create an off-screen surface 'An off-screen surface is one that is available in memory '(video or system memory) but is not visible to the user ddsdLake.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT ddsdLake.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN ddsdLake.lWidth = Picture1.Width ddsdLake.lHeight = Picture1.Height 'Create the surface and load lake.bmp onto the surface Set objDDLakeSurf = objDD.CreateSurfaceFromFile("lake.bmp", ddsdLake) 'Copy the background to the compositing surface RepaintEntireBackground 'Load a sprite image of a flying dog ddsdSprite.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT ddsdSprite.lWidth = 64 ddsdSprite.lHeight = 64 ddsdSprite.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Set objDDSpriteSurf = objDD.CreateSurfaceFromFile("disk1.bmp", ddsdSprite)
Next we set the transparent color key for the sprite surface. The color key is specified through a DDCOLORKEY type variable. This type has two members, low and high, both Longs. You can specify a range of values to be used as the color key or a single color by passing the same values to both members. In the Tutorial 2 sample both the high and low values are 0, indicating that we want solid black to be the color key. The color key is set on a surface by calling the DirectDrawSurface7.SetColorKey method.
There are two types of color keys you can specify on a surface. A source color key is a color that (in the case of blitting) is not copied to, or (in the case of overlays) not visible on, the destination surface. A destination color key is a color that (in the case of blitting) is replaced or (in the case of overlays) is covered on the destination surface. The Tutorial 2 sample uses a source color key. The steps in setting a color key are as follows:
'Set the transparent color of the sprite Dim key As DDCOLORKEY key.low = 0 key.high = 0 'Assign the transparent color to the sprite object 'DDCKEY_SRCBLT specifies that when a blt is done the 'transparent color is associated with the surface being 'blitted and not the one being blitted to objDDSpriteSurf.SetColorKey DDCKEY_SRCBLT, key