Tricks of the Windows Game Programming Gurus:Vector Rasterization and 2D Transformations

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

#### Global Domination

You know that I like globals because they’re so fast. Moreover, they’re really appropriate for a lot of system-level variables (which a 2D/3D engine has a lot of). So here are the globals for the engine. Again, you’ve seen many of them, but the ones that look alien have comments, so read them:

    extern FILE *fp_error;                           // general error file

    // notice that interface 4.0 is used on a number of interfaces

    extern LPDIRECTDRAW4        lpdd;        // dd object

    extern LPDIRECTDRAWSURFACE4 lpddsprimary;// dd primary surface

    extern LPDIRECTDRAWSURFACE4 lpddsback;  // dd back surface

    extern LPDIRECTDRAWPALETTE  lpddpal;    // a pointer to the

                                            // created dd palette

    extern LPDIRECTDRAWCLIPPER  lpddclipper;       // dd clipper

    extern PALETTEENTRY         palette[256];      // color palette

    extern PALETTEENTRY         save_palette[256]; // used to save palettes

    extern DDSURFACEDESC2       ddsd; // a direct draw surface

                                      // description struct

    extern DDBLTFX              ddbltfx; // used to fill

    extern DDSCAPS2             ddscaps;// a direct draw surface

                                        // capabilities struct

    extern HRESULT              ddrval; // result back from dd calls

    extern UCHAR                *primary_buffer;  // primary video buffer

    extern UCHAR                *back_buffer;     // secondary back buffer

    extern int                  primary_lpitch;       // memory line pitch

    extern int                  back_lpitch;          // memory line pitch

    extern BITMAP_FILE          bitmap8bit;           // a 8 bit bitmap file

    extern BITMAP_FILE          bitmap16bit;          // a 16 bit bitmap file

    extern BITMAP_FILE          bitmap24bit;          // a 24 bit bitmap file

    extern DWORD                start_clock_count;// used for timing

    extern int                  windowed_mode;    // tracks if dd is

                                                      // windowed or not

    // these defined the general clipping rectangle for software clipping

    extern int min_clip_x,             // clipping rectangle

               max_clip_x,

               min_clip_y,

               max_clip_y;

    // these are overwritten globally by DD_Init()

    extern int screen_width,             // width of screen

               screen_height,           // height of screen

               screen_bpp;              // bits per pixel

    extern int window_client_x0;   // used to track the starting

                                   // (x,y) client area

    extern int window_client_y0;   // for windowed mode directdraw operations

    // storage for our lookup tables

    extern float cos_look[360];

    extern float sin_look[360];

#### The DirectDraw Interface

Now that you’ve seen all the data support, let’s take a look at all the DirectDraw support functions that you’ve written. Surprisingly, you’ve done a lot of work, but you probably haven’t really noticed. The DirectDraw system you’ve worked out is double buffered (has a back buffer), supports 256 colors, and has a palette, along with a shadow palette, clipping, and the capability to write to both the primary and secondary buffers, along with page flipping, of course. Let’s take a quick look at each function.

Function Prototype:

    int DDraw_Init(int width,  // width of display

                   int height, // height of display

                   int bpp);   // bits per pixel

Purpose:

`DDraw_Init()` starts up and initializes DirectDraw. You can send any resolution and color depth. Returns `TRUE` if successful.

Example:

    // put the system into 800x600 with 256 colors

    DDraw_Init(800,600,8);

Function Prototype:

    int DDraw_Shutdown(void);

Purpose:

`DDraw_Shutdown()` shuts down DirectDraw and releases all interfaces.

Example:

    // in your system shutdown code you might put

    DDraw_Shutdown();

Function Prototype:

    LPDIRECTDRAWCLIPPER

     DDraw_Attach_Clipper(

        LPDIRECTDRAWSURFACE4 lpdds, // surface to attach to

        int num_rects,     // number of rects

        LPRECT clip_list); // pointer to rects

Purpose:

`DDraw_Attach_Clipper()` attaches a clipper to the sent surface (the back buffer in most cases). In addition, you must send the number of rectangles in the clipping list and a pointer to the `RECT` list itself. Returns `TRUE` if successful.

Example:

    // creates a clipping region the size of the screen

    RECT clip_zone = {0,0,SCREEN_WIDTH-1, SCREEN_HEIGHT-1};

    DDraw_Attach_Clipper(lpddsback, 1, &clip_zone);

Function Prototype:

    LPDIRECTDRAWSURFACE4

       DDraw_Create_Surface(int width, // width of surface

                            int height, // height of surface

                            int mem_flags); // control flags

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.