I'm having some difficulty applying a texture to a sprite array. I'm trying to apply the same texture to all, so that I can later setTextRect to decide which part of the texture is used as a tile in my game.
The declaration of the sprite array is in a different class and declared as follows:
sf::Sprite tileSprites[30][40]
Going line by line and debugging the stumbling block is the for loop. The response from the window is just to close and crash out with no errors.
The game crashes once the line where I try to apply the texture.
tileSprites[idx][idy].setTexture(tileMap);
std::cout << "Creating Map... \n";
// load the image to be used as map/the spritesheet.
if (!tileMap.loadFromFile("Data/Maps/tilemap.png"))
{
std::cout << "Tilemap PNG did not load";
}
//load the generated tilemap
if(!map.load("Data/Maps/test_map.tmx"))
{
std::cout << "TMX map file failed to load";
}
// access the layers in the map
const auto& layers = map.getLayers();
const auto layer = layers[0]->getLayerAs<tmx::TileLayer>();
const auto tiles = layer.getTiles();
int idx = 0;
int idy = 0;
for (int j = 0; j < tiles.size(); ++j)
{
idx = j / 30;
idy = j % 30;
tileSprites[idx][idy].setTexture(tileMap); // <-
}
std::cout << tiles.size();
}
Any advice would be really appreciated.