• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
exOfde

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

2 posts in this topic

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

0

Share this post


Link to post
Share on other sites

tileX = Gid % mapwidth;

tileY = Gid / mapwidth;

---

EDIT: I [b]think[/b] that's what you're asking, at least...

Edited by CoreLactose
1

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.
                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);
            
            }
        }
    }
    
}
  • http://code.google.com/p/tmx-parser/
  • 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
0

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  
Followers 0