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

Author Topic: having trouble using a returned sf::Sprite from an array element as a parameter  (Read 1677 times)

0 Members and 1 Guest are viewing this topic.

jbar98

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Hi, so I created a class called "Card" which holds a struct containing a sf::Sprite and a sf::Texture and also two variables which contain enum values.
The way I loaded the texture to the sf::Sprite is as follows:
First I made two enums to give each Card object a Typ and a Rank:
enum CardTyp
        {
                ZÖLD,
                TÖCK,
                MOCK,
                PIROS,
                MAX_TYPES
        };
        enum CardRank
        {
                DAME,
                KÖNIG,
                BUBE,
                SIEBEN,
                ACHT,
                NEUN,
                ZEHN,
                ASS,
                MAX_RANKS
        };


Then I made a switch on the m_typ variable to pick the correct texture:
switch (m_typ)
        {
        case ZÖLD:
        {
                switch (m_rank)
                {
                case CardRank::DAME:    m_texture.loadFromFile("kartenspiel/zöld/zöldBauer.jpg"); break;
                case CardRank::KÖNIG:  m_texture.loadFromFile("kartenspiel/zöld/zöldKönig.jpg"); break;
                case CardRank::BUBE:    m_texture.loadFromFile("kartenspiel/zöld/zöld2.jpg"); break;
                case CardRank::SIEBEN:  m_texture.loadFromFile("kartenspiel/zöld/zöld7.jpg"); break;
                case CardRank::ACHT:    m_texture.loadFromFile("kartenspiel/zöld/zöld8.jpg"); break;
                case CardRank::NEUN:    m_texture.loadFromFile("kartenspiel/zöld/zöld9.jpg"); break;
                case CardRank::ZEHN:    m_texture.loadFromFile("kartenspiel/zöld/zöld10.jpg"); break;
                case CardRank::ASS:             m_texture.loadFromFile("kartenspiel/zöld/zöldSpringer.jpg"); break;
                default:                                std::cout << "error in setTexture case zöld switch"; break;
                }
                break;
        }
}
(There are cases for the three other Card types but they basically are the same.)
And then I used this texture as a parameter for the setTexture Function:
m_graphicalCard.setTexture(m_texture); //where as m_graphicalCard is the sf::Sprite variable

That code seems to work because when I create a single Card object as follows:
Card card{ Card::TÖCK, Card::SIEBEN };
and then calling a member function from the card class, which returns the sprite variable and use it as a parameter for the
window.draw(card.getGraphicalCard());
function, im getting expected results.
But in my next class, the deck class which contains a std::vector out of Card objects, when calling the same memberfunction:
window.draw(deck.getLastCard().getGraphicalCard()); // getLastCard() is basically my own function for std::vector::back()
, the texture doesnt seem to get passed over properly, resulting in a blank rectangle. One of my first thoughts was that something went wrong when initializing the vector so that the elements didn't get the rights parameters for the card class constructor.
Here is the code of the class construtor:
Card::Card(const CardTyp &typ, const CardRank &rank)
        :m_typ{typ}, m_rank{rank}
{
        setTexture(); // my own function which initializes the object's typ&rank with the code above and initializes m_texture accordingly
        m_graphicalCard.setTexture(m_texture); //
}

Here the code of how I initialized the std::vector variable in my deck class:
for (int typ{ 0 }; typ < Card::MAX_TYPES; ++typ)
                for (int rank{ 0 }; rank < Card::MAX_RANKS; ++rank)
                {
                        m_deck.push_back(Card(static_cast<Card::CardTyp>(typ),static_cast<Card::CardRank>(rank)));
                        std::cout << temp << m_deck[temp++] << "\n";
                }
This code also seems to give the elements the correct types and ranks which i could tell by printing out all the elements. Now I really don't know what could be causing the error and i hope some of you guys can help me somehow :D. I hope that you can understand my code, im still really new to all of this and also im not writing in english that often so if you didn't understand something please ask and i will try to answer as fast as possible.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Cards should not own their textures. Make a texture holder class, load the textures you need into it (this way you won't have duplicates, another huge plus) and send the texture addresses to the cards. Do not put texture owners into vectors.

rjames

  • Newbie
  • *
  • Posts: 1
    • View Profile
I was having the same problem and ran across this: https://en.sfml-dev.org/forums/index.php?topic=17106.msg123000#msg123000
I think it will be helpful in explaining the problem. Thanks.

 

anything