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

### Cooperating with Windows

As you know, Windows is a cooperative, shared environment. At least that’s the idea, although as a programmer I still haven’t figured out how to make it cooperate with my code! Anyway, DirectX is similar to any Win32 system, and at the very least, it must inform Windows that it’s going to use various resources so that other Windows applications don’t try to request (and get) resources that DirectX has control over. Basically, DirectX can be a complete resource hog as long is it tells Windows what it’s doing—seems fair to me <BG>.

In the case of DirectDraw, about the only thing that you should be interested in is the video display hardware. There are two cases that you must concern yourselves with:

  • Full-screen mode
  • Windowed mode

In full-screen mode, DirectDraw acts much like an old DOS program. That is, the entire screen surface is allocated to your game, and you write directly to the video hardware. No other application can touch the hardware. Windowed mode is a little different. In windowed mode, DirectDraw must cooperate much more with Windows because other applications may need to update their own client window areas (which may be visible to the user). Hence, in windowed mode your control and monopolization of the video hardware is much more restrained. However, you still have full access to 2D and 3D acceleration, so that’s a good thing. But then, so were bell-bottoms at first...

Chapter 7, “Advanced DirectDraw and Bitmapped Graphics,” will talk more about windowed DirectX applications, but they are a little more complex to handle. Most of this chapter will deal with full-screen modes because they are easier to work with, so keep that in mind.

Now that you know a little bit about why there needs to be cooperation between Windows and DirectX, let’s see how to tell Windows how you want to cooperate. To set the cooperation level of DirectDraw, use the `IDirectDraw4:: SetCooperativeLevel()` function, which is a method of `IDirectDraw4`.

C++ For you C programmers, the syntax `IDirectDraw4::SetCooperative Level()` may be a little cryptic. The `::` operator is called the scope resolution operator, and the syntax simply means that `SetCooperativeLevel()` is a method (or member function) of the `IDirectDraw4` interface. This is basically a class that is nothing more than a structure with data and a virtual function table. In some cases, I may forgo using the interface to prefix the method and write it like `SetCooperativeLevel()`. However, be advised that all DirectX functions are part of an interface and thus must be called using a function pointer style call, like `lpdd->function(...)`.

Here’s the prototype of `IDirectDraw4::SetCooperativeLevel()`:

    HRESULT SetCooperativeLevel(HWND hWnd,     // window handle

                                DWORD dwFlags);// control flags

This returns `DD_OK` if successful, and an error code if not.

Interestingly enough, this is the first time that the window handle has entered into the DirectX equation. The `hWnd` parameter is needed so that DirectX (or more specifically, DirectDraw) has something to anchor to. Simply use your main window handle in all cases.

The second and last parameter to `SetCoopertiveLevel()` is `dwFlags`, which is the control flags parameter and directly influences the way that DirectDraw works with Windows. Table 6.2 lists the most commonly used values that can be logically `OR`’ed together to obtain the desired cooperation level.

Table 6.2 Control Flags for `SetCooperativeLevel()`

Value | Description
--- | ---

`DDSCL_ALLOWMODEX` | Allows the use of Mode X (320×200,240,400) display modes. Can be used only if the `DDSCL_EXCLUSIVE` and `DDSCL_FULLSCREEN` flags are present.
`DDSCL_ALLOWREBOOT` | Allows Ctrl+Alt+Del to be detected while in exclusive (full-screen) mode.
`DDSCL_EXCLUSIVE` | Requests the exclusive level. This flag must be used with the `DDSCL_FULLSCREEN` flag.
`DDSCL_FPUSETUP` | Indicates that the calling application is likely to keep the FPU set up for optimal Direct3D performance (single precision and exceptions disabled) so Direct3D does not need to explicitly set the FPU each time. For more information, look up “DirectDraw Cooperative Levels and FPU Precision” in the DirectX SDK.
`DDSCL_FULLSCREEN` | Indicates full-screen mode will be used. GDI from other applications will not be able to draw on the screen. This flag must be used with the `DDSCL_EXCLUSIVE` flag.
`DDSCL_MULTITHREADED` | Requests multithread-safe DirectDraw behavior. Don’t worry about this for now.
`DDSCL_NORMAL` | Indicates that the application will function as a regular Windows application. This flag cannot be used with the `DDSCL_ALLOWMODEX`, `DDSCL_EXCLUSIVE`, or `DDSCL_FULLSCREEN` flags.
`DDSCL_NOWINDOWCHANGES` | Indicates that DirectDraw is not allowed to minimize or restore the application window on activation.

If you take a good look at the various flags, it may seem that some of them are redundant—very true. Basically, `DDSCL_FULLSCREEN` and `DDSCL_EXCLUSIVE` must be used together, and if you decide to use any Mode X modes, you must use `DDSCL_FULLSCREEN`, `DDSCL_EXCLUSIVE,` and `DDSCL_ALLOWMODEX` all together. Other than that, the flags pretty much do what they would seem to from their definitions. In most cases, you’ll set full-screen applications like this:

    lpdd4->SetCooperativeLevel(hwnd,

                               DDSCL_FULLSCREEN |

                               DDSCL_ALLOWMODEX |

                               DDSCL_EXCLUSIVE |

                               DDSCL_ALLOWREBOOT);

and normal windowed applications like this:

    lpdd4->SetCooperativeLevel(hwnd, DDSCL_NORMAL);

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.