Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite not displaying after successful texture load  (Read 225 times)

0 Members and 1 Guest are viewing this topic.

jhender

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Sprite not displaying after successful texture load
« 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.
« Last Edit: June 17, 2024, 02:11:30 am by jhender »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
Re: Sprite not displaying after successful texture load
« Reply #1 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();
    }
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jhender

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Sprite not displaying after successful texture load
« Reply #2 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3368
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite not displaying after successful texture load
« Reply #3 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*