SFML community forums

Help => Graphics => Topic started by: ormenbjorn on August 21, 2016, 02:41:48 pm

Title: sf::texture destroyed when in class
Post by: ormenbjorn on August 21, 2016, 02:41:48 pm
hi! I wanted to have a texture in a class like this:

 
class Thing{
    private:
    sf::Texture texture;
    sf::Sprite sprite;

    public:
    Thing(){
        texture.loadFromFile("file");
        sprite.setTexture(texture);
    }

    void draw(){
        //draw
    }
}
 

but then, the texture is gone in the draw() function and the sprite is white.
When I do
std::cout << &texture << endl;
, nothing shows up.

I have read about how the texture is destroyed if you make it in a function, which is pretty obvious to me, but i'm just loading the texture in a function here, which i think should work?
Thanks
Title: Re: sf::texture destroyed when in class
Post by: man'O'war on August 21, 2016, 04:15:31 pm
Hi,

As long as the object is alive, the texture should be .

How are you drawing it ?
Title: Re: sf::texture destroyed when in class
Post by: ormenbjorn on August 21, 2016, 07:08:14 pm
just with a function similar to this
void draw(RenderWindow &window){
    window.draw(sprite);
}
 
Title: Re: sf::texture destroyed when in class
Post by: ormenbjorn on August 21, 2016, 07:16:38 pm
the class is inheriting another class and being used by another one(not the inherited one), should i post all of the code? it's not that long if i take away the unneeded parts
Title: Re: sf::texture destroyed when in class
Post by: man'O'war on August 21, 2016, 07:37:05 pm
Yes, i would like to see the code

Because i've tested a minimal code, and it's just working fine.

main.cpp
(click to show/hide)

Thing.h
(click to show/hide)

Thing.cpp
(click to show/hide)
Title: Re: sf::texture destroyed when in class
Post by: ormenbjorn on August 21, 2016, 08:13:23 pm
Here's code that replicates the problem:

main.cpp:
(click to show/hide)

Map.h:
(click to show/hide)

Map.cpp:
(click to show/hide)

Slider.h
(click to show/hide)

Slider.cpp:
(click to show/hide)
Title: Re: sf::texture destroyed when in class
Post by: man'O'war on August 21, 2016, 08:37:52 pm
The problem comes from this part
vector<Slider> sliders;

It should be like that.
class Slider{
    private:
        vector<Slider*> sliders;
}


Map::Map(){
    sliders.push_back( new Slider(Vector2f(0, 0)));
    sliders.push_back( new Slider(Vector2f(200, 200)));
}

void Map::draw(RenderWindow &window) {
    sliders[0]->draw(window);
    sliders[1]->draw(window);
}

 

The problem comes from here.
    sliders.push_back(Slider(Vector2f(0, 0)));
 

It is equivalent to:
Slider s1 = Slider(Vector2f(0, 0));
sliders.push_back(s1);
 

You create a Slider s1, The sprite of s1 is pointing to the address of the texture inside s1.
Then you push it into the vector, which creates a copy of it. In that case, the Sprite of the new copy is still pointing to the address of the texture inside s1. And when the constructor goes out of scope, ( finishes ), s1 will be destroyed, and vector(i) will have no texture attached.
Title: Re: sf::texture destroyed when in class
Post by: ormenbjorn on August 21, 2016, 09:31:56 pm
Thank you.
Title: Re: sf::texture destroyed when in class
Post by: Jesper Juhl on August 21, 2016, 09:37:43 pm
I'd say that should probably be
std::vector<std::unique_ptr<Slider>> sliders;