SFML community forums

Help => Graphics => Topic started by: Weeve on February 09, 2013, 06:26:12 pm

Title: White box instead of texture when using assignment operater on sf::Texture
Post by: Weeve on February 09, 2013, 06:26:12 pm
probably did something stupid, or do not have proper understanding of sf::Texture:

// Pic is a valid sf::Texture pointer, created in main
Class::Class(sf::Texture* Pic){
   // _Texture is a member variable, so is _Sprite
   // currently only use of _Texture
   _Texture = *Pic; // assign _Texture to the value of Pic
   _Sprite = sf::Sprite(_Texture); // does not work, white rectangle drawn
   //_Sprite = sf::Sprite(*Pic);   draws the proper Texture
}
 
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: Laurent on February 09, 2013, 06:52:58 pm
Can you show the code where you instanciate and use this class? You're probably doing copies, and your copy constructor and assignment operators are not correctly implemented.
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: Weeve on February 09, 2013, 07:08:52 pm
Class uses a default copy constructor and default assignment operators, Whenever the class is used, it is used as an element of a vector, the _ClassList is working properly

creation of class:
void Window::MakeClass(sf::Texture* Pic){
   // copy a default class over to the vector
   _ClassList.push_back(Class(Pic));
}
 
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: Nexus on February 09, 2013, 07:15:47 pm
This is the problem. If a sf::Texture is copied, the new object will reside at a different address than the old one, so the sf::Sprite still refers to the old one.

By the way, identifiers beginning with underscore and capital letter like _Texture are reserved for the implementation. You should not use them in your own program. For members, you could for example take mTexture.
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: Weeve on February 09, 2013, 07:38:40 pm
thanks about the _Names being for implementation, I was working on a project a while back that enforced using _Name, and never switched back to m_Name :-) all future code will be using it,

how do I properly copy a texture? I want a new pixel set to be created, but the pixel set to have the same values as the old texture, your wording is slightly hard to understand, I want it to reside at a different address, don't I? but if it is a different address, how could it refer to the old address?
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: eXpl0it3r on February 09, 2013, 08:39:23 pm
how do I properly copy a texture? I want a new pixel set to be created, but the pixel set to have the same values as the old texture, your wording is slightly hard to understand, I want it to reside at a different address, don't I? but if it is a different address, how could it refer to the old address?
Maybe this illustrates it a bit:
 Sprite
  |
  v
[Tex]
 
 Sprite
  |
  v
[Tex]      [Copy]

 Sprite
  |
  v
[DELETED]  [Copy]
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: Weeve on February 09, 2013, 09:15:55 pm
Not understanding the picture.. text please? what got deleted?

most of all, what should I do to copy a texture properly?
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: Laurent on February 09, 2013, 09:28:08 pm
Your texture is copied properly. The sprite is also copied correctly, bu the copied sprite still refers to the original texture, not the copied one. So you must explicitely write the copy constructor to make your new sprite use the new texture, instead of the old one (which doesn't exist anymore).

I'll try my own version of the illustrated explanation ;D

original Class
{
    original texture
    original sprite (uses original texture, ok)
}

copied Class
{
    copied texture
    copied sprite (still uses original texture, not copied texture, oops!)
}

From a different point of view, ask yourself which line of code makes the copied sprite use the copied texture. The answer is: none, because you use the copy constructor and your own custom constructor is not used.
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: Weeve on February 09, 2013, 09:59:42 pm
Oh, so when I copy my Class, it copies the texture and the sprite, but I need to update the sprite to use the copied texture instead of the old one, thanks :D
Title: Re: White box instead of texture when using assignment operater on sf::Texture
Post by: Laurent on February 09, 2013, 10:18:18 pm
Exactly ;)