Skip to content

Instantly share code, notes, and snippets.

@prewk
Last active August 29, 2015 14:15
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/fbdd5027716660f15c44 to your computer and use it in GitHub Desktop.
Save prewk/fbdd5027716660f15c44 to your computer and use it in GitHub Desktop.
#include <SFML/Graphics.hpp>
// Map of sprite vectors, mapped by name
std::map<std::string, std::vector<sf::Sprite>> sprites;
sf::Texture texture;
// Create some sprite frames and map to a name
void defineSprite(const std::string& name, const 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;
}
// Get a specific sprite frame
const sf::Sprite& getFrame(const std::string &name, int frame) {
return sprites.at(name)[frame];
}
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "Hej");
texture.loadFromFile("resources/Fighter-F-01.png");
defineSprite("TEST", std::vector<sf::IntRect> {sf::IntRect(28, 5, 16, 27)});
bool run = true;
while (run) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
run = false;
}
window.clear(sf::Color::White);
// Works
sf::Sprite spr1 = sf::Sprite(texture, sf::IntRect(28, 5, 16, 27));
spr1.setPosition(0, 0);
window.draw(spr1);
// Doesn't work
sf::Sprite spr2 = getFrame("TEST", 0);
spr2.setPosition(50, 0);
window.draw(spr2);
window.display();
}
}
window.close();
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment