Search code examples
c++texturessfml

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?


Solution

  • 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);