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?
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...