• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nazrim

Iterate over TileLayers(Vector) doesn't work

5 posts in this topic

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

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]
2

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.
2

Share this post


Link to post
Share on other sites
@rip-off: thank you, solved my problem... What a dumb mistake;)

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

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
1

Share this post


Link to post
Share on other sites
@Servant: Thank you, I am grateful for any additional information at any given time:)
0

Share this post


Link to post
Share on other sites

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  
Followers 0