Advertisement Jump to content
Sign in to follow this  

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

This topic is 1807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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

Share this post

Link to post
Share on other sites

tileX = Gid % mapwidth;

tileY = Gid / mapwidth;


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

Edited by CoreLactose

Share this post

Link to post
Share on other sites

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 )
         renderQuad.w = clip->w;
         renderQuad.h = clip->h;

      //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.
                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() ) ;
  • 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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!