How to achieve selection of a tile from a tile sheet based on an ID?

Posted by Bugster on Game Development See other posts from Game Development or by Bugster
Published on 2012-11-24T18:02:05Z Indexed on 2012/11/25 5:18 UTC
Read the original article Hit count: 255

Filed under:
|
|
|

Let's say I have a tile sheet that contains 8 sprites per sheet. Each sprite is a tile of 30x30.

I wrote my own custom map parser/map loader however I'm having trouble extracting a certain tile sprite from the file. I'll describe my problem better in order for everyone to understand.

I wrote an enum of materials, each material has a value according to it's location relative to the tile sheet. For example void is 1, grass is 2, rock is 3, etc. So in my tile sheet they are represented as such:

+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+

Which is equivalent to:

+------+-------+-------+
| void | grass | stone |
+------+-------+-------+

Basically when rendering, I created a tile class, each tile has 2 coordinates: X and Y (They are calculated automatically) and a material which can be represented either as a number, either as a value (ID). When rendering, I have a vector of sprites which are all taken from 1 file called tilesheet.png, however each of them must only draw a certain portion of the tile sheet, for example say I have something like this:

tile coordinateBounds(topLeftX, topLeftY, tileWidth, tileHeight); 

During the initialization of the map I calculate an array of tiles, and I give each of them their position, their materials based on the values in a map file and a few other variables such as collision. I need to apply the coordinateBounds to each of them according to their material value. For example if the material is grass it should only take the grass sprite from the tilesheet. I must also mention I'm using SFML, and there are no borders or spacing between the tiles.

© Game Development or respective owner

Related posts about c++

Related posts about tiles