Animation – Sprite Sheets

 

This example program shows how to animate sprites using a series of images stored in one file. Instead of loading in a series of files, we can load one image containing all the frames laid out in a grid pattern (a sprite sheet).

Normally the sprite’s u and v coordinates range from 0 to 1 which displays the entire image. But we can change the variables u1, u2, v1, and v2 to show only a portion of the image. To make the math a little easier for you, I added a member function to the sprite class called SelectAnimationFrame. Since the sprite doesn’t know anything about the image you are applying to it, we have to tell it how many rows and columns there are. The function will do the rest assuming that the frames are properly laid out in a grid pattern.

 

explodeSpr.SelectAnimationFrame(2, 4, int(frame));

 

A detailed explanation of the code is provided in the comments. Please read through all the comments and code found in Main.cpp. And as always, feel free to experiment with the code or replace the graphics with your own designs. Download the Visual Studio project by clicking on the link below:

 

Animation2.zip  400KB