Much better now that I can see it with the right images
I'm going through your code (for fun
) and I'm noticing a few things here and there that should be fixed before it causes your problems later on.
Your grid iteration is mixed.
Grid.resize(GridSizeY);
for (int x = 0; x < GridSizeX; x++)//Column
{
for (int y = 0; y < GridSizeY; y++)//Row
{
Grid[y].push_back(blankGridSprite);
blankGridSprite.setPosition(x * 32, y * 32);
}
}
That makes no sense as you are resizing the grid to hold rows and then go through them as columns. Then, when you're iterating the "y", you're pushing the new y's based on the y. That was not a very good explanation of what I mean. However, in your code-style, I think it should be this:
Grid.resize(GridSizeX);
for (int x = 0; x < GridSizeX; x++)//Column
{
for (int y = 0; y < GridSizeY; y++)//Row
{
blankGridSprite.setPosition(x * 32, y * 32);
Grid[x].push_back(blankGridSprite);
}
}
You should replace all of your usage (you used it 6 times) of
rint (as I found that it's rounding to the nearest integer, which is upwards when it hits halfway) with
floor. Try it. Trust me
Cases in a switch statement don't need block braces. Just so you know
You should probably handle the failures when loading textures. Returning from the application seems to make sense since you can't use it without them.
Your outputting of row and column are incorrect. Row should be y and column should be x.
You mix up X and Y access again in the Grid vector here:
Grid[indexY][indexX].setTexture(tileTexture);
and here:
Grid[indexY][indexX].setTexture(blankGridTexture);
You are only testing to make sure that Y is within boundary (the bottom of the grid). You aren't testing for the top, or testing the x for the sides. Also, if y equals Grid.size(), it's accessing an element that is not there.
You are loading in your images and creating your textures and sprites on every cycle. Take them outside the main window loop and load them just once.
As for the problem you mentioned about it drawing on the bottom and missing the last tile off, it's because you're setting the position of the temporary variable
after assigning it to the grid, so each tile is getting the position of the previous one (the first two have the same position).
You'll need to swap them to this:
blankGridSprite.setPosition(x * 32, y * 32);
Grid[x].push_back(blankGridSprite);
and then remove the +1 from indexY.
After making these modifications myself, it works perfectly and never crashes