SFML community forums
Help => Graphics => Topic started by: affenmehl8 on June 13, 2019, 04:37:30 pm
-
So I'm new to SFML. I read a lot of post, but I really don't get it.
I wrote an texture holder:
class tile_texture_holder {
private:
sf::Texture tx;
public:
tile_texture_holder(type a) {
switch (a) {
case type::desert:
tx.loadFromFile("C:/Users/Andreas/source/repos/conquer/Media/desert.png");
break;
case type::grass:
tx.loadFromFile("C:/Users/Andreas/source/repos/conquer/Media/grass.png");
break;
case type::mountain:
tx.loadFromFile("C:/Users/Andreas/source/repos/conquer/Media/mountain.png");
break;
case type::water:
tx.loadFromFile("C:/Users/Andreas/source/repos/conquer/Media/water.png");
break;
}
}
sf::Texture ret_texture() {
return tx;
}
~tile_texture_holder() {
std::cout << "///////////////////////HOLDER DELETED!!!/////////////////////" << std::endl;
}
};
And I tried to load a sprite with it in different ways....
For example:
tile_texture_holder t(type::desert);
sf::Sprite s;
s.setTexture(t.ret_texture());
(in the same function, where I draw the sprite)
I always get the white box being drawn. And I really dont get why the texture_holder is getting deleted.
BTW type is an enum.
I hope somebody can help me solve my issue!
-
You create a new, local tile_texture_holder which creates a texture, then you set the texture to the sprite.
But since the tile_texture_holder is local, it will be destroyed as soon as you run out of scope, destroying the texture and thus the reference from the sprite to the texture becomes invalid.