Jump to content

View more

Image of the Day

Full Syncs #screenshotsaturday favourites https://t.co/i1Flnwcg3l #xbox #ps4 https://t.co/m0v2F1SxGs
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.


Sign up now

2d tile rendering using tmx-parser and SDL

2: Adsense

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.


  • You cannot reply to this topic
1 reply to this topic

#1 Kpaiva   Members   

100
Like
0Likes
Like

Posted 27 January 2012 - 09:05 PM

I'm working on getting a map created in Tiled (http://mapeditor.org) to render after parsing the map with tmx-parser (http://code.google.com/p/tmx-parser/). I've got the tiles to render in the correct positions, but i can't seem to get it to render the correct tiles from the tileset. I'm using the isometric_grass_and_water example from tiled to test it.

Can anyone tell me what i'm doing wrong?

void Map::RenderMapIsometric(SDL_Surface *SurfaceDest)
    {
	    for (int i = 0; i < map->GetNumLayers(); ++i)
	    {
		    // Get a layer.
	    this->layer = map->GetLayer(i);
	    for (int x = 0; x < layer->GetWidth(); ++x)
	    {
		    for (int y = 0; y < layer->GetHeight(); ++y)
		    {
			    int CurTile = layer->GetTileGid(x, y);
			    if(CurTile == 0)
			    {
				    continue;
			    }
			    int tileset_col = (CurTile % (TilesetWidth / this->tileset->GetTileWidth()));
			    int tileset_row = (CurTile / (TilesetWidth / this->tileset->GetTileWidth()));
			    std::cout << CurTile << std::endl;
			    SDL_Rect rect_CurTile;
			    rect_CurTile.x = (this->tileset->GetMargin() + (this->tileset->GetTileWidth() + this->tileset->GetSpacing()) * tileset_col);
			    rect_CurTile.y = (this->tileset->GetMargin() + (this->tileset->GetTileHeight() + this->tileset->GetSpacing()) * tileset_row);
			    rect_CurTile.w = this->tileset->GetTileWidth();
			    rect_CurTile.h = this->tileset->GetTileHeight();
			    int DrawX = (x * this->tileset->GetTileWidth() / 2) + (y * this->tileset->GetTileWidth() / 2);
			    int DrawY = (y * this->tileset->GetTileHeight() / 2) - (x * this->tileset->GetTileHeight() / 2);
			    apply_surfaceClip(DrawX, DrawY, surf_Tileset, SurfaceDest, &rect_CurTile);
		    }
	    }
    }
}


#2 Kpaiva   Members   

100
Like
0Likes
Like

Posted 28 January 2012 - 05:18 PM

I found the answer after some brute force here is the changed working code if anyone else needs it:
PS: Num_Of_Cols is the same thing as (TilesetWidth / TileWidth)

void Map::RenderMapIsometric(SDL_Surface *SurfaceDest)
{
   for (int i = 0; i < map->GetNumLayers(); ++i)
   {
	  // Get a layer.
	  this->layer = map->GetLayer(i);
	 
	  for (int x = 0; x < layer->GetWidth(); ++x)
	  {
		 for (int y = 0; y < layer->GetHeight(); ++y)
		 {
		    int CurTile = layer->GetTileGid(x, y);
		    if(CurTile == 0)
		    {
			   continue;
		    }
		    //CurTile = tileset->GetFirstGid() + CurTile;
		    CurTile--;
		   
		    int tileset_col = (CurTile % Num_Of_Cols);
		    int tileset_row = (CurTile / Num_Of_Cols);
		    SDL_Rect rect_CurTile;
		    rect_CurTile.x = (this->tileset->GetMargin() + (this->tileset->GetTileWidth() + this->tileset->GetSpacing()) * tileset_col);
		    rect_CurTile.y = (this->tileset->GetMargin() + (this->tileset->GetTileHeight() + this->tileset->GetSpacing()) * tileset_row);
		    rect_CurTile.w = this->tileset->GetTileWidth();
		    rect_CurTile.h = this->tileset->GetTileHeight();
		    int DrawX = (x * this->tileset->GetTileWidth() / 2) + (y * this->tileset->GetTileWidth() / 2);
		    int DrawY = (y * this->tileset->GetTileHeight() / 2) - (x * this->tileset->GetTileHeight() / 2);
		    apply_surfaceClip(DrawX, DrawY, surf_Tileset, SurfaceDest, &rect_CurTile);
		 }
	  }
   }
}





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.