Delphi Graphics and Game Programming Exposed! with DirectX For versions 5.0-7.0:Sprite Techniques
Search Tips
Advanced Search
Title
Author
Publisher
ISBN
Please Select
-----------
Artificial Intel
Business & Mgmt
Components
Content Mgmt
Certification
Databases
Enterprise Mgmt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Productivity Apps
Programming Langs
Security
Soft Engineering
UI
Web Services
Webmaster
Y2K
-----------
New Arrivals
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
The Illusion of Motion
Creating the illusion of animation was simple enough. However, even the most complex and beautifully rendered animation would quickly get boring if it stayed in the same spot. So, how do we go about moving the animated sprite across the screen to produce a more interesting and believable animation?
Again, the process is quite simple. On the most primitive level, when the application prepares to draw the next frame of animation, simply change the horizontal or vertical coordinate (or both) at which the image will be drawn into the destination. If these coordinates keep changing, the animated image will be drawn at different positions, creating the illusion of motion.
Sprite Attributes
Before we begin, a few items of information need to be tracked for each sprite. In particular, its current X (horizontal) and Y (vertical) coordinate must be tracked so the application knows where to draw the sprite. A horizontal and vertical velocity could also be stored. These velocities would indicate the number of pixels to move the sprite, and would be added to the X and Y coordinates on each iteration through the game loop. If we also tracked the current frame of animation, the number of frames in the animation, and a pointer to the bitmap or DirectDraw surface containing the sprite images, we could create a basic sprite object that might look like this:
TSprite = class
FXPos, FYPos, // tracks the current position
FXVel, FYVel, // tracks the velocity
Height, Width, // sprite frame height and width
FCurFrame, // tracks the current frame number
FFrameNum: Integer; // tracks the number of frames
FImages: IDirectDrawSurface4; // a pointer to the sprite images
end;
The Animation Cycle
The effect we want to produce is the image of an object moving across the screen in a believable manner. To achieve this illusion of motion, we will tackle the process of animation by breaking it into three distinct steps: erasing the sprite image, updating the sprite position, and drawing the sprite image.
If we simply move the sprite and continue to draw it on the screen without ever erasing it, the previous sprite images will still be visible, ruining the animation effect. We want our animation to look like a single object moving about as one would expect to see in a cartoon or movie. Thus, the first thing we must do is erase the previous sprite image. For this example, we will simply clear the entire surface by performing a color fill using the Blt method. The IDirectDrawSurface4s Blt method is defined as:
function Blt(
lpDestRect: PRect; // the destination rectangle
lpDDSrcSurface: IDirectDrawSurface4; // the source surface
lpSrcRect: PRect; // the source rectangle
dwFlags: DWORD; // control flags
lpDDBltFx: PDDBltFX // a pointer to a TDDBltFX structure
): HResult; // returns a DirectX error code
The first parameter describes the rectangular area in the destination surface to which the image data is copied. The second parameter identifies the surface whose image data is being copied. The third parameter describes the rectangular area in the source surface from which the image data is copied. Note that if these rectangular areas are not the same size, the resulting image will be scaled as required. The fourth parameter is a series of flags indicating which members of the final parameter are valid and generally control the behavior of the blit. The final parameter is a pointer to a TDDBltFX structure. This rather large and complex structure contains information for various special graphical effects, such as color fills and raster operations. Well see an example of this functions use to perform a color fill in Listing 6-2.