I'll give you my own Animated sprites class : Gfx
http://www.megaupload.com/?d=PPDBQ0E0BE CAREFULL, THERE IS NO COMMENTS ON THE SOURCES§§
In some word :
Gfx is an enhanced class inheriting of sf::Sprite. Gfx as just a supplemental parametter to make it rotate over time.
It is splitted into other inheriting class :
- AnimatedGfx : animate a gfx over the time from a tilesheet. You can set it to loop or not, and to play the animation backward.
- AngledGfx : animate a gfx from an angle. The best exemple is to animate a turret. From the direction the turret is facing, the class display the right frame.
- ControlledGfx : animate a gfx choosing a specific frame. It is used for exemple for the waving effect of a shmup ship going left and right. You change the desired frame and it go smoothly to it from the precedent frame.
For the multiple tile class to work, you need to have your tilesheet to be parsed in this way : From left to right, then from top to bottom AND the tiles must have the same width and height. If you don't do so, you will have some weird animation.
For angled animation, your first tile must face down (270°), or else you will have an offset. If you look into the AngledGfx.cpp, at this formulae :
current_frame = (static_cast<int>(frame_list.size()*((static_cast<unsigned int>(gfx_angle)%359 + (360.f/(frame_list.size()*2)))/360.f))+
4)%16;
The 4 in bold is used to offset the frame orientation. If you want your tilesheets to start from 90° or 0°, juste change this value, but it will apply to all your tilesheets so be carefull.
Exemple :
http://img246.imageshack.us/img246/425/explosion1.pngIt is also enhanced with class call GfxEffect, used to control over the time the basic attributes of a sprite, like alpha, size and in some way the position.
Is is based on raw pointers (I hear some guy growl from here!), so you use it with new and delete.
Here is an exemple to create it:
// We load an image
sf::Image image;
image.LoadFromFile("pwet.png");
// We create a simple animated gfx
// first param : the image we use for the graphics
// 2nd param : the number of horizontal tiles
// 3rd param : the total number of tiles
// If you specify the right amount of tiles in both previous parameter, your image can have blank tiles at the end, it will be ignored.
// 4th param : the delay between each frame. 0.033f is the good delay for a 30 fps animation
// 5th param : we want the animation to loop or not.
Gfx *gfx = new AnimatedGfx(pwet, 5, 14, 0.033f, false);
// We add a fading effect, to play on the alpha of the image.
gfx->AddEffect(new FadeEffect());
while(app.IsOpened())
{
// If we press E key, the gfx start fading from it's current alpha to 0 during 1 second
if(app.GetInput().IsKeyDown(sf::Key::E))
gfx->GetEffect(0)->Start(gfx, 0, 1.f);
// If we press T key, the gfx start fading from it's current alpha to 255 during 2,5 seconds
if(app.GetInput().IsKeyDown(sf::Key::T))
gfx->GetEffect(0)->Start(gfx, 255, 2.5f);
// We refresh the gfx to make it calculate the right frame and apply effect;
gfx->Refresh();
// And we draw it
app.Draw(*gfx);
}
OF COURSE, if somes want to enhance those classes (thinking about boost pointer), you are gladly welcome to do it. =p