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

Of course, I’m leaving out all the Windows initialization and event loop stuff, but that never changes. However, to be complete, take a look at `DEMO6_3.CPP` and the associated executable `DEMO6_3.EXE` on the CD. They contain the preceding code injected into your Game Console’s `Game_Main()` function, shown in the following listing along with the updated `Game_Init()`. Figure 6.12 is a screen shot of the program in action.

Figure 6.12 `DEMO6_3.EXE` in action.

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

    {

    // this is the main loop of the game, do all your processing

    // here

    // for now test if user is hitting ESC and send WM_CLOSE

    if (KEYDOWN(VK_ESCAPE))

       SendMessage(main_window_handle,WM_CLOSE,0,0);

    // plot 1000 random pixels to the primary surface and return

    // clear ddsd and set size, never assume it's clean

    memset(&ddsd,0,sizeof(ddsd));

    ddsd.dwSize = sizeof(ddsd);

    if (FAILED(lpddsprimary->Lock(NULL, &ddsd,

                       DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,

                       NULL)))

       {

       // error

       return(0);

       } // end if

    // now ddsd.lPitch is valid and so is ddsd.lpSurface

    // make a couple aliases to make code cleaner, so we don't

    // have to cast

    int mempitch        = (int)ddsd.lPitch;

    UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;

    // plot 1000 random pixels with random colors on the

    // primary surface, they will be instantly visible

    for (int index=0; index < 1000; index++)

        {

        // select random position and color for 640x480x8

        UCHAR color = rand()%256;

        int x = rand()%640;

        int y = rand()%480;

        // plot the pixel

        video_buffer[x+y*mempitch] = color;

        } // end for index

    // now unlock the primary surface

    if (FAILED(lpddsprimary->Unlock(NULL)))

       return(0);

    // sleep a bit

    Sleep(30);

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

    return(1);

    } // end Game_Main

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

    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

    // clear ddsd and set size

    memset(&ddsd,0,sizeof(ddsd));

    ddsd.dwSize = sizeof(ddsd);

    // enable valid fields

    ddsd.dwFlags = DDSD_CAPS;

    // request primary surface

    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

    // create the primary surface

    if (FAILED(lpdd4->CreateSurface(&ddsd, &lpddsprimary, NULL)))

       {

       // error

       return(0);

       } // end if

    // build up the palette data array

    for (int color=1; color < 255; color++)

        {

        // fill with random RGB values

        palette[color].peRed   = rand()%256;

        palette[color].peGreen = rand()%256;

        palette[color].peBlue  = rand()%256;

        // set flags field to PC_NOCOLLAPSE

        palette[color].peFlags = PC_NOCOLLAPSE;

        } // end for color

    // now fill in entry 0 and 255 with black and white

    palette[0].peRed   = 0;

    palette[0].peGreen = 0;

    palette[0].peBlue  = 0;

    palette[0].peFlags = PC_NOCOLLAPSE;

    palette[255].peRed   = 255;

    palette[255].peGreen = 255;

    palette[255].peBlue  = 255;

    palette[255].peFlags = PC_NOCOLLAPSE;

    // create the palette object

    if (FAILED(lpdd4->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 |

                                    DDPCAPS_INITIALIZE,

                                    palette,&lpddpal, NULL)))

    {

    // error

    return(0);

    } // end if

    // finally attach the palette to the primary surface

    if (FAILED(lpddsprimary->SetPalette(lpddpal)))

        {

        // error

        return(0);

        } // end if

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

    return(1);

    } // end Game_Init

The only other detail I want to bring to your attention about the demo program code is the creation of the main window, shown here:

    // create the window

    if (!(hwnd = CreateWindowEx(NULL,                   // extended style

                               WINDOW_CLASS_NAME,       // class

                    "T3D DirectX Pixel Demo", // title

                    WS_POPUP | WS_VISIBLE,

                     0,0,    // initial x,y

                             640,480,  // initial width, height

                           NULL,    // handle to parent

                    NULL,    // handle to menu

                    hinstance, // instance of this application

                    NULL)))    // extra creation parms

    return(0);

Notice that instead of using the `WS_OVERLAPPEDWINDOW` window style, the demo uses `WS_POPUP`. If you’ll recall, this style is devoid of all controls and Windows GUI stuff, which is what you want for a full-screen DirectX application.

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.