Tricks of the Windows Game Programming Gurus:Advanced DirectDraw and Bitmapped Graphics

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

As a demo of this little software clipper, I’ve written the crudest bitmap engine you’ve ever seen. Basically, I created an array of 64 bytes to hold a little happy face. Here’s the declaration:

    UCHAR happy_bitmap[64] = {0,0,0,0,0,0,0,0,

                              0,0,1,1,1,1,0,0,

                              0,1,0,1,1,0,1,0,

                              0,1,1,1,1,1,1,0,

                              0,1,0,1,1,0,1,0,

                              0,1,1,0,0,1,1,0,

                              0,0,1,1,1,1,0,0,

                              0,0,0,0,0,0,0,0};

Then I put the system into 320×240×8 back buffer mode and made color index RGB(255,255,0), which is yellow. Then I made the little happy face move around the screen by moving it on a constant random velocity and then wrapping the face around when it goes too far off any of the four screen edges. It goes out of the window just far enough for you to see the clipping function work. Then I got carried away and made 100 happy faces! The final program is `DEMO7_8.CPP|EXE`, and Figure 7.19 is a screen shot of the program in action.

Figure 7.19 `DEMO7_8.EXE` in action.

Here’s the `Game_Main()` function for your review:

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

    {

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

    // here

    DDBLTFX ddbltfx; // the blitter fx structure

    // make sure this isn’t executed again

    if (window_closed)

       return(0);

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

    if (KEYDOWN(VK_ESCAPE))

       {

       PostMessage(main_window_handle,WM_CLOSE,0,0);

       window_closed = 1;

       } // end if

    // use the blitter to erase the back buffer

    // first initialize the DDBLTFX structure

    DDRAW_INIT_STRUCT(ddbltfx);

    // now set the color word info to the color we desire

    ddbltfx.dwFillColor = 0;

    // make the blitter call

    if (FAILED(lpddsback->Blt(NULL, // ptr to dest RECT, NULL means all

                              NULL, // pointer to source surface

                              NULL, // pointer to source RECT

                              DDBLT_COLORFILL | DDBLT_WAIT,

                              // do a color fill and wait if you have to

                              &ddbltfx))) // pointer to DDBLTFX holding info

    return(0);

    // initialize ddsd

    DDRAW_INIT_STRUCT(ddsd);

    // lock the back buffer surface

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

                                  DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,

                                  NULL)))

        return(0);

    // draw all the happy faces

    for (int face=0; face < 100; face++)

        {

        Blit_Clipped(happy_faces[face].x,

                     happy_faces[face].y,

                     8,8,

                     happy_bitmap,

                     (UCHAR *)ddsd.lpSurface,

                     ddsd.lPitch);

        } // end face

    // move all happy faces

    for (face=0; face < 100; face++)

        {

        // move

        happy_faces[face].x+=happy_faces[face].xv;

        happy_faces[face].y+=happy_faces[face].yv;

        // check for off screen, if so wrap

        if (happy_faces[face].x > SCREEN_WIDTH)

             happy_faces[face].x = -8;

        else

        if (happy_faces[face].x < -8)

            happy_faces[face].x = SCREEN_WIDTH;

        if (happy_faces[face].y > SCREEN_HEIGHT)

             happy_faces[face].y = -8;

        else

        if (happy_faces[face].y < -8)

            happy_faces[face].y = SCREEN_HEIGHT;

        } // end face

    // unlock surface

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

       return(0);

    // flip the pages

    while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));

    // wait a sec

    Sleep(30);

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

    return(1);

    } // end Game_Main

> Note: Make sure to look at the code for `Blit_Clipped()` in the demo program, because I slightly modified it to work with a variable memory pitch. No big deal, but I thought you might want to know. Also, you may be wondering why I decided to use 320×240 mode. Well, the little 8×8 bitmap in 640×480 was so small, I was going blind <BG>.

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.