SFML community forums

Help => Graphics => Topic started by: jhender on June 16, 2024, 10:13:37 pm

Title: Sprite not displaying after successful texture load
Post by: jhender on June 16, 2024, 10:13:37 pm
I'm loading a texture and passing it to my sprite - I know the texture is being loaded correctly as no error message is displayed (and have tried w/ other images that work correctly), but when trying to draw the Sprite I see no result.

Here is the code:
if (!texture.loadFromFile("Media/Textures/eagle.png")) {
    // Error handling as .loadFromFile() returns a boolean.
    std::cout << "Could not import texture.";
}

mPlayer.setTexture(texture);
 

and the player is being drawn in the following:
void Game::render() {

    mWindow.clear(sf::Color::Black);
    mWindow.draw(mPlayer);
    mWindow.display();

}

I've checked to make sure the image is compatible with SFML, that the file is ACTUALLY a png and not manually renamed, that all linkages & directory paths are working (have tried both relative and absolute paths). At this point I'm completely lost and could use some help.
Title: Re: Sprite not displaying after successful texture load
Post by: eXpl0it3r on June 17, 2024, 09:55:36 am
When you say "I see no result", do you just see the black background?

Have you run it through a debugger to ensure that the code you're expecting to be called is actually called?

If you try the following minimal example, does it work?
#include <SFML/Graphics.hpp>
 
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
 
    sf::Texture texture;
    if (!texture.loadFromFile("Media/Textures/eagle.png"))
        return -1;
    sf::Sprite sprite(texture);
 
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
 
        window.clear();
        window.draw(sprite);
        window.display();
    }
}
Title: Re: Sprite not displaying after successful texture load
Post by: jhender on June 17, 2024, 07:59:37 pm
Running that code still didn't work, and honestly I'm thinking it has something to do with the .png file, but I'm not sure what. I ended up using a different image and it's been working, so I suppose all's well that ends okay.
Title: Re: Sprite not displaying after successful texture load
Post by: Hapax on June 18, 2024, 09:47:22 pm
Sometimes, just loading it into an editor and re-saving it can fix these weird anomalies.

At least you can be clear exactly which format is being used and you can (re-)save other images using that exact format in the future.
Title: Re: Sprite not displaying after successful texture load
Post by: hdsiria on June 26, 2024, 04:41:38 pm
You'll need to make sure that the image is loaded and that the textured sprite is visible