Skip to content

Instantly share code, notes, and snippets.

@prewk
Created February 8, 2015 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prewk/ea73c9345e0b72a130f7 to your computer and use it in GitHub Desktop.
Save prewk/ea73c9345e0b72a130f7 to your computer and use it in GitHub Desktop.
class SpriteSheet
{
public:
SpriteSheet();
SpriteSheet(const std::string& filename);
void defineSprite(std::string name, std::vector<sf::IntRect> frames);
const sf::Sprite& getFrame(const std::string& name, int frame);
sf::Texture texture;
private:
std::map<std::string, std::vector<sf::Sprite>> sprites;
};
SpriteSheet::SpriteSheet() {}
SpriteSheet::SpriteSheet(const std::string &filename)
: sprites{}
{
if (!texture.loadFromFile(filename)) {
std::cerr << "Couldn't load " << filename << std::endl;
}
}
void SpriteSheet::defineSprite(std::string name, std::vector<sf::IntRect> frames) {
std::vector<sf::Sprite> namedFrames(frames.size());
for (auto& i : frames) {
namedFrames.push_back(sf::Sprite(texture, i));
}
sprites[name] = namedFrames;
}
const sf::Sprite& SpriteSheet::getFrame(const std::string &name, int frame) {
return sprites[name][frame];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment