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

#### Cleaning Up

Before moving on to the end of the chapter, I want to bring up a topic that I’ve been putting off for a while—resource management. Yuck! Anyway, this seemingly un-fun concept simply means making sure that you `Release()` DirectDraw or DirectX objects in general when you’re done with them. For example, if you take a look at the source code in `DEMO6_3.CPP`, in the `Game_Shutdown()` function you’ll see a number of `Release()` calls to release all the DirectDraw objects back to the operating system, and DirectDraw itself, shown here:

    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 you cleanup and shutdown here

    // first the palette

    if (lpddpal)

       {

       lpddpal->Release();

       lpddpal = NULL;

       } // end if

    // now the primary surface

    if (lpddsprimary)

       {

       lpddsprimary->Release();

       lpddsprimary = NULL;

       } // end if

    // now 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

In general, you should `Release()` objects only when you’re done with them, and you should do so in reverse order of creation. For example, you created the DirectDraw object, the primary surface, and the palette, in that order, so a good rule of thumb would be to release the palette, surface, and then DirectDraw, like this:

    // first kill the palette

    if (lpddpal)

       {

       lpddpal->Release();

       lpddpal = NULL;

       } // end if

    // now the primary surface

    if (lpddsprimary)

        lpddsprimary->Release();

    // and finally the directdraw object itself

    if (lpdd4)

       {

       lpdd4->Release();

       lpdd4 = NULL;

       } // end if

> Warning: Before you make a call to `Release()`, notice the testing to see if the interface is non-`NULL`. This is absolutely necessary because the interface pointer may be `NULL`, and releasing on a `NULL` pointer may cause problems if the implementers of the interface haven’t thought of it.

### Summary

In this chapter you learned the basics of DirectDraw—how to get it up and running in full-screen mode, for the most part. Also, we touched upon palettes, display surfaces, and the differences between full-screen and windowed applications. In the next chapter, I’m going to put on the gas and we’re going to cover a lot of ground, so strap on your seat belt, baby!

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.