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

Author Topic: White box instead of texture when using assignment operater on sf::Texture  (Read 3659 times)

0 Members and 1 Guest are viewing this topic.

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
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
}
 
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #1 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.
Laurent Gomila - SFML developer

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #2 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));
}
 
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #3 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #4 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?
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10879
    • View Profile
    • development blog
    • Email
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #5 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]
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #6 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?
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #7 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.
« Last Edit: February 09, 2013, 09:30:43 pm by Laurent »
Laurent Gomila - SFML developer

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #8 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
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: White box instead of texture when using assignment operater on sf::Texture
« Reply #9 on: February 09, 2013, 10:18:18 pm »
Exactly ;)
Laurent Gomila - SFML developer

 

anything