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

Author Topic: private member that is a texture isn't loading properly  (Read 1408 times)

0 Members and 1 Guest are viewing this topic.

PsuedoBlessing

  • Newbie
  • *
  • Posts: 3
    • View Profile
private member that is a texture isn't loading properly
« on: April 28, 2022, 06:42:46 am »
I'm trying to make a private member a texture and set it to that. My problem is that it won't get the texture from the files if it is in an if statement and when it's not in an if statement it won't assign the sprite's texture.

Here's the code for the constructor in Tile:

Tile::Tile()
{
        int RNG = rand() % 3 + 1; // randomizer for texture loading

        this->pX = this->pY = 0;


        if (RNG == 1)
        {
                // loads pattern 1
                this->tileTexture.loadFromFile("Sprites/Ground/Unrevealed/Unrevealed Tile 1.png");
                this->tileSprite.setTexture(this->tileTexture);
        }

        else if (RNG == 2)
        {
                // loads pattern 2
                this->tileTexture.loadFromFile("Sprites/Ground/Unrevealed/Unrevealed Tile 2.png");
                this->tileSprite.setTexture(this->tileTexture);
        }

        else
        {
                // loads pattern 3
                this->tileTexture.loadFromFile("Sprites/Ground/Unrevealed/Unrevealed Tile 3.png");
                this->tileSprite.setTexture(this->tileTexture);
        }
}
 

And here's how the class looks:


class Tile
{
public:
        // constructors
        Tile();
        ~Tile();

        // getters
                // data getters
        float getX();
        float getY();
        sf::Sprite getSprite();

                // list getters
        Tile* getNext();
        Tile* getPrev();
        int getListpos();

                //tile getters
        Tile* getLf();
        Tile* getRt();
        Tile* getUp();
        Tile* getDn();
        Tile* getClu();
        Tile* getCld();
        Tile* getCru();
        Tile* getCrd();


        //setters
                // data setters
        void setX(float newX);
        void setY(float newY);
        virtual void setSprite(sf::Texture& newTexture);
        void setSpritePos(float xPos, float yPos);

                // list setters
        void setNext(Tile* nextTile);
        void setPrev(Tile* prevTile);
        void setListpos(int Listpos);

                //tile setters
        void setLf(Tile* newLf);
        void setRt(Tile* newRt);
        void setUp(Tile* newUp);
        void setDn(Tile* newDn);
        void setClu(Tile* newClu);
        void setCld(Tile* newCld);
        void setCru(Tile* newCru);
        void setCrd(Tile* newCrd);

        //other functions
       

private:

        // data of the node
        float pX;
        float pY;
        sf::Sprite tileSprite;
        sf::Texture tileTexture;

        // postions of list
        Tile* tNext;
        Tile* tPrev;
        int Listpos;
       
        // postions of Tile
        Tile* Lf; // Left of the tile
        Tile* Rt; // right of the tile
        Tile* Up; // Up of the tile
        Tile* Dn; // down of the tile
        Tile* Clu; // conner left up of the tile
        Tile* Cld; // conner left down of the tile
        Tile* Cru; // conner right up of the tile
        Tile* Crd; // conner right down of the tile
};

 

What do I do and what is wrong?

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: private member that is a texture isn't loading properly
« Reply #1 on: April 28, 2022, 10:20:56 am »
On it's own that looks fine, assuming the working directory is correct when it's run.
But how are tiles being allocated and put in (what looks like) the linked list? Could you show an example of making a couple of tiles.

One semi-unrelated tip: textures only need to be loaded once for a whole program, you can reuse a texture for any number of sprites. If you have a texture in every tile, that texture has to be loaded and stored many times instead of just once.

PsuedoBlessing

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: private member that is a texture isn't loading properly
« Reply #2 on: April 29, 2022, 06:47:05 am »
Here's the class tile list that contains the head pointer, that links all of the nodes.


class TileList
{
public:
        TileList(Tile* newHead = nullptr);
        ~TileList();

        Tile* getHead();
        void setHead(Tile* newHead);

        void insertAtFront();
        void AssignTilePos();

private:
        Tile* pHead;
};

TileList::TileList(Tile* newHead)
{
        this->pHead = newHead;
}

TileList::~TileList()
{
        //Add deconstrcutor here
}

Tile* TileList::getHead()
{
        return this->pHead;
}

void TileList::setHead(Tile* newHead)
{
        this->pHead = newHead;
}

void TileList::insertAtFront()
{
        Tile* pMem = new Tile;

        if (pMem != nullptr)
        {
                if (this->pHead != nullptr)
                {
                        pMem->setNext(this->pHead);
                        this->pHead->setPrev(pMem);
                }

                this->pHead = pMem;
        }
}

void TileList::AssignTilePos()
{
// assigning the top
        if (this->pHead->getY() != 0) // if it doesn't equal zero then there is a tile above it
        {
               
        }

}

 

Here's the Minefield that calls upon it.

class Minefield
{
public:
        Minefield();
        ~Minefield();

        void displayBoard(sf::RenderWindow& window);

private:
        TileList boardList;
};

Minefield::Minefield()
{
        float rowSlot = 0, colSlot = 0;

        for (int index1 = 0; index1 < 10; ++index1)
        {
                for (int index2 = 0; index2 < 10; ++index2)
                {
                        this->boardList.insertAtFront();
                        this->boardList.getHead()->setX(colSlot);
                        this->boardList.getHead()->setY(rowSlot);
                        this->boardList.getHead()->setSpritePos(colSlot, rowSlot);


                        cout << "(" << colSlot << "," << rowSlot << ")" << endl;

                        colSlot = colSlot + 16;
                }

                colSlot = 0;
                rowSlot = rowSlot + 16;
        }
}

Minefield::~Minefield()
{

}

void Minefield::displayBoard(sf::RenderWindow& window)
{
        Tile* tileCur = this->boardList.getHead();

        while (tileCur != nullptr)
        {
                window.draw(tileCur->getSprite());
                tileCur = tileCur->getNext();
        }
}

 

It's now working sometimes, it sometimes crashes and sometimes doesn't...

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: private member that is a texture isn't loading properly
« Reply #3 on: April 29, 2022, 10:24:55 am »
It looks like the pointers (tNext, tPrev and the 8 surrounding tiles) aren't being initialised. Uninitialised pointers have random values (whatever was in memory already) or special debug sequences (like 0xcccccccccccccccc), that could cause it to think there are tiles when there really isn't.

PsuedoBlessing

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: private member that is a texture isn't loading properly
« Reply #4 on: April 29, 2022, 07:31:53 pm »
Do you think I should just initialize the pointers within the construction of Tile?