DirectDraw Architectural Overview
DirectDraw is made up of the DirectDraw class and its associated classes and methods that allow you to create related DirectX classes. These classes include palettes, clippers, and surfaces. Palettes are created by using the DirectDrawPalette class, surfaces by using the DirectDrawSurface class, and clippers by the DirectDrawClipper class. Figure 1 shows the relationship between these classes and DirectDraw.
Figure 1 DirectDraw and its supporting classes.
The DirectDraw Class
DirectDraw is the core class of DirectX. After declaring a DirectX class, you can then obtain a DirectDraw class. With this class, you can then obtain its subclasses, such as DirectDrawSurface and DirectDrawPalette.
The following code syntax is an example of declaring and using the DirectDraw class:
Dim oDD As DxVBLib.DirectDraw7
This is how the code would look when included with the DirectX declaration:
Dim oDX As DxVBLib.DirectX7 = New DxVBLib.DirectX7() Dim oDD As DxVBLib.DirectDraw7
NOTE
As you might have noticed in the previous example, I declared the DirectDraw class as DirectDraw7 because DirectDraw is no longer being updated in DirectX 8. For compatibility reasons, the class is always called DirectDraw7.
The DirectDraw Methods
The DirectDraw class gives you access to many methods. The most commonly used methods are detailed in Table 1.
Table 1 The Most Commonly Used DirectDraw Methods
Method |
Syntax |
Purpose |
CreateClipper |
DirectDraw7.CreateClipper |
Creates a DirectDrawClipper class. |
CreatePalette |
DirectDraw7.CreatePalette |
Creates a DirectDrawPalette class. |
CreateSurface |
DirectDraw7.CreateSurface |
Creates a DirectDrawSurface class. |
CreateSurfaceFromFile |
DirectDraw7.CreateSurfaceFromFile |
Creates a DirectDrawSurface class and attaches the specified bitmap image to this new class. |
GetDisplayMode |
DirectDraw7.GetDisplayMode |
Finds the current display mode settings. |
LoadPaletteFromBitmap |
DirectDraw7.LoadPaletteFromBitmap |
Creates a DirectDrawPalette class, based on the palette of the specified bitmap for this DirectDraw class. |
RestoreAllSurfaces |
DirectDraw7.RestoreAllSurfaces |
Restores all the surfaces created for the DirectDraw class. |
SetDisplayMode |
DirectDraw7.SetDisplayMode |
Sets the display mode of the video card. |
The DirectDrawSurface Class
DirectDrawSurface is like your canvas; it's the surface you use to assemble your animation frames on. You can have multiple surfaces to create the illusion of animation through page flipping. You can then flip each page in succession and assemble a back buffer surface in the background.
The following code syntax is an example of declaring and using the DirectDrawSurface class:
Dim oDDPrimSurf As DxVBLib.DirectDrawSurface7
This is what the code would look like when included with the DirectX and DirectDraw declarations:
'''First you need to access the DirectX 7 COM class Dim oDX As DxVBLib.DirectX7 = New DxVBLib.DirectX7() '''Next you need to create a DirectDraw class Dim oDD As DxVBLib.DirectDraw7 '''Then you need to create a DirectDrawSurface class Dim oDDPrimSurf As DxVBLib.DirectDrawSurface7
DirectDrawSurface gives you access to many methods. Table 2 outlines some of the more commonly used ones.
Table 2 The Most Commonly Used DirectDrawSurface Methods
Method |
Syntax |
Purpose |
AddAttachedSurface |
DirectDrawSurface7.AddAttachedSurface |
Attaches a specified surface to the current surface. |
Blt |
DirectDrawSurface7.Blt |
Performs a bit block transfer (blit). |
BltColorFill |
DirectDrawSurface7.BltColorFill |
Performs a bit block transfer (blit) of a single color to the specified destination rectangle. |
BltFast |
DirectDrawSurface7.BltFast |
Performs a source copy blit or transparent blit by using a source color key or destination color key. |
BltFx |
DirectDrawSurface7.BltFx |
Performs a bit block transfer with additional blit effects. |
DrawText |
DirectDrawSurface7.DrawText |
Draws text on the current surface. |
Flip |
DirectDrawSurface7.Flip |
Performs a memory swap with the back buffer surface and the front buffer surface. |
GetCaps |
DirectDrawSurface7.GetCaps |
Finds out the capabilities of the current surface. |
GetClipper |
DirectDrawSurface7.GetClipper |
Retrieves the DirectDrawClipper class associated with the current surface. |
GetColorKey |
DirectDrawSurface7.GetColorKey |
Retrieves the color key value for the current surface. |
GetDirectDraw |
DirectDrawSurface7.GetDirectDraw |
Retrieves the DirectDraw class used to create the surface. |
GetDirectDrawGammaControl |
DirectDrawSurface7.GetDirectDrawGammaControl |
Returns the DirectDrawGammaControl class used with the surface. |
GetPalette |
DirectDrawSurface7.GetPalette |
Retrieves the DirectDrawPalette class associated with this surface. |
IsLost |
DirectDrawSurface7.IsLost |
Determines whether the surface memory allocated to a DirectDrawSurface has been released. |
Restore |
DirectDrawSurface7.Restore |
Restores a surface that has been lost, which occurs when the surface memory associated with the DirectDrawSurface class has been released. |
SetClipper |
DirectDrawSurface7.SetClipper |
Attaches a clipper class to, or deletes one from, a surface |
SetColorKey |
DirectDrawSurface7.SetColorKey |
Sets the color key value for the DirectDrawSurface class if the hardware supports color keys on a per-surface basis. |
SetFont |
DirectDrawSurface7.SetFont |
Specifies the font to be used in DirectDrawSurface7.DrawText. |
SetPalette |
DirectDrawSurface7.SetPalette |
Attaches a palette class to (or detaches one from) a surface. The surface uses this palette for all subsequent operations. The palette change takes place immediately, without regard to refresh timing. |
The DirectDrawPalette Class
The DirectDrawPalette class is special in that it handles the color palette you use for your animations. With the palette, you can control what happens to the colors on the screen. You can cause classes to flash and change colors by changing the associated colors in the DirectDrawPalette class.
The following code syntax is an example of declaring and using the DirectDrawPalette class:
Dim oDDPalette As DxVBLib.DirectDrawPalette
This is what the code would look like when included with the DirectX and DirectDraw declarations:
'''First you need to access the DirectX 7 COM class Dim oDX As DxVBLib.DirectX7 = New DxVBLib.DirectX7() '''Next you need to create a DirectDraw class Dim oDD As DxVBLib.DirectDraw7 '''Then you need to create a DirectDrawPalette class Dim oDDPalette As DxVBLib.DirectDrawPalette
A DirectDrawPalette class is created by calling the DirectDraw.CreatePalette method, as shown in this example:
Set oDDPalette = oDD.CreatePalette
Table 3 outlines some of the more commonly used DirectDrawPalette methods.
Table 3 The Most Commonly Used DirectDrawPalette Methods
Method |
Syntax |
Purpose |
GetCaps |
DirectDrawPalette.GetCaps |
Retrieves the capabilities of the DirectDrawPalette class. In essence, it tells the program which video capabilities are available with the video card. |
GetEntries |
DirectDrawPalette.GetEntries |
Queries palette values from a DirectDrawPalette class. |
SetEntries |
DirectDrawPalette.SetEntries |
Changes entries in a DirectDrawPalette class immediately. |
The DirectDrawClipper Class
Clipping is important for controlling what areas of the screen are being animated, and the DirectDrawClipper class is the mechanism by which you accomplish that task. The DirectDrawClipper class uses a clipper list to determine the areas that "hide" the animated action from the viewer.
Let's look at the most common use for clippers: handling animation in windows. When you begin work with DirectDraw sprites, you'll notice that if the sprite nears the edge of the screen, it just suddenly disappear, which might not be quite the effect you're looking for. Most of us would expect the sprite to gradually move offscreen, and this behavior can be enforced through clippers. By assigning the complete window as the clipper area for your surface, you can control how your sprites disappear off the edges of the screen.
The following code syntax is an example of declaring and using the DirectDrawClipper class:
Dim oDDClipper As DxVBLib.DirectDrawClipper
In DirectX and DirectDraw declarations, the code would look like this:
'''First you need to access the DirectX 7 COM class Dim oDX As DxVBLib.DirectX7 = New DxVBLib.DirectX7() '''Next you need to create a DirectDraw class Dim oDD As DxVBLib.DirectDraw '''Then you need to create a DirectDrawClipper class Dim oDDClipper As DxVBLib.DirectDrawClipper
Table 4 shows the most commonly used DirectDrawClipper methods.
Table 4 The Most Commonly Used DirectDrawClipper Methods
Method |
Syntax |
Purpose |
GetClipList |
DirectDrawClipper.GetClipList |
Retrieves a copy of the clipper list associated with a DirectDrawClipper class. |
IsClipListChanged |
DirectDrawClipper.IsClipListChanged |
Monitors the status of the clipper list of a DirectDrawClipper class. |
SetClipList |
DirectDrawClipper.SetClipList |
Sets or deletes the clipper list used by the DirectDrawSurface7.Blt method on surfaces that the parent DirectDrawClipper class is attached to. |
GetHWnd |
DirectDrawClipper.GetHWnd |
Retrieves the window handle previously associated with this DirectDrawClipper class by the DirectDrawClipper.SetHWnd method. |
SetHWnd |
DirectDrawClipper.SetHWnd |
Sets the window handle to retrieve the clipping information. |