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:

`Load_Animation()` takes a little explaining. This function is used to load one of 16 arrays internal to the BOB that contain animation sequences. Each sequence contains an array of indices or frame numbers to display in sequence.

Example:

You might have a BOB that has eight frames, 0,1,...7, but you might have four animations defined as follows:

    int anim_walk[]  = {0,1,2,1,0};

    int anim_fire[]  = {5,6,0};

    int anim_die[]   = {3,4};

    int anim_sleep[] = {0,0,7,0,0};

Then, to load the animations into the BOB, you would do the following:

    // create a mutli animation bob

    // create the bob

    if (!Create_BOB(&alien, 0,0, 32,32,8,BOB_ATTR_MULTI_ANIM,0))

       { /* error */ }

    // load the bob frames in...

    // load walk into animation 0

    Load_Animation_BOB(&alien, 0,5,anim_walk);

    // load fire into animation 1

    Load_Animation_BOB(&alien, 1,3,anim_fire);

    // load die into animation 2

    Load_Animation_BOB(&alien, 2,2,anim_die);

    // load sleep into animation 3

    Load_Animation_BOB(&alien, 3,5,anim_sleep);

After loading the animations, you can set the active animation and play them with functions you’ll see in a minute.

Function Prototype:

    int Set_Pos_BOB(BOB_PTR bob, // ptr to bob to set position

                 int x, int y);  // new position of bob

Purpose:

`Set_Pos_BOB()` is a simple way to set the position of the BOB. It does nothing more than assign the internal (x,y) variables, but it’s nice to have a function.

Example:

    // set the position of the alien BOB above

    Set_Pos_BOB(&alien, player_x, player_y);

Function Prototype:

    int Set_Vel_BOB(BOB_PTR bob, // ptr to bob to set velocity

             int xv, int yv); // new x,y velocity

Purpose:

Each BOB has an internal velocity contained in (xv,yv). `Set_Vel_BOB()` simply assigns these values the new values sent in the function. The velocity values in the BOB won’t do anything unless you use the function `Move_BOB()` to move your BOBs. However, even if you don’t, you can use (xv,yv) to track the velocity of the BOB yourself.

Example:

    // make the BOB move in a straight horizontal line

    Set_Vel_BOB(&alien, 10,0);

Function Prototype:

    int Set_Anim_Speed_BOB(BOB_PTR bob, // ptr to bob

                     int speed); // speed of animation

> Note: This also resets the BOB animation to the first frame in the sequence.

Purpose:

`Set_Anim_Speed()` sets the internal animation rate for a BOB `anim_count_max`. The higher this number, the slower the animation. The lower the number (0 is the lowest), the faster the animation. However, this function only matters if you use the internal BOB animation function `Animate_BOB()`. And of course, you must have created a BOB that has multiple frames.

Example:

    // set the rate to change frames every 30 frames

    Set_Anim_Speed_BOB(&alien, 30);

Function Prototype:

    int Set_Animation_BOB(

             BOB_PTR bob,  // ptr of bob to set animation

             int anim_index); // index of animation to set

Purpose:

`Set_Animation_BOB()` sets the current animation that will be played by the BOB. In the earlier example of `Load_Animation_BOB()`, you created four animations.

Example:

    // make animation sequence number 2 active

    Set_Animation_BOB(&alien, 2);

Function Prototype:

    int Animate_BOB(BOB_PTR bob); // ptr to bob to animate

Purpose:

`Animate_BOB()` animates a BOB for you. Normally, you would call this function once per frame to update the animation of the BOB.

Example:

    // erase everything...

    // move everything...

    // animate everything

    Animate_BOB(&alien);

Function Prototype:

    int Move_BOB(BOB_PTR bob); // ptr of bob to move

Purpose:

`Move_BOB()` moves the BOB a delta of xv,yv, and then, depending on the attributes, it will either bounce the BOB off the walls, wrap it around, or do nothing. Similarly to the `Animate_BOB()` function, you would place this call once in the main loop right after (or before) `Animate_BOB()`.

Example:

    // animate bob

    Animate_BOB(&alien);

    // move it

    Move_BOB(&alien);

Function Prototype:

    int Hide_BOB(BOB_PTR bob); // ptr to bob to hide

Purpose:

`Hide_BOB()` simply sets the invisible flag on the BOB so `Draw_BOB()` won’t display it.

Example:

    // hide the bob

    Hide_BOB(&alien);

Function Prototype:

    int Show_BOB(BOB_PTR bob); // ptr to bob to show

Purpose:

`Show_BOB()` sets the visible flag on a BOB so it will be drawn (undoes a `Hide_BOB()` call). Here’s an example of hiding and showing a BOB because you’re displaying a GDI object or something and don’t want the BOB to occlude it:

Example:

    Hide_BOB(&alien);

    // make calls to Draw_BOB and GDI etc.

    Show_BOB(&alien);

Function Prototype:

    int Collision_BOBS(BOB_PTR bob1,  // ptr to first bob

                       BOB_PTR bob2); // ptr to second bob

Purpose:

`Collision_BOBS()` detects if the bounding rectangles of two BOBs overlap. This can be used for collision detection in a game to see if a player BOB hits a missile BOB or whatever.

Example:

    // check if a missile BOB hit a player BOB:

    if (Collision_BOBS(&missile, &player))

       { /* make explosion sound */ }

### Summary

This chapter has taken forever, huh? There’s simply too much material to cover, so I’ve tried to give you just the most important data on a wide variety of topics. However, don’t despair—you’re going to hit all this polygon stuff again when you get to 3D in Volume II, so you’ll be expert when that’s said and done.

Think of this chapter as a primer on a number of topics: rasterization, clipping, line drawing, matrices, collision detection, timing, scrolling, isometric engines, and more. Sometimes you have to take a top-down approach and then follow it up with a bottom-up approach later. Game programming is like that. Anyway, enough philosophy. Now that you have a full bitmap and polygon library, you can do some real damage with the demos during the remaining chapters. Let’s have some fun…

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.