Tricks of the Windows Game Programming Gurus:First Contact: DirectDraw

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

## Chapter 6
First Contact: DirectDraw

> “Are you sure you don’t want to come upstairs?”

> —John Milton, The Devil’s Advocate

In this chapter you’re going to take your first look at one of the most important components of DirectX: DirectDraw. This is perhaps the most enabling technology in DirectX because it’s the conduit through which 2D graphics are performed and the frame buffer layer that Direct3D is built upon. Furthermore, if you understand DirectDraw alone, you have more than enough power to create any kind of graphical application that you might have written under DOS16/32. DirectDraw is the key to understanding a number of concepts indigenous to DirectX, so listen up!

Here’s your hit list for this chapter:

  • The interfaces of DirectDraw
  • Creating a DirectDraw object
  • Cooperating with Windows
  • Getting into the mode of things
  • The subtleties of color
  • Building a display surface

### The Interfaces of DirectDraw

DirectDraw is composed of a number of interfaces. If you recall from the discussion on the Component Object Model (COM) in Chapter 5, “DirectX Fundamentals and the Dreaded COM,” interfaces are nothing more than collections of functions and/or methods that you use to communicate with components. Take a look at Figure 6.1 for a graphical illustration of the DirectDraw interfaces.

Figure 6.1 The interfaces of DirectDraw.

#### Interface Characteristics

As you can see, there are only five interfaces that make up DirectDraw:

  `IUnknown`—All COM objects must be derived from this base interface, and DirectDraw is no exception. `IUnknown` doesn’t contain much more than the `Addref()`, `Release()`, and `QueryInterface()` functions that are overridden by each of the other interfaces.
  `IDirectDraw`—This is the main interface object that must be created to start working with DirectDraw. `IDirectDraw` literally represents the video card and support hardware. Interestingly enough, with MMS (Multiple Monitor Support) and Windows 98/NT, now you can have more than one video card installed in your system and hence more than one DirectDraw object. However, in this book we’ll assume that there is only one video card in the computer and always select the default card to represent the DirectDraw object, even if there is more than one card in the system.
  `IDirectDrawSurface`—This represents the actual display surface(s) that you will create, manipulate, and display using DirectDraw. A DirectDraw surface can exist on the video card itself using VRAM (Video RAM) or within system memory. There are basically two types of surfaces: primary surfaces and secondary surfaces.

  Primary surfaces usually represent the actual video buffer that is currently being rasterized and displayed by the video card. Secondary surfaces, on the other hand, are usually offscreen. In most cases, you will create a single primary surface to represent the actual video display, and then one or more secondary surfaces to represent object bitmaps and/or back buffers to represent offscreen drawing areas where you’ll build up the next frame of animation. We’ll get to the details of surfaces later in the chapter, but for now, take a look at Figure 6.2 for a little graphical elaboration.

  Figure 6.2 DirectDraw surfaces.

  `IDirectDrawPalette`—DirectDraw is equipped to deal with any color space, from 1-bit monochrome to 32-bit Ultra-True Color. Thus, DirectDraw supports the `IDirectDrawPalette` interface to deal with color palettes in video modes that use 256 or fewer colors. In this case, you will use the 256-color mode extensively in a number of demos because it’s the fastest mode for a software rasterizer. In the discussion of Direct3D Immediate Mode in Volume II, you’ll switch over to 24-bit color because that’s the native mode that D3D likes to work in. In any case, the `IDirectDrawPalette` interface is used to create, load, and manipulate palettes, and to attach palettes to drawing surfaces, such as the primary or secondary surfaces that you might create for your DirectDraw applications. Take a look at Figure 6.3 to see the relationship between a drawing surface and a DirectDraw palette.

  Figure 6.3 The relationship between DirectDraw surfaces and palettes.

  `IDirectDrawClipper`—This is used to help with clipping DirectDraw raster and bitmap operations to some subset of the visible display surface. In most cases, you’ll only use DirectDraw clippers for windowed DirectX applications and/or to clip bitmap operations to the extents of your display surface, whether it be a primary or secondary surface. The cool thing about the `IDirectDrawClipper` interface is that it takes advantage of hardware acceleration if it’s available, and the costly pixel-by-pixel or sub-image processing that is normally needed to clip bitmaps to the screen extents is done for you.

Now, before you move on to creating a DirectDraw object, I want to refresh your memory with some tasty tidbits of information that we touched upon in the previous chapter when dealing with COM. DirectDraw and all DirectX components are in constant flux, and thus the interfaces are always being upgraded. Alas, even though so far in this chapter I have referred to the interfaces of DirectDraw generically as `IDirectDraw`, `IDirectDrawSurface`, `IDirectDrawPalette`, and `IDirectDrawClipper`, for the most part these interfaces have all been updated and newer versions exist. For example, `IDirectDraw` is up to `IDirectDraw4` as of DirectX version 6.0.

All this means is that if you want the very latest software and hardware performance, you should always `IUnknown::QueryInterface()` for the latest interface revision. However, to find this out you’ll have to take a look at the DirectX SDK docs. Of course, in this book you’re using DirectX 6.0, so you already know what’s up, but keep in mind that when you upgrade to 7.0 you might have some newer interfaces that you want to use. However, both volumes of this book are about writing your own rasterization and 3D software, so I want to cheat as little as possible. In most cases, you’re going to be using very few of the bells and whistles of all the new revisions. Cool, home slice?

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.