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

Author Topic: Std::Map texture not wokring  (Read 1489 times)

0 Members and 1 Guest are viewing this topic.

OriginalBrand

  • Newbie
  • *
  • Posts: 1
    • View Profile
Std::Map texture not wokring
« 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);
                }
 
« Last Edit: June 28, 2018, 10:39:31 pm by OriginalBrand »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Std::Map texture not wokring
« Reply #1 on: June 29, 2018, 12:07:28 am »
You're creating a local copy of the texture, use a reference instead.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tomvidm

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Std::Map texture not wokring
« Reply #2 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.