Reputation: 464
I have a DBObject that stores media
(an sf::Sprite
) and mediaTexture
(an sf::Texture
). This is my DBObject::loadMedia()
function:
void DBObject::loadMedia() {
myStream mediaStream(mediaPath);
mediaTexture.loadFromStream(mediaStream);
media.setTexture(mediaTexture);
mediaInit = true;
}
These DBObjects are stored in a std::vector
.
When I try to draw the sprite to the display, it is completely white, and apparently this is from the texture going out of scope. But I define the texture in the DBObject, so it should have a lifetime of the object... right?
However, when I make a NEW sprite in my main() function, and do newSprite.setTexture(DBObj.mediaTexture)
, it is displayed fine, so the texture itself is loading fine, but something is happening to the sprite.
Upvotes: 0
Views: 122
Reputation: 3530
This is called the white square problem.
Basically, at some point, your object is copied but the copy constructor doesn't update the copied sprite texture to use the copied texture, and the original texture is destroyed so the copied sprite doesn't have a valid texture anymore. This can happens for example when your std::vector
needs to allocate more memory and move/copy stuff around.
Upvotes: 1