Jump to content

  • Log In with Google      Sign In   
  • Create Account

Iterate over TileLayers(Vector) doesn't work


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Nazrim   Members   -  Reputation: 147

Like
0Likes
Like

Posted 28 July 2012 - 08:30 AM

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

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8224

Like
2Likes
Like

Posted 28 July 2012 - 10:19 AM

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


#3 SamiHuutoniemi   Members   -  Reputation: 259

Like
2Likes
Like

Posted 28 July 2012 - 10:19 AM

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.

#4 Nazrim   Members   -  Reputation: 147

Like
0Likes
Like

Posted 28 July 2012 - 10:34 AM

@rip-off: thank you, solved my problem... What a dumb mistake;)

@SamiHuutoniemi: No but next time I will try this, thank you;)

#5 Servant of the Lord   Crossbones+   -  Reputation: 19579

Like
1Likes
Like

Posted 28 July 2012 - 10:35 AM

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. Posted Image
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:] 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. Posted Image

Edited by Servant of the Lord, 28 July 2012 - 10:37 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#6 Nazrim   Members   -  Reputation: 147

Like
0Likes
Like

Posted 28 July 2012 - 03:59 PM

@Servant: Thank you, I am grateful for any additional information at any given time:)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS