Tricks of the Windows Game Programming Gurus:DirectX Fundamentals and the Dreaded COM

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

#### Querying for Interfaces

The weird thing about DirectX is that all the version numbers are out of sync. This is a bit of a problem, and definitely a cause for confusion. Here’s the deal: when the first version of DirectX came out, the DirectDraw interface was named like this:

    IDIRECTDRAW

Then, when DirectX 2.0 came out, DirectDraw was upgraded to version 2.0, so we had this:

    IDIRECTDRAW

    IDIRECTDRAW2

Now, at version 6.0, we have something like this:

    IDIRECTDRAW

    IDIRECTDRAW2

    IDIRECTDRAW4

Wait a minute—what happened to interfaces 3 and 5? I have no idea, but this is the problem. Hence, the idea is that even though you’re using DirectX 7.0, it doesn’t mean that the interfaces are up to that version. Moreover, they can all be out of sync. DirectX 6.0 may have DirectDraw interfaces up to `IDIRECTDRAW4`, but DirectSound is only up to interface version 1.0, which is simply called `IDIRECTSOUND`. You can see the mess we’re in! The moral of the story is that whenever you use a DirectX interface, you should make sure that you’re using the latest version. If you’re not sure, use the revision 1 interface pointer from the generic create function to get the latest version.

Here’s an example of what I’m talking about: `DirectDrawCreate()` returns a revision 1.0 interface pointer, but DirectDraw is really up to `IDIRECTDRAW4`. So how do you take advantage of this new functionality?

> Tip: If you’re having a panic attack over this stuff, don’t feel alone. I was using the version 1.0 interfaces up until version 5.0 of DirectX, since the DirectX docs are nebulous in this area—typical!

There are two ways to do this: with low-level COM functions or with `QueryInterface()`. Let’s use the latter. The process goes like this: First, you create the DirectDraw COM interface with a call to `DirectDrawCreate()`. This returns a boring `IDIRECTDRAW` interface pointer. Then, you make a call to `QueryInterface()` using this pointer and you retrieve it using the Interface ID (or GUID) for `IDIRECTDRAW4`. Here’s an example:

    LPDIRECTDRAW lpdd;    // version 1.0

    LPDIRECTDRAW4 lpdd4;  // version 6.0, but called 4.0

    // create version 1.0 DirectDraw object interface

    DirectDrawCreate(NULL, &lpdd, NULL);

    // now look in DDRAW.H header, find IDIRECTDRAW4 interface

    // ID and use it to query for the interface

    lpdd->QueryInterface(IID_IDirectDraw4, &lpdd4);

At this point, you have two interface pointers. But you don’t need the pointer to `IDIRECTDRAW`, so you should release it:

    // release, decrement reference count

    lpdd->Release();

    // set to NULL to be safe

    lpdd = NULL;

Remember this? You should release an interface when you’re done with it. Hence, when your program terminates, you would also release the `IDIRECTDRAW4` interface like this:

    // release, decrement reference count

    lpdd4->Release();

    // set to NULL to be safe

    lpdd4 = NULL;

That’s all there is to using DirectX and COM. Of course, you haven’t seen all the hundreds of functions that DirectX components have or all the interfaces—but you will <BG>.

### The Future of COM

Currently, there are a number of distributed object technologies similar to COM, such as COBRA (Common Object Broker Architecture). However, since you’re worried about Windows games, these other technologies aren’t as important.

The latest version of COM is called COM++, and it’s a much more robust implementation, with better rules and a more thought-out set of implementation details. COM++ will make distributed component software even easier to create. Granted, COM++ is a bit more complex than COM, but hey, that’s life.

In addition to COM and COM++, there’s also the full Internet/intranet version of COM called DCOM—Distributed COM. With DCOM technology, the COM objects don’t even need to be on your machine. They can be served from other machines on the network. Is that cool or what? Imagine having massive DCOM servers that your programs basically act as clients to. Incredible technology, if I do say so myself.

### Summary

This chapter has covered some pretty technical material and concepts. COM is not simple to understand, and it does take a bit of studying to really get a good hold on it. However, using COM is ten times easier than understanding it, as you’ll see in the next chapter. Anyway, you also took a look at DirectX and all of its components. So once you’ve seen the details of each component and how to use it in the following chapters, you’ll have a good idea of how they fit together.

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.