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

Purpose:

`DDraw_Create_Surface()` is used to create a generic offscreen DirectDraw surface in system memory, VRAM, or AGP memory. The default is `DDSCAPS_OFFSCREENPLAIN`. Any additional control flags are logically OR’ed with the default. They’re the standard DirectDraw `DDSCAP*` flags, such as `DDSCAPS_SYSTEMMEMORY` and `DDSCAPS_VIDEO-MEMORY` for system memory and VRAM, respectively. If the function is successful, it returns a pointer to the new surface. Otherwise, it returns `NULL`.

Example:

    // let's create a 64x64 surface in VRAM

    LPDIRECTDRAWSURFACE4 image =

DDraw_Create_Surface(64,64, DDSCAPS_VIDEOMEMORY);

Function Prototype:

    int DDraw_Flip(void);

Purpose:

`DDraw_Flip()` simply flips the primary surface with the secondary surface. The call waits until the flip can take place, so it may not return immediately. Returns `TRUE` if successful.

Example:

    // flip em baby

    DDraw_Flip();

Function Prototype:

    int DDraw_Wait_For_Vsync(void);

Purpose:

`DDraw_Wait_For_Vsync()` waits until the next vertical blank period begins (when the raster hits the bottom of the screen). Returns `TRUE` if successful and `FALSE` if something really bad happened.

Example:

    // wait 1/70th of sec

    DDraw_Wait_For_Vsync();

Function Prototype:

    int DDraw_Fill_Surface(LPDIRECTDRAWSURFACE4 lpdds,int color);

Purpose:

`DDraw_Fill_Surface()` is used to fill a surface with a color. The color must be in the color depth format of the surface, such as a single byte in 256-color mode or a RGB descriptor in high-color modes. Returns `TRUE` if successful.

Example:

    // fill the primary surface with color 0

    DDraw_Fill_Surface(lpddsprimary,0);

Function Prototype:

    UCHAR *DDraw_Lock_Surface(LPDIRECTDRAWSURFACE4 lpdds,int *lpitch);

Purpose:

`DDraw_Lock_Surface()` locks the sent surface (if possible) and returns a `UCHAR` pointer to the surface, along with updating the sent `lpitch` variable with the linear memory pitch of the surface. While the surface is locked, you can manipulate it and write pixels to it, but the blitter will be blocked, so remember to unlock the surface ASAP. In addition, after unlocking the surface, the memory pointer and pitch most likely become invalid and should not be used. `DDraw_Lock_Surface()` returns the non-`NULL` address of the surface memory if successful and `NULL` otherwise.

Example:

    // holds the memory pitch

    int lpitch = 0;

    // let's lock the little 64x64 image we made

    UCHAR *memory = DDraw_Lock_Surface(image, &lpitch);

Function Prototype:

    int DDraw_Unlock_Surface(LPDIRECTDRAWSURFACE4 lpdds);

Purpose:

`DDraw_Unlock_Surface()` unlocks a surface previously locked with `DDraw_Lock_Surface()`. You need only send the pointer to the surface. Returns `TRUE` if successful.

Example:

    // unlock the image surface

    DDraw_Unlock_Surface(image);

> Note: Do not change any of these globals yourself; they’re used to track state changes in the locking functions. Changing them yourself may make the engine go crazy.

Function Prototypes:

    UCHAR *DDraw_Lock_Back_Surface(void);

    UCHAR *DDraw_Lock_Primary_Surface(void);

Purpose:

These two functions are used to lock the primary and secondary rendering surfaces. However, in most cases you’ll only be interested in locking the secondary surface in the double buffered system, but the ability to lock the primary surface is there if you need it. If you call `DDraw_Lock_Primary_Surface()`, the following globals will become valid:

    extern UCHAR  *primary_buffer;      // primary video buffer

    extern int     primary_lpitch;       // memory line pitch

Then you’re free to manipulate the surface memory as you want; however, the blitter will be blocked. Anyway, making the call to `DDraw_Lock_Back_Surface()` will lock the back buffer surface and validate the following globals:

    extern UCHAR  *back_buffer;         // secondary back buffer

    extern int     back_lpitch;          // memory line pitch

Example:

    // let lock the primary surface and write a pixel to the

    // upper left hand corner

    DDraw_Lock_Primary();

    primary_buffer[0] = 100;

Function Prototype:

    int DDraw_Unlock_Primary_Surface(void);

    int DDraw_Unlock_Back_Surface(void);

Purpose:

These functions are used to unlock the primary or back buffer surfaces. If you try to unlock a surface that wasn’t locked, there’s no effect. Returns `TRUE` if successful.

Example:

    // unlock the secondary back buffer

    DDraw_Unlock_Back();

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.