• Create Account

## [Solved] TMX: how to calculate the exact position within a tileset with the gid of a tile

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1exOfde  Members

836
Like
0Likes
Like

Posted 08 February 2014 - 06:20 AM

Hello,

Right at the moment i am trying to use the tmx format in my little project. Unfortunately I noticed at one point i start to struggle. I don't get how to calculate a position of an tile with a specific gid in a tileset.

The gid self is really simple. Gid= tileX + (mapwidth * tileY) //mapwidth in tiles

The point is if I have just the gid,mapwidth/height and tilewidth/height. With which way i can calculate tileX and tileY.
I'm sure the solution is quite simple and obvious because It should be just simple math. But i cannot get a clear thought right know and need some assistance

With Google I didn't found an explanation which is clear enough, yet. I hope some of you have an idea for me

"Hello World"

### #2Lactose!  GDNet+

9615
Like
1Likes
Like

Posted 08 February 2014 - 06:25 AM

tileX = Gid % mapwidth;

tileY = Gid / mapwidth;

---

EDIT: I think that's what you're asking, at least...

Edited by CoreLactose, 08 February 2014 - 06:27 AM.

Hello to all my stalkers.

### #3exOfde  Members

836
Like
0Likes
Like

Posted 08 February 2014 - 02:59 PM

jepp, that is it ... sometimes is it too easy to come up with right way. anyway. Here the Code To render a TMX map with SDL2.

bool inTileset(int tile_id, const Tmx::Tileset *tileset)
{
int tiles_x = (tileset->GetImage()->GetWidth() - tileset->GetMargin()) / (tileset->GetTileWidth() + tileset->GetSpacing());
int tiles_y = (tileset->GetImage()->GetHeight() - tileset->GetMargin()) / (tileset->GetTileHeight() + tileset->GetSpacing());
int total_tiles = tiles_x * tiles_y;

if((tile_id >= tileset->GetFirstGid()) && (tile_id <= tileset->GetFirstGid() + total_tiles)) {
return true;
}

return false;
}

void apply_renderer( SDL_Renderer* rend, int x, int y, SDL_Rect* clip,SDL_Texture* tex )
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, 0, 0 };

//Set clip rendering dimensions
if( clip != NULL )
{
}

//Render to screen
SDL_RenderCopy( rend, tex, clip, &renderQuad );
}

void Drawmap(SDL_Renderer* rend,SDL_Texture* tileset)
{
SDL_Rect        RectCurrent;
//Look into each Layer
for( int i = 0; i < Map->GetNumLayers() ;i++ )
{

const Tmx::Layer*           layer = Map->GetLayer(i);

//Look into each line of an specific Layer |Map->GetLayer(i)|
for(int y = 0; y < layer->GetHeight(); ++y)
{
//Check each tile of each line in each layer |Map->GetLayer(i)|
for(int x = 0; x < layer->GetWidth(); ++x)
{
int CurrentTile = layer->GetTileId(x,y);
//Check if the current tile exist within the tileset, if not restart loop.
if(!inTileset(CurrentTile,Map->GetTileset(0)))
continue;
CurrentTile -= Map->GetTileset(0)->GetFirstGid() -1;

//Calculate specific Position of the currentile in the Map
int tileset_col = (CurrentTile % (Map->GetTileset(0)->GetImage()->GetWidth() / Map->GetTileset(0)->GetTileWidth()));
int tileset_row = (CurrentTile / (Map->GetTileset(0)->GetImage()->GetWidth() / Map->GetTileset(0)->GetTileWidth()));

//Set the rect which will be used to clip out the specific tile from the tileset
RectCurrent.w = Map->GetTileset(0)->GetTileWidth();
RectCurrent.h = Map->GetTileset(0)->GetTileHeight();

//Position on the tileset
RectCurrent.y = (Map->GetTileset(0)->GetMargin() + (Map->GetTileset(0)->GetTileHeight() + Map->GetTileset(0)->GetSpacing()) * tileset_row);
RectCurrent.x = (Map->GetTileset(0)->GetMargin() + (Map->GetTileset(0)->GetTileWidth() + Map->GetTileset(0)->GetSpacing()) * tileset_col) ;

//Position on the Screen
int DrawX = (x * Map->GetTileset(0)->GetTileWidth() ) ;
int DrawY = (y * Map->GetTileset(0)->GetTileHeight() ) ;

apply_renderer(rend,DrawX,DrawY,&RectCurrent,tileset);

}
}
}

}

• Tmx::Map has to initlizied before and the *.tmx parsed before the function can work above.
• Short additional info this function works only with ONE tileset. anyone who wants use it and use more tilesets then one have to modife the function.

Edited by exOfde, 08 February 2014 - 03:29 PM.

"Hello World"

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.