I couldn't seem to find anything to help me with sprite animation using sprite sheets, so I experimented a bit and got it to work. I was wondering if I could do this in a better way or if someone has a link to a useful page. Here's my code:
//SpriteSheet.h:
#include <SFML/Graphics.hpp>
class SpriteSheet
{
public:
SpriteSheet(sf::Image &spritesheet, int frameHeight, int frameWidth, int frames);
void updateSprite(int animationID);
// Some functions that aren't important here
private:
sf::Image imgSheet;
sf::Sprite sheet;
int currentFrame;
int frameHeight;
int frameWidth;
int animationID;
};
// SpriteSheet.cpp:
#include <SFML/Graphics.hpp>
#include "SpriteSheet.h"
using namespace sf;
SpriteSheet::SpriteSheet(Image &spriteSheet, int frameHeight, int frameWidth, int frames)
{
imgSheet = spriteSheet;
sheet.SetImage(spriteSheet);
sheet.SetSubRect(IntRect(0, 0, frameWidth, frameHeight)); // Default subrect
}
void SpriteSheet::updateSprite(int animationID)
{
currentFrame++; // Increments the frame
currentFrame %= imgSheet.GetWidth() / frameWidth; // Makes the animation loop
sheet.SetSubRect(IntRect(frameWidth * currentFrame,
frameHeight * animationID,
frameWidth * (currentFrame + 1),
frameHeight * animationID));
}
// Some functions here that aren't important
Any help would be appreciated, thanks.