Fixed atlas index

Index is always (gid-1), so the processing order of the tile sets does not matter.

The old logic with the tileidx counter assumed the tilesets in the correct order, starting with the tileset with firstgid=1.

Fixes  #1082