# Tilesets with two dimensions

I have done tiled maps and tile sets quite often, but in a rather simplified way. My tile sets were linear images of the size (tile_width * num_tiles) * tile_height, so I could find an index easily by using val src_pos = (index * tile_width, 0).

But in reality, I've come to notice, tile sets are actually fully sized images of two dimensions. That has some advantages, clearly, it's easier to draw. So I wanted to support that. The problem would be just to turn the index to the right source position. Failed a bit, so I wanted to ask here.

I've had two ideas dealing with it:

Turn the index into two values with

src_x = index modulo num_tiles_x
src_y = index div num_tiles_y


which lead me to some quite weird results. The tiles are correctly cut, but they're the wrong ones for the position.

The other idea is to take the image and turn it into a 1D-tile set by cutting it up. But I am afraid that would not be an option, as I am using Textures.

How is this solved in 'professional' applications?

The formula you are using to convert from a 1d position to a 2d position is almost correct (and I'm sure the same method is used professionally)

src_x = index modulo num_tiles_x

src_y = index div num_tiles_x

Where num_tiles_x is the number of tiles in a single row.

//used to find tile on sheet sourceY = (ScreenMap[count]/10)*60; sourceX = ((ScreenMap[count]%10)-1)*60;

my tiles are 60x60 pixel squares. ScreenMap[count] is an array of tiles but in your case it would be the number of the tile for example 14 for the fourth tile on the second row.

the only problem is you cannot read 1 or 10 due to a mathmatical error so you tile sheets are ten tiles wide but the first and last tile cannot be read so you only get eight per row.

this method works great and I have many tile sheets working in this way.

one other thing, you mentioned 'professional' so you might be interested in this. In commercial games a tile sheet is always limited to a maximum of 256 tiles per sheet, this is so each tile can be assigned a binary value.

hope this helps.

• Why do you think binary values are restricted to 256 possible values? Apr 8 '11 at 8:28
• basic 8 bit binary can have 256 values, this is the smallest and most efficient way of storing data. Apr 8 '11 at 9:28
• Yes, 8 bit means 256 different values, but you could just as well use 4 bit or 24 bit to encode tiles. So your statement about commercial games always being limited to 256 tiles is simply not true. Also your statement about 8bit being the smallest and most efficient way to store data seems a bit odd. What leads you to this conclusion? Apr 8 '11 at 11:05
• Conversation with a square-enix developed a few years back. Its just one of those random bits of information that gets stuck in your brain. Just thought it might interest him as it seemed relevant. Also I didn't mean they were limited I meant they limit them selves to 256 Apr 8 '11 at 11:11