Implementing Animation

If you are using the LAnimationWindow (or derived) class, the class object will perform all of the processing required to implement animation for you.

Alternatively, to load and play an animated GIF file, you can load a list of bitmaps from the file, then play the list to a target bitmap in a loop that displays each change in the target bitmap. You can also create an animation sequence from scratch and save the bitmaps to create an animated GIF file.

The following is an outline of possible steps:

1.

Create and load a bitmap list using LFile::LoadBitmapList.

2.

Use LBitmapBase::Create to create a target bitmap that is the size of the images in the bitmap list.

3.

Use LPlayBack::Create to create an animation playback that references bitmap list and the target bitmap.

4.

In a loop that processes each bitmap in the list, do the following:

 

a.

Use LPlayBack::Process to process the current state and get the next state of the playback.

b.

Use LPlayBack::GetUpdateRect to get the update rectangle (the portion of the target bitmap that has changed).

c.

Use LPaint::PaintDC to paint the changed portion of the bitmap. To paint only the changed portion, use the update rectangle as the source clipping rectangle.

The playback loop can have a number of states, and you should only paint the changes in an appropriate state (for example, when the next state is PLAYSTATE_POSTRENDER). For a list of possible states, refer to Animation Playback States.

The DisposalMethod field in the BITMAPHANDLE for the target bitmap determines what happens to the bitmap in the playback loop after rendering and after any wait state, when the next state is PLAYSTATE_POSTDISPOSE. The options are as follows: keep the image as it is, restore the background, or restore the previous image. (Restoring the background is a common option for animation.)

The playback loop goes continuously through the list of bitmaps. If you want to stop at the end of the list, you can exit the loop when the next state is PLAYSTATE_END.

Animated GIF Files

Before you load an animated GIF file, you can use the LFile::GetInfo function to get the file's global animation information, which is updated in the following FILEINFO fields: Flags, GlobalLoop, GlobalWidth, GlobalHeight, GlobalBackground, and GlobalPalette. You can use these values in the animation playback.

When you load an animated GIF file, using LFile::LoadBitmapList, the following animation fields in each BITMAPHANDLE are updated with information from the file: Left, Top, Delay, Background, Transparency, and DisposalMethod. These fields are used in the animation playback.

When you save an animated GIF file, using LFile::SaveBitmapList, these same BITMAPHANDLE fields are saved in the file. In addition, if you want to save the global animation information, you can use the SAVEFILEOPTION structure to set the following fields: Flags, GlobalLoop, GlobalWidth, GlobalHeight, GlobalBackground, and GlobalPalette. Notice that the field names are the same as in the FILEINFO structure (although the values of the Flags field are different).

When loading an AVI file, the same BITMAPHANDLE and FILEINFO fields are updated, but you cannot save an AVI file.

Code Examples

Code examples for supporting functions show how to implement various features. Refer to the following function descriptions:

Function

What the Example Shows

LPlayBack::Process

This example loads a bitmap list from an animated GIF or AVI file. It then plays the animation, using the current bitmap as the target.

LPlayBack::Append

This example plays an animated GIF or AVI file as it loads the file. The example includes both the calling function and the LFile::LoadFileCallBack function, which displays the image.

LBitmapList::Create

This example creates a bitmap list from the current bitmap, rotating each copy by 10 degrees. It defines the rotation fill color as a transparent color, and assigns an animation background color. It then plays the animation, using the current bitmap as the target.

LBitmapList::GetItem

This example changes the hue of bitmaps in a list, then updates the paint palette and the target bitmap's palette for animation playback. When changing the hue, it preserves the transparent color in the last palette entry.

Related Functions

For a summary of the functions for creating and managing a bitmap list, refer to Creating and Maintaining Lists of Images.

For a summary of the animation playback functions, refer to Playing Animated Images.

 

You can also use the high-level class LAnimationWindow to implement animation. This class creates a window and does all of the processing required to implement animation for you.