Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
## CHAPTER 6 Sprite Techniques This chapter covers the following topics: • The illusion of animation • The illusion of motion • Transparency issues • Collision detection • Animation issues As we discussed in Chapter 2, The Anatomy of a Game, the most important graphical elements of any game are those graphics with which the user interacts, known as sprites. Sprites are such an integral part of any game that knowledge of their implementation and utilization is paramount. Sprites provide the animated elements of a game that make the gaming environment come alive. These can take the form of images that the player directly interacts with, such as a spaceship, or background elements that are non-interactive yet provide animation that makes the environment richer, deeper, and more believable. Without sprites, all you would have is a static bitmap, which would be quite boring. The natural discussion of sprite implementation also leads to such sprite related issues as collision detection. With the exception of the most simplistic, most games need to detect when one sprite comes into contact with another. This would be important when, say, a missile launched from your ship comes into contact with a space-borne hunk of rock. Most players would expect the game to replace the rock with several smaller rocks and increment their score. Thus, it is important to understand the techniques by which sprite collision can be detected. In this chapter, we examine several techniques for implementing sprites, discussing animation and movement as well as collision detection and timing issues. ### The Illusion of Animation The method by which a game displays an animated image on the video screen has actually been around for many decades, even before computers were a household item. Indeed, if you have any knowledge of how movies or cartoons work, you already know the fundamentals of sprite animation. The basic premise is simple. A series of images is required, where each image shows the character or object to be animated in a slightly different position than the image before it, like a filmstrip. Each image is known as a frame.
| frame: A single image in a series of images to be animated.
When each image is rapidly displayed one after another, the human brain expects to see smooth motion, and any gaps in movement from one frame to another (provided it is not too large) will be automatically filled in. In essence, it’s really nothing more than a complex visual illusion. Figure 6-1: Movie frames Creating the illusion of animated images using the computer is almost identical to this technique. Instead of a filmstrip, a series of bitmaps is constructed in which each bitmap contains an image of the character or object in a slightly different position. If each bitmap is copied to the video screen in rapid succession, the illusion of animation will be achieved. This is known as frame animation.
| frame animation: The process of producing animation by rapidly displaying a series of images depicting an object or character in slightly different positions each frame.
Unlike a filmstrip, which contains images of a scene in its entirety, the series of bitmaps to be animated usually contains only an image of the sprite itself. In fact, these images usually only contain images of the sprite in different positions without regard to its actual location in the scene. Location is controlled by other methods, which we’ll see shortly. Of course, a series of bitmaps that does depict an entire scene could be used under special circumstances, such as showing an opening animation or perhaps an animation when the user is moving from one level to another. However, for the purposes of this chapter, we will examine only sprite animation techniques. #### Constructing the Animation Frames The first step that is required is to create the series of bitmaps that will depict the animated object or character. As explained above, each bitmap should display the object or character in a slightly different position. You could store these as individual bitmap files, but the most common practice is to store all of these images in one bitmap, with each frame of the animation in the correct sequence, one after another. Another common practice is to make all frames of the animation equal in size. This simplifies the code when copying the image from the bitmap to the screen, as demonstrated in the example to follow. Figure 6-2: An animated image bitmap #### Producing the Animation The actual process of producing animation is simple. We start by defining a variable that will hold the current frame number, starting at 0. Through each iteration of the game loop, we increment the frame number by 1. If it goes beyond the actual number of frames, reset it to 0. Then, we copy the appropriate frame out of the bitmap onto the screen, using this frame number as an index. Figure 6-3: Producing animation Since each frame of the animation is exactly the same size, it is very easy to calculate the appropriate place in the bitmap from which to copy the image. Assuming that we’ve arranged our frames horizontally in the bitmap, we only need to compute the X offset within the bitmap at which to start and end the copy. These are calculated using the following formulas:
    XStart := FrameNum * WidthOfOneFrame;

    XEnd := XStart + WidthOfOneFrame;
These offsets can easily be used with the Blt or BltFast methods of the IDirectDrawSurface object to copy the correct frame to the backbuffer. Incrementing the frame number each iteration through the game loop causes a new frame of the animation to be displayed each time. If this occurs in rapid succession, and the bitmaps are displayed at the same spot, we achieve the illusion of animation, as the example below illustrates. Listing 6-1: Simple frame animation
    procedure TfrmDXAppMain.DrawSurfaces;

    var

      SourceRect: TRect;      // determines the area to copy from in the source bitmap

      SrfcDC: HDC;            // these two are used to make text drawing easier

      TempCanvas: TCanvas;

    begin

      {incremennt the overall frame number}

      Inc(FFrameNum);

      {start the frame number from the beginning if it goes beyond the

       number of frames in the animation}

      if FFrameNum > 5 then

        FFrameNum := 0;

      {blit the entire snake frame bitmap to illustrate the animation frames}

      SourceRect := Rect(0, 0, 540, 50);

      FBackBuffer.BltFast(50, 75, FImages, SourceRect,

                          DDBLTFAST_NOCOLORKEY OR DDBLTFAST_WAIT);

      {blit the current frame of the animation}

      SourceRect := Rect(FFrameNum*90, 0, (FFrameNum*90)+90, 50);

      FBackBuffer.BltFast(275, 215, FImages, SourceRect,

                          DDBLTFAST_NOCOLORKEY OR DDBLTFAST_WAIT);

      .

      .

      .

    end;
Previous | Table of Contents | Next

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 whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.