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

#### Math and Error Functions

The math library thus far is almost nonexistent, but that will soon change once you get to the math section of the book. I’ll pump your brain full of all kinds of fun mathematical information and functions. Until then, sip the sweet simplicity because it will be your last…

Function Prototype:

    int Fast_Distance_2D(int x, int y);

Purpose:

`Fast_Distance()` computes the distance from (0,0) to (x,y) using a fast approximation. Returns the distance within a 3.5 percent error truncated to an integer.

Example:

    int x1=100,y1=200; // object one

    int x2=400,y2=150; // object two

    // compute the distance between object one and two

    int dist = Fast_Distance_2D(x1-x2, y1-y2);

Function Prototype:

    float Fast_Distance_3D(float x, float y, float z);

Purpose:

`Fast_Distance_3D()` computes the distance from (0,0,0) to (x,y,z) using a fast approximation . The function returns the distance within an 11 percent error.

Example:

    // compute the distance from (0,0,0) to (100,200,300)

    float dist = Fast_Distance_3D(100,200,300);

Function Prototype:

    int Find_Bounding_Box_Poly2D(

                    POLYGON2D_PTR poly, // the polygon

                    float &min_x, float &max_x, // bounding box

                    float &min_y, float &max_y);

Purpose:

`Find_Bounding_Box_Poly2D()` computes the smallest rectangle that contains the sent polygon in `poly`. Returns `TRUE` if successful. Also, notice that the function takes parameters by reference.

Example:

    POLYGON2D poly; // assume this is initialized

    int min_x, max_x, min_y, max_y; // hold result

    // find bounding box

    Find_Bounding_Box_Poly2D(&poly,min_x,max_x,min_y,max_y);

Function Prototype:

    int Open_Error_File(char *filename);

Purpose:

`Open_Error_File()` opens a disk file that receives error messages sent by you via the `Write_Error()` function. Returns `TRUE` if successful.

Example:

    // open a general error log

    Open_Error_File(“errors.log”);

Function Prototype:

    int Close_Error_File(void);

Purpose:

`Close_Error_File()` closes a previously opened error file. Basically, it shuts down the stream. If you call this and an error file is not open, nothing will happen. Returns `TRUE` if successful.

Example:

    // close the error system, note no parameter needed

    Close_Error_File();

Function Prototype:

    int Write_Error(char *string, ...); // error formatting string

Purpose:

`Write_Error()` writes an error out to the previously opened error file. If there is no file open, the function returns a `FALSE` and there’s no harm. Note that the function uses the variable parameter indicator, so you can use this function as you would `printf()`. Returns `TRUE` if successful.

Example:

    // write out some stuff

    Write_Error(“\nSystem Starting…”);

    Write_Error(“x-vel = %d”, y-vel = %d”, xvel, yvel);

#### Bitmap Functions

The following function set makes up the `BITMAP_IMAGE` and `BITMAP_FILE` manipulation routines. There are functions to load 8-, 16-, 24-, and 32-bit bitmaps, as well as to extract images from them and create simple `BITMAP_IMAGE` objects (which are not DirectDraw surfaces). In addition, there’s functionality to draw these images, but there’s no clipping support. Hence, you can modify the source yourself if you need clipping or want to step up to the BOB objects, described at the end of the section.

Function Prototype:

    int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, // bitmap file

                         char *filename); // disk .BMP file to load

Purpose:

`Load_Bitmap_File()` loads a `.BMP` bitmap file from disk into the sent `BITMAP_FILE` structure where you can manipulate it. The function loads 8-, 16-, and 24-bit bitmaps, as well as the palette information on 8-bit `.BMP` files. Returns `TRUE` if successful.

Example:

    // let's load "andre.bmp" off disk

    BITMAP_FILE bitmap_file;

    Load_Bitmap_File(&bitmap_file, "andre.bmp");

Function Prototype:

    int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap);

                          // bitmap to close and unload

> Note: There’s a big difference between a `BITMAP_FILE` and a `BITMAP_IMAGE`. A `BITMAP_FILE` is a disk `.BMP` file, whereas a `BITMAP_IMAGE` is a system memory object like a sprite that can be moved and drawn.

Purpose:

`Unload_Bitmap_File()` deallocates the memory associated with the image buffer of a loaded `BITMAP_FILE`. Call this function when you’ve copied the image bits and/or are done working with a particular bitmap. You can reuse the structure, but the memory must be freed first. Returns `TRUE` if successful.

Example:

    // close the file we just opened

    Unload_Bitmap_File(&bitmap_file);

Function Prototype:

    int Create_Bitmap(BITMAP_IMAGE_PTR image, // bitmap image

                      int x, int y, // starting position

                      int width, int height); // size

Purpose:

`Create_Bitmap()` creates an 8-bit system memory bitmap at the given position with the given size. The bitmap is initially blank and is stored in the `BITMAP_IMAGE` image. The bitmap is not a DirectDraw surface, so there’s no acceleration or clipping available. Returns `TRUE` if successful.

Example:

    // let's create a 64x64 bitmap image at (0,0)

    BITMAP_IMAGE ship;

    Create_Bitmap(&ship, 0,0, 64,64);

Function Prototype:

    int Destroy_Bitmap(BITMAP_IMAGE_PTR image); // bitmap image to destroy

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.