Sign in to follow this  

Iterate over TileLayers(Vector) doesn't work

This topic is 1962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[CODE]
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);
}
}
}
[/CODE]

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;).

Share this post


Link to post
Share on other sites
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:
[code]
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);
}
}
}
}
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.
[CODE]
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.
[/CODE]

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

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

But really, for compact loops like that, it's better to break things out clearly, like this:
[code]
std::vector<myTile> tiles = (*it)[X+(Y * MAP_WIDTH)];
std::vector<myTile>::iterator tmpIt;
tmpIt = tiles.begin();[/code]
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. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]
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.

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

void DrawLayer()
{
for(...every tile in layer...)
{
DrawTile(tile);
}
}[/code]

[b][Edit:][/b] [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. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] [/size] Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

This topic is 1962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this