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

#### Utility Functions

The next set of functions are just utility functions that I seem to use a lot, so I thought you might want to use them too.

Function Prototype:

    DWORD Get_Clock(void);

Purpose:

`Get_Clock()` returns the current clock time in milliseconds since Windows was started.

Example:

    // get the current tick count

    DWORD start_time = Get_Clock();

Function Prototype:

    DWORD Start_Clock(void);

Purpose:

`Start_Clock()` basically makes a call to `Get_Clock()` and stores the time in a global variable for you. Then you can call `Wait_Clock()`, which will wait for a certain number of milliseconds since your call to `Start_Clock()`. Returns the starting clock value at the time of the call.

Example:

    // start the clock and set the global

    Start_Clock();

Function Prototype:

    DWORD Wait_Clock(DWORD count); // number of milliseconds to wait

Purpose:

`Wait_Clock()` simply waits the sent number of milliseconds since the call was made to `Start_Clock()`. Returns the current clock count at the time of the call. However, the function will not return until the time difference has elapsed.

Example:

    // wait 30 milliseconds

    Start_Clock();

    // code…

    Wait_Clock(30);

Function Prototype:

    int Collision_Test(int x1, int y1, // upper lhs of obj1

                       int w1, int h1, // width, height of obj1

                       int x2, int y2, // upper lhs of obj2

                       int w2, int h2);// width, height of obj2

Purpose:

`Collision_Test()` basically performs an overlapping rectangle test on the two sent rectangles. The rectangles can represent whatever you like. You must send the upper-left-corner coordinates of each rectangle, along with its width and height. Returns `TRUE` if there is an overlap and `FALSE` if not.

Example:

    // do these two BITMAP_IMAGE's overlap?

    if (Collision_Test(ship1->x,ship1->y,ship1->width,ship1->height,

                       ship2->x,ship2->y,ship2->width,ship2->height))

       { // hit

       } // end if

Function Prototype:

    int Color_Scan(int x1, int y1, // upper left of rect

                   int x2, int y2, // lower right of rect

                   UCHAR scan_start, // starting scan color

                   UCHAR scan_end,  // ending scan color

                   UCHAR *scan_buffer,  // memory to scan

                   int scan_lpitch); // linear memory pitch

Purpose:

`Color_Scan()` is another collision-detection algorithm that scans a rectangle for a single 8-bit value or sequence of values in some continuous range. You can use it to determine if a color index is present within some area. Of course, it only works with 8-bit images, but the source is easily extensible to 16-bit or higher modes. Returns `TRUE` if the color(s) was found.

Example:

    // scan for colors in range from 122-124 inclusive

    Color_Scan(10,10, 50, 50, 122,124, back_buffer, back_lpitch);

### The BOB (Blitter Object) Engine

Although with a bit of programming you can get the `BITMAP_IMAGE` type to do what you want, it’s lacking in a serious way—it doesn’t use DirectDraw surfaces, so there’s no support for acceleration. Therefore, I’ve created a new type called a Blitter Object (BOB) that’s very similar to a sprite. For those of you who’ve been in a game programming cave, a sprite is nothing more than an object you can move around the screen that usually doesn’t disturb the background. In this case that isn’t true, so I called my animation object a BOB rather than a sprite—so there!

You haven’t seen any of the BOB engine source code so far in this book, but you have seen everything that went into making it. I don’t have enough space to list the source code, but it’s in the file `T3DLIB1.CPP` along with everything else. What I’m going to do is show you each function that makes up the engine, and you’re free to use the code, print it out and burn it, or whatever. I just wanted to give you a good example of using DirectDraw surfaces and full acceleration before you moved on to the remaining nongraphical components of DirectX.

Let’s talk briefly about what a BOB is. First, take a look at the data structure for a BOB, which is back in the section “Data Types and Structures,” and then come back here… Ready?

A BOB is basically a graphical object represented by one or more DirectDraw surfaces (up to 64). You can move a BOB, draw a BOB, and animate a BOB. BOBs are clipped by the current DirectDraw clipper, so they’re clipped as well as accelerated—which is a good thing! Figure 8.55 shows a BOB and its relationship to its animation frames.

Figure 8.55 The BOB (blitter object) animation system.

Also, the BOB engine supports animation sequences, so you can load in a set of frames and an animation sequence and the sequence will play by feeding from the frames. This is a very cool feature. Also, all the BOB functions return `TRUE` if successful and `FALSE` otherwise. So let’s take a look at them all…

Function Prototype:

    int Create_BOB(BOB_PTR bob, // ptr to bob to create

       int x, int y,          // initial position of bob

       int width, int height, // size of bob

       int num_frames, // total number of frames for bob

       int attr,       // attributes of bob

       int mem_flags); // surface memory flags, 0 is VRAM

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.