Sign in to follow this  

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

Recommended Posts

exOfde    838


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
exOfde    838

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this