Hello, I followed this tutorial: http://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php and was able to draw a simple map. However, I can't figure out how to add a tile on top of another tile.
My tile sheet looks like this:
(http://i.imgur.com/XQMv5SF.png)
As you can see, some have a background while the others have a transparent one. If I draw the transparent background tiles next to one that isn't, it looks out of place:
(http://i.imgur.com/V3PNJxt.png)
So I wanted to draw another tile underneath it; like grass or sand. Do I have to make 2 arrays? I read my array from a XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Level>
<!-- Location of tile sheet -->
<Tileset>Resources/Tiles/medieval.png</Tileset>
<!-- Number of rows and columns on sheet -->
<SheetRows>7</SheetRows>
<SheetColumns>18</SheetColumns>
<!-- Size of the world -->
<WorldWidth>16</WorldWidth>
<WorldHeight>10</WorldHeight>
<!-- Offsets -->
<TopOffset>0</TopOffset>
<BottomOffset>32</BottomOffset>
<LeftOffset>0</LeftOffset>
<RightOffset>32</RightOffset>
<!-- Tiles from sheet -->
<Map>
<Row>75 4 75 1 1 1 0 0 1 0 0 0 0 0 0 0</Row>
<Row>0 4 75 1 0 0 0 0 0 0 44 0 1 57 0 0</Row>
<Row>0 4 0 75 0 0 0 1 1 0 4 0 0 0 0 0</Row>
<Row>0 40 24 72 0 0 0 0 0 0 4 0 0 0 0 0</Row>
<Row>0 0 0 1 0 0 0 0 0 57 4 0 1 0 0 0</Row>
<Row>0 0 0 0 0 0 0 1 1 0 4 0 0 0 0 0</Row>
<Row>0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0</Row>
<Row>0 0 0 1 0 0 57 0 0 0 4 0 1 0 0 0</Row>
<Row>0 0 0 1 0 0 57 0 0 0 4 0 1 0 0 0</Row>
<Row>0 0 0 0 0 0 0 1 1 0 4 0 0 0 0 0</Row>
</Map>
</Level>
Here's my TileMap.cpp code: https://gist.github.com/anonymous/f9ddbf24f684d0552d7fee31f4063155
What's the best way to achieve multiple layers? Any help would be appreciated!
Rather than having:
Tile tiles[width][height];
You could have something like:
vector< vector< list<Tile> > > tiles;
Then each position could store an ordered list of the tiles to be drawn at given position. Let's say you separate them with ";" in your xml:
<Map>
<Row>75;1 4 75 1 1 1 0 0 1 0 0 0 0 0 0 0</Row>
<Row>0 1;4 75 1 0 0 0 0 0 0 44 0 1 57 0 0</Row>
<Row>0 1;4 0 75 0 0 0 1 1 0 4 0 0 0 0 0</Row>
<Row>0 40 24 1;72 0 0 0 0 0 0 4 0 0 0 0 0</Row>
<Row>0 0 0 1 0 0 0 0 0 1;57 4 0 1 0 0 0</Row>
<Row>0 0 0 0 0 0 0 1 1 0 4 0 0 0 0 0</Row>
<Row>0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0</Row>
<Row>0 0 0 1 0 0 57 0 0 0 4 0 1 0 0 0</Row>
<Row>0 0 0 1 0 0 57 0 0 0 4 0 1 0 0 0</Row>
<Row>0 0 0 0 0 0 0 1 1 0 4 0 0 0 0 0</Row>
</Map>
And drawing is easy enough:
for (int i = 0; i < width; i++){
for (int j = 0; j < height; j++){
for (auto& tile : tiles[i][j]){
window.draw(tile);
}
}
}