Tamas Nyari
Tamas Nyari

Reputation: 49

My sfml code can't find / load any sprite. Why?

I have this SFML c++ code that is supposed to load a sprite from a file:

Texture TankTexture;
Sprite tank;

std::string path = "tank.png";
TankTexture.loadFromFile(path);
tank.setTexture(TankTexture);

But it gives me an error after I run the code and crashes my app with a bunch of random characters in the terminal. I have also tried with other images and it doesn't work. I made sure that the sprite is in the same folder as the .cpp file. Still doesn't work. Any ideasenter image description here?

Here is the mumbo jumbo the console gives me: enter image description here

Upvotes: 0

Views: 73

Answers (1)

user18514542
user18514542

Reputation:

I don't see a file extension in your screenshot. Make sure the path in the SFML code is absolute or relative: "./tank.png", and that the file you have saved actually matches that path. (there is no .png in screenshot)

Also using when using SFML its good practice to validate if a texture is loaded or not:


if(!texture.loadFromFile("./tank.png")){
    std::cout << "[ERR] could not load image!"
    exit(1)
}

If something went wrong and the texture wasn't loaded (ie the image wasn't there), we exit the program.

Upvotes: 3

Related Questions