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

Referring back to the function, the first three parameters are straightforward, but the last two need a bit of explanation. `dwRefreshRate` is used to override the video driver’s default refresh for the mode you request. Hence, if you request a 320×200 mode, chances are the refresh will be at 70Hz. But with this parameter, you could force it to 60Hz if you wanted to. I would leave the refresh rate alone, to tell you the truth, and simply set the bit to 0 (which indicates to the driver to use the default).

The last parameter, `dwFlags`, is an extra flags `WORD` that is a catchall and is of very little use. Currently, it’s used as an override so you can use VGA mode 13h for 320×200 instead of Mode X 320×200 via the flag `DDSDM_STANDARDVGAMODE`. Again, I wouldn’t worry about it. If you do write a game that uses 320×200, you can try experimenting with this flag and using VGA mode 13h or Mode X for 320×200 to see which is faster, but the performance difference will be almost negligible. For now, just set `dwFlags` to 0.

That’s enough of the preliminaries. Let’s get to switching modes! To switch modes, you must create the DirectDraw object, set the cooperation level, and finally set the display mode, like this:

    lpdd4->SetDisplayMode(width,height,bpp,0,0);

For example, to create a 640×480 mode in 256 (8-bit) color, you would do this:

    lpdd4->SetDisplayMode(640,480,8,0,0);

And to set a 800×600 with 16-bit color, you would do this:

    lpdd4->SetDisplayMode(800,600,16,0,0);

Now, there’s a big difference between these two modes that extends further than the mere difference in resolution: the color depth. An 8-bit mode works completely differently than a 16-, 24-, or 32-bit mode. If you’ll recall, the previous chapters on Win32/GDI programming covered the topic of palettes extensively (Chapter 3, “Advanced Windows Programming,” and Chapter 4, “Windows GDI, Controls, and Last-Minute Gift Ideas”), and the same theory is in force with DirectDraw. That is, when you create an 8-bit mode, you are requesting a palettized mode, and you must also create a palette and fill it with 8.8.8 RGB entries.

On the other hand, if you create a straight RGB mode like 16, 24, or 32 bpp (bits per pixel), you don’t have to take this step. You can write encoded data directly to the video buffer (when you learn how). At very least, you must learn how to work with DirectDraw palettes (which will be the next topic of discussion). However, before moving on, let’s take a look at a complete example of creating a full-screen DirectX application with a resolution of 640×480×8.

`DEMO6_2.CPP` on the CD and the associated executable do just that. I would show you a figure, but all you would see is a black rectangle because the demo is a full-screen application. However, I can surely show you the code that makes it happen. As usual, I have based the demo on your game console, with the appropriate modifications, and made the DirectX-related changes to the `Game_Init()` and `Game_Shutdown()` sections, which are listed here from `DEMO6_2.CPP`. Take a close look at them and be amazed by the simplicity...

    int Game_Init(void *parms = NULL, int num_parms = 0)

    {

    // this is called once after the initial window is created and

    // before the main event loop is entered, do all your initialization

    // here

    // first create base IDirectDraw interface

    if (FAILED(DirectDrawCreate(NULL, &lpdd, NULL)))

       {

       // error

       return(0);

       } // end if

    // now query for IDirectDraw4

    if (FAILED(lpdd->QueryInterface(IID_IDirectDraw4,

                                   (LPVOID *)&lpdd4)))

       {

       // error

       return(0);

       } // end if

    // set cooperation to full screen

    if (FAILED(lpdd4->SetCooperativeLevel(main_window_handle,

                       DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |

                       DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))

       {

       // error

       return(0);

       } // end if

    // set display mode to 640x480x8

    if (FAILED(lpdd4->SetDisplayMode(SCREEN_WIDTH,

                      SCREEN_HEIGHT, SCREEN_BPP,0,0)))

       {

       // error

       return(0);

       } // end if

    // return success or failure or your own return code here

    return(1);

    } // end Game_Init

    /////////////////////////////////////////////////////////////

    int Game_Shutdown(void *parms = NULL, int num_parms = 0)

    {

    // this is called after the game is exited and the main event

    // loop while is exited, do all your cleanup and shutdown here

    // simply blow away the IDirectDraw4 interface

    if (lpdd4)

       {

       lpdd4->Release();

       lpdd4 = NULL;

       } // end if

    // return success or failure or your own return code here

    return(1);

    } // end Game_Shutdown

At this point, there are two things that you’re still missing: controlling the palette (in 256-color modes) and accessing the display buffers. Let’s take care of the color problem first.

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.