Iterate over TileLayers(Vector) doesn't work

Started by
4 comments, last by Nazrim 11 years, 8 months ago
Hi guys,

I have a (in my opinion) strange problem:

I am working on a 2D TileMap with multiple layers. It needs multiple layers because a Terrian-Transmission is a smaller Tile, laying on top of a normal Tile.

The Map, is a Vector of Tile-Vectors.
A Tile is a set of 2 Integers: TileID is ID inside TileSet, TypeID is Tile Type in game (NORMAL/NONE/WALL).

Now this is the code of my render() function:


int TilesetWidth = Surf_Tileset->w / TILE_SIZE;

std::vector<std::vector<myTile> >::iterator it;
it = TileList.begin();

for (int Y = 0; Y < MAP_HEIGHT; Y++)
{
for (int X = 0; X < MAP_WIDTH; X++)
{
std::vector<myTile>::iterator tmpIt;
tmpIt = it[X+(Y * MAP_WIDTH)].begin();
int tX = MapX + (X * TILE_SIZE);
int tY = MapY + (Y * TILE_SIZE);
int TilesetX = (tmpIt->TileID % TilesetWidth) * TILE_SIZE;
int TilesetY = (tmpIt->TileID / TilesetWidth) * TILE_SIZE;
for (; tmpIt < it[X+(Y * MAP_WIDTH)].end(); tmpIt++)
{
if (tmpIt->TypeID == TILE_TYPE_NONE || tmpIt->TypeID == TILE_TYPE_ERROR)
{
continue;
}
applySurface (Surf_Display, Surf_Tileset, tX, tY, TilesetX, TilesetY, TILE_SIZE, TILE_SIZE);
}
}
}


It should iterate about every Tile-Vector, calculate the screen position, and blit all tiles inside the vector that are not of type ERROR or NONE.

But instead of this it shows the first tile of every vector, nothing else. (I can choose the layer being drawn by setting tmpIt = ...begin() + x; )

So my question is: why does my iterator not iterate?

I have no idea why this isn't working...

Thanks for every help;).
Advertisement
You are indexing into an iterator... which is very strange. Also I am suspicious of the code which determines TilesetX and TilesetY outside the loop, but depends on the value of the loop iterator.

I would write the code more like this:

int TilesetWidth = Surf_Tileset->w / TILE_SIZE;
for (int y = 0; y < MAP_HEIGHT; y++)
{
for (int x = 0; x < MAP_WIDTH; x++)
{
std::vector<myTile> &tiles = TileList[x + (y * MAP_WIDTH)]
int tileX = MapX + (x * TILE_SIZE);
int tileY = MapY + (y * TILE_SIZE);
for (std::vector<myTile>::iterator i = tiles.begin(); i < tiles.end(); i++)
{
int TilesetX = (i->TileID % TilesetWidth) * TILE_SIZE;
int TilesetY = (i->TileID / TilesetWidth) * TILE_SIZE;
if (i->TypeID != TILE_TYPE_NONE && i->TypeID != TILE_TYPE_ERROR)
{
applySurface (Surf_Display, Surf_Tileset, tileX, tileY, TilesetX, TilesetY, TILE_SIZE, TILE_SIZE);
}
}
}
}
Have you tried to set a breakpoint in your IDE, and step through the loops? That way you can easily see where things go wrong. Especially good while working on multidimensional sets of data, since it easily gets you confused otherwise.
@rip-off: thank you, solved my problem... What a dumb mistake;)

@SamiHuutoniemi: No but next time I will try this, thank you;)
Advice A) Learning to use a debugger, and using breakpoints and watch variables greatly aids in figuring out what is going wrong.
Advice B) If something isn't working, trying a simpler version of it, and build upwards. In this case, if "Blit every tile of every layer" isn't working, try a "Blit every tile of the first layer only" for testing.

std::iterator uses [] to support dereferencing. It looks like you are forgetting to de-reference it, and are trying to use it to select the layer or the tile.

std::vector<int> myVector;
std::vector<int>::iterator it = myVector.begin();

it[3] //Accesses the fourth element after whereever "it" is pointing to.
it++;
it++;
it[3] //Accesses the 6th element.


myVector.begin() = 0
++ = 1
++ = 2
[3] = 5 (2 + [3] = 5)

It should be:
std::vector<myTile>::iterator tmpIt;
tmpIt = (*it)[X+(Y * MAP_WIDTH)].begin(); //De-reference 'it' first!


But really, for compact loops like that, it's better to break things out clearly, like this:

std::vector<myTile> tiles = (*it)[X+(Y * MAP_WIDTH)];
std::vector<myTile>::iterator tmpIt;
tmpIt = tiles.begin();

This makes it easier to see and debug where the mistakes are.

However, "tmpIt" is a meaningless name. All your iterators are temp, and all of them are iterators, so "temporary iterator" doesn't describe it at all. You might as well call all your integers, "holdsANumberOfSomeKind", because neither is descriptive. wink.png
Better (more descriptive) names make it less likely for mistakes to occur, and easier to spot them when they do occur.

Furthermore, your loop is three loops deep, plus an embedded if() statement. This calls greatly for you to break out the function into more than one function for clarity, for ease-of-debugging, and for ease-of-reading.

void DrawArea()
{
for(...each layer in area...)
{
DrawLayer(layer);
}
}

void DrawLayer()
{
for(...every tile in layer...)
{
DrawTile(tile);
}
}


[Edit:] [size=2]Ninja-d by two people saying the exact thing I'm saying, and then the OP responding. Posted anyway (despite seeing the "2 people posted while you were commenting" popup), for additional comments and explanations - and because I took the time to type it all out, and am not letting it go to waste. tongue.png
@Servant: Thank you, I am grateful for any additional information at any given time:)

This topic is closed to new replies.

Advertisement