Direct DrawCreate and Direct DrawCreateEx

There are two functions you can use to create a DirectDraw object if you don't want to use the pure COM method: DirectDrawCreateO and DirectDrawCreateEx(). The latest version of the DirectX API supports both methods, but Microsoft recommends using the DirectDrawCreateEx() function because it returns an object with full Direct3D support. The DirectDrawCreateO function provides support for 2D drawing only.

Both functions require a globally unique identifier (GUID) as input for the first argument. The GUID points to a device driver—a requirement if the host system provides support for more than one display device. Setting this argument to null tells DirectX to use the active driver. You can also specify one of two constant values to place the system in a test mode: hardware (DDCREATE_HARDWAREONLY) or software (DDCREATE_EMULATIONONLY). Because you can supply more than one type of input for this argument, the example provides two overrides of each function.

The second argument is an IDirectDraw7 interface pointer. You'll need to provide this object reference as an Object or as an IntPtr. The example code uses an Object for ease of conversion.

The DirectDrawCreateEx() function includes a third argument not included with the DirectDrawCreate() function. The iid argument contains the Interface Identifier (IID) of the DirectDraw 7 object. Consequently, you must always use the IID_IDirectDraw7 constant defined in the library for this argument. The example defines this entry as a GUID structure, which is correct for the managed environment. However, the Platform SDK documentation describes the entry as a REFIID type. It isn't until you spend some time wandering through the C/C++ header files that you discover the two are equivalent in this case. Of course, creating a constant GUID value presents a problem for C# developers. Visual C++ developers have the DEFINE_GUID() macro they can use to create a constant GUID value, but C# doesn't define this mechanism. You can begin solving the problem by creating a special variable as shown here:

// This is a constant value substitute for the IID_IDirectDraw7 // iid value.

public static GUID IID_IDirectDraw7 =

CreateIID.DEFINE_GUID(0x15e65ec0)0x3b9c)0x11d2,0xb9)

0x2f)0x00)0x60)0x97)0x97)0xea,0x5b);

As you can see, we make a static GUID variable equal to the output of a class function. There are a number of other ways to perform this task, but using a static class member function proves the best because you can hide the implementation details from the user. Listing 14.1 shows the class definition.

Listing 14.1: Defining an IID Constant Value class CreateIID {

public static GUID DEFINE_GUID(UInt32 DatalIn,

GUID OutData = new GUID()

UInt16 Data2In,

UInt16 Data3In,

Byte

Data4

0In,

Byte

Data4

1In,

Byte

Data4

2In,

Byte

Data4

3In,

Byte

Data4

4In,

Byte

Data4

5In,

Byte

Data4

6In,

Byte

Data4

7In)

//

Create

: the GUID

// Assign the IID_IDirectDraw7 values. OutData.Datal = DatalIn; OutData.Data2 = Data2In; OutData.Data3 = Data3In;

// Remember to create the array and then assign // values to it.

OutData.

.Data4

=

new Byte[

8];

OutData.

.Data4

[0]

= Data4

0In

OutData.

.Data4

[1]

= Data4

1In

OutData.

.Data4

[2]

= Data4

2In

OutData.

.Data4

[3]

= Data4

3In

OutData.

Data4

[4]

= Data4

4In

OutData.

Data4

[5]

= Data4

5In

OutData.

Data4

[6]

= Data4

6In

OutData.

Data4

[7]

= Data4

7In

// Output the result, return OutData;

// Output the result, return OutData;

Note that the class itself is hidden from view, but the DEFINE_GUID() method is visible internally. The DEFINE_GUID() method works precisely like the DEFINE_GUID() macro used by Visual C++, so you can transfer the data for IID calls to that macro directly to a C# application that incorporates this class. This is yet another way to create macro substitutes for the managed environment.

The DirectDrawCreate() and DirectDrawCreateEx() functions both include the same final argument and you must set it to null in all cases. The pUnkOuter argument is supposed to provide aggregation support for a future version of DirectX. The current version doesn't support this feature.

Was this article helpful?

0 0

Post a comment