Spartak Aghababyan
Spartak Aghababyan

Reputation: 47

How can I load an image in C++ using SFML library?

How can I load an image in C++ using SFML library?

I am making a game using C++, and for textures of bonuses I want to upload *.png from directory instead of creating them in *.cpp file. How can I do this using SFML library?

Upvotes: 0

Views: 465

Answers (1)

Jakub Bednarski
Jakub Bednarski

Reputation: 281

Try something like:

sf::Texture texture;
if (!texture.loadFromFile("path/to/myTexture.png")) {
    perror("Couldn't load texture \"myTexture\".");
    return;
}  

You can then put it on spirte:

sf::Sprite sprite;
sprite.setTexture(texture);

and finally display it:

sprite.setPosition(x,y);
window.draw(sprite);

Upvotes: 1

Related Questions