Montreal
Montreal

Reputation: 2623

Why does SFML load a white square instead of a texture?

I am working on a class which manages the loading of multiple textures. Before I've done that everything worked fine, but when I use my brand-new class, only a white square appears on the screen:

namespace Textures {
    enum ID { Landscape, Airplane, Missile };
}

class ATextureHolder {
public:
    void                Load(Textures::ID id, const std::string& filename);
    sf::Texture&        Get(Textures::ID);
private:
    std::map<Textures::ID, std::unique_ptr<sf::Texture>> texture_map;

};

void
ATextureHolder::Load(Textures::ID id, const std::string& filename) {
    std::unique_ptr<sf::Texture> texture( new sf::Texture() );
    texture->loadFromFile(filename.c_str());

    this->texture_map.insert(std::make_pair(id, std::move(texture)));
}

sf::Texture&
ATextureHolder::Get(Textures::ID id) {
    auto found = this->texture_map.find(id);
    return *found->second;
}

AGame::AGame():
    window( sf::VideoMode( 640, 480 ), "SFML Game" ),
    player(), //player is an instance of sf::Sprite class
    player_speed( 100.0 ),
    time_per_frame( sf::seconds( 1.0 / 60.0 ) ) {

        ATextureHolder textures;
        textures.Load(Textures::Airplane, "Media/Textures/plane.png");
        this->player.setTexture(textures.Get(Textures::Airplane));
        this->player.setPosition(100.0, 100.0);
}

The idea is to bind texture names stored in enum ID with unique pointers, which in their turn point to actual sf::Texture objects. So the class makes a binding in the Load() method and the Get method should return a reference to these texture objects. But something goes wrong and I have no idea what is it.

Upvotes: 0

Views: 1079

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

I'm just guessing here, but... In the AGame constructor, the textures variable is a local variable which goes out of scope and is destructed once the constructor returns. This destruction of textures or course means that the map it contains is also destructed, and with it the unique_ptr objects, leading to the reference returned by ATextureHolder::Get to become invalid.

Unless the this->player.setTexture takes its argument by value and does a deep copy, then the texture will be invalid and any use of the object will lead to undefined behavior.

Upvotes: 2

Related Questions