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

Author Topic: sf::texture destroyed when in class  (Read 5856 times)

0 Members and 1 Guest are viewing this topic.

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
sf::texture destroyed when in class
« 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

man'O'war

  • Newbie
  • *
  • Posts: 46
  • What needs to be done is done by those capable.
    • View Profile
Re: sf::texture destroyed when in class
« Reply #1 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 ?

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: sf::texture destroyed when in class
« Reply #2 on: August 21, 2016, 07:08:14 pm »
just with a function similar to this
void draw(RenderWindow &window){
    window.draw(sprite);
}
 

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: sf::texture destroyed when in class
« Reply #3 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

man'O'war

  • Newbie
  • *
  • Posts: 46
  • What needs to be done is done by those capable.
    • View Profile
Re: sf::texture destroyed when in class
« Reply #4 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)

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: sf::texture destroyed when in class
« Reply #5 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)

man'O'war

  • Newbie
  • *
  • Posts: 46
  • What needs to be done is done by those capable.
    • View Profile
Re: sf::texture destroyed when in class
« Reply #6 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.
« Last Edit: August 21, 2016, 08:39:27 pm by man'O'war »

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: sf::texture destroyed when in class
« Reply #7 on: August 21, 2016, 09:31:56 pm »
Thank you.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::texture destroyed when in class
« Reply #8 on: August 21, 2016, 09:37:43 pm »
I'd say that should probably be
std::vector<std::unique_ptr<Slider>> sliders;

 

anything