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

Author Topic: Issue with display of pictures  (Read 423 times)

0 Members and 1 Guest are viewing this topic.

JasonLeon

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Issue with display of pictures
« on: October 03, 2023, 09:29:26 pm »
Dear Community,
I don't know how to express this problem explicitly..
I set a class like this:
class Image {
public:
void init();
void show();
private:
Sprite spr;
};
and a vector like vector <Image*> pics;
init() could initialize spr and pics.push_back(this) and show() is just window.draw(spr); and window.display();.
Here comes the problem. When it comes to pics[0]->show();, the screen becomes whole white.
I'm sure the problem is from the sentence pics[0]->show();, because when I delete it and put show() at the bottom of init(), everything works fine.
I hope you can understand what I was saying according to the attaching sceenshots, because my expression was really poor.

Best regards.
« Last Edit: October 03, 2023, 09:35:14 pm by JasonLeon »

kojack

  • Sr. Member
  • ****
  • Posts: 328
  • C++/C# game dev teacher.
    • View Profile
Re: Issue with display of pictures
« Reply #1 on: October 04, 2023, 04:08:53 am »
In your setSprite function, you make a texture and assign it to the sprite. But the texture is just a local variable, so when the function ends the texture is deleted and the sprite now contains an invalid pointer to where the texture used to be.
Textures need to have at least the same lifetime as a sprite, since sprites don't make a copy of the texture.

JasonLeon

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Issue with display of pictures
« Reply #2 on: October 04, 2023, 07:06:40 am »
In your setSprite function, you make a texture and assign it to the sprite. But the texture is just a local variable, so when the function ends the texture is deleted and the sprite now contains an invalid pointer to where the texture used to be.
Textures need to have at least the same lifetime as a sprite, since sprites don't make a copy of the texture.
Thank you. Problem solved. :D