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:

`Create_BOB()` creates a single BOB object and sets it up. The function sets up all the internal variables in addition to creating a separate DirectDraw surface for each frame. Most of the parameters are self-explanatory; the only value that needs a little explanation is the attribute variable `attr`. Take a look at Table 8.4 to see a better description of each of the attributes you can logically `OR` together and send in this field.

Table 8.4 Valid BOB Attributes

Value | Description
--- | ---

`BOB_ATTR_SINGLE_FRAME` | Creates BOB with a single frame.
`BOB_ATTR_MULTI_FRAME` | Creates BOB with multiple frames, but the animation of the BOB will be a linear sequence through the frames 0..n.
`BOB_ATTR_MULTI_ANIM` | Creates a multiple frame BOB that supports animation sequences.
`BOB_ATTR_ANIM_ONE_SHOT` | If this is set, an animation sequence will play only once and then stop. At that point the internal variable `anim_state` will be set. To play the animation again, reset this variable.
`BOB_ATTR_BOUNCE` | This flag tells the BOB to bounce off the screen boundaries like a ball. This only works if you use `Move_BOB()`.
`BOB_ATTR_WRAPAROUND` | This flag tells the BOB to wrap around to the other side of the screen as it moves. This only works if you use `Move_BOB()`.

Examples:

Here are some examples of creating BOBs. First, a single-frame BOB at (50,100) with a size of 96×64:

    BOB car; // a car bob

    // create the bob

    if (!Create_BOB(&car, 50,100,

                    96,64,1,BOB_ATTR_SINGLE_FRAME,0))

       { /* error */ }

And here’s a multiple-frame BOB with eight frames and a size of 32×32:

    BOB ship; // a space ship bob

    // create the bob

    if (!Create_BOB(&ship, 0,0,

                    32,32,8,BOB_ATTR_MULTI_FRAME,0))

       { /* error */ }

Finally, a multiple-frame BOB that supports animation sequences:

    BOB greeny; // a little green man bob

    // create the bob

    if (!Create_BOB(&greeny, 0,0,

        32,32,32,BOB_ATTR_MULTI_ANIM,0))

       { /* error */ }

> Warning: For this function to work, the destination surface must not be locked.

Function Prototype:

    int Destroy_BOB(BOB_PTR bob); // ptr to bob to destroy

Purpose:

`Destroy_BOB()` destroys a previously created BOB. You would do this when you are done with the BOB and want to release the memory it used back to Windows.

Example:

    // destroy the BOB above, you would do this

    Destroy_BOB(&greeny);

Function Prototype:

    int Draw_BOB(BOB_PTR bob,  // ptr of bob to draw

      LPDIRECTDRAWSURFACE dest); // dest surface to draw on

Purpose:

`Draw_BOB()` is a very powerful function. It draws the sent BOB on the DirectDraw surface that you send it. The BOB is drawn in its current position and current frame, as defined by its animation parameters.

Example:

    // this is how you would position a multiframe BOB at

    // (50,50) and draw the first frame of it on the back

    // surface:

    BOB ship; // a space ship bob

    // create the bob

    if (!Create_BOB(&ship, 0,0,

                    32,32,8,BOB_ATTR_MULTI_FRAME,0))

    // load the bob images in..well get to this in a bit

    // set the position and frame of bob

    ship.x = 50;

    ship.y = 50;

    ship.curr_frame = 0; // this contains the frame to draw

    // draw bob

    Draw_BOB(&ship, lpddsback);

Function Prototype:

    int Draw_Scaled_BOB(BOB_PTR bob, // ptr of bob to draw

      int swidth, int sheight, // new width and height of bob

      LPDIRECTDRAWSURFACE dest); // dest surface to draw on

Purpose:

`Draw_Scaled_BOB()` works exactly like `Draw_BOB()`, except that you can send a new width and height to draw the BOB with. This is very cool, and if you have acceleration, it’s a great way to scale a BOB to make it look 3D!

Example:

    // an example of drawing the ship 128x128 even though

    // it was created as only 32x32 pixels

    Draw_Scaled_BOB(&ship, 128,128,lpddsback);

Function Prototype:

    int Load_Frame_BOB(

     BOB_PTR bob, // ptr of bob to load frame into

     BITMAP_FILE_PTR bitmap,// ptr of bitmap file to scan data

     int frame,   // frame number to place image into 0,1,2...

     int cx,int cy, // cell pos or abs pos to scan from

     int mode);     // scan mode, same as Load_Frame_Bitmap()

Purpose:

The `Load_Frame_BOB()` function works identically to the `Load_Frame_Bitmap()` function, so refer to that for details. The only additional control parameter `frame` is the frame to load. If you create a BOB that has four frames, you’ll load the frames in one by one.

Example:

    // here’s an example of loading 4 frames into a BOB from a

    // bitmap file in cell mode

    BOB ship; // the bob

    // loads frames 0,1,2,3 from cell position (0,0), (1,0),

    // (2,0), (3,0)

    // from bitmap8bit bitmap file, assume it has been loaded

    for (int index=0; index<4; index++)

          Load_Frame_BOB(&ship,&bitmap8bit,

                         index, index,0,

                         BITMAP_EXTRACT_MODE_CELL);

Function Prototype:

    int Load_Animation_BOB(

       BOB_PTR bob,    // bob to load animation into

       int anim_index, // which animation to load 0..15

       int num_frames, // number of frames of animation

       int *sequence); // ptr to array holding sequence

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.