;DHello everybody,
first post by me.
In my project, I have a class that should handle Sprites that are made of a pixel array.
The class has a vector of Sprites, which is looped over and the individual Sprites should be drawn every update cycle. Now I made a method that creates new sprites of the pixel array and pushes them onto the vector.
sth like this (shortened pseudocode version):
void CSpriteHandleClass::createButton(/*params*/){
sf::Image image;
sf::Sprite sprite;
sf::Texture texture;
//create UInt8 pixel array & fill it
//create image from the pixel array
//set texture from the image
//set sprite from the texture
global_spritevector.push_pack(sprite);
}
(in main it loops over the vector and draws every sprite)
When I then run the code, everything seemed okay except that the texture of the sprite had no color (it should've been red, as I filled it that way). I found out, that if I declare the Texture in the pseudocode as a pointer (sf::Texture* texture = new sf::Texture) it would work and the colors showed up.
I guess the Sprite is only storing a pointer to the texture which dies when getting out of the scope (method)?
But why does the Sprite itself live on and the Texture not? They should both be copied into the vector I thought, so technically even as a non-pointer the data should be there, doesn't it?? I looked up the reference (
https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Texture.php#details) but except "it lives in the graphics card" nothing regarding the life time of the texture object is said. I'm really confused, would love if someone could help me understand whats going on.
Greetings!