SFML community forums

Help => Graphics => Topic started by: OriginalBrand on June 28, 2018, 05:37:13 am

Title: Std::Map texture not wokring
Post by: OriginalBrand on June 28, 2018, 05:37:13 am
For the following code even though nametext = "card001" and when I hard code card001.Art it works. When I don't it gives me the normal white square. This is really confusing me.  ;D

       

struct tempCard {
        sf::Texture Art;

        std::string Texture;
        //"Sprites/BasicCard.png"
        void loadArt() {
                if (!Art.loadFromFile(Texture) ){
                        // error...
                }
        }
};

.....
for (int i = 0; i < Player.handMax;i++) {
                if (Player.Hand[i] != 0) {
                        sf::RectangleShape Card(sf::Vector2f(80.0f, 128.0f));

                        std::string nametext = function_cardTexture(Player.Hand[i]);
                                std::cout << nametext;
                        sf::Texture texture = Vars[nametext].Art;
                       
                        Card.setTexture(&texture);
                        window.draw(Card);
                }
 
Title: Re: Std::Map texture not wokring
Post by: eXpl0it3r on June 29, 2018, 12:07:28 am
You're creating a local copy of the texture, use a reference instead.
Title: Re: Std::Map texture not wokring
Post by: tomvidm on July 17, 2018, 01:53:12 pm
Assuming the object Card is a Sprite, then what eXpl0it3r says is true.
sf::Sprites only stores a pointer to the sf::Texture instance, meaning that whenever the address of the sf::Texture instance changes. the sf::Sprite will not be able to access that texture again. You must ensure that the Texture is stored in an manner, such that the address won't change and that the sf::Texture is not a temporary that goes out of scope before a draw call.