Fill once for future setTile calls of it:
void Tileset::setTile(int tileNum)
{
if(subRect.size()==0)
{
for(int h = 0; h < numTilesY; h++)
{
for(int w = 0; w < numTilesX; w++)
{
rect.left = w * tileWidth;
rect.top = h * tileHeight;
rect.width = tileWidth;
rect.height = tileHeight;
subRect.push_back(rect);
}
}
}
this->setTextureRect(subRect[tileNum]);
}
Using local variables:
void Tileset::setTile(int tileNum)
{
std::vector <sf::Rect<int>> subRect;
for(int h = 0; h < numTilesY; h++)
{
for(int w = 0; w < numTilesX; w++)
{
sf::Rect <int> rect;
rect.left = w * tileWidth;
rect.top = h * tileHeight;
rect.width = tileWidth;
rect.height = tileHeight;
subRect.push_back(rect);
}
}
this->setTextureRect(subRect[tileNum]);
}//all memory used by subRect gets released by its dtor
There is like bilion problems here: using sprites(classes derieved from it = even worse) for tiles is bad, you should use vertexarray instead. Why make entire array of all possible rects and not compute the tile rect directly like that(there might be error here, I wrote it very hastily):
void Tileset::setTile(int tileNum)
{
int x=tileNum%numTilesX;
int y=tileNum/numTilesX;
sf::Rect<int> rect;
rect.left=x*tileWidth;
rect.top=y*tileHeight;
rect.width=tileWidth;
rect.height=tileHeight;
this->setTextureRect(rect);
}