Adam's Lair Forum
https://forum.adamslair.net:80/

[WIP] TiledTilemap plugin
https://forum.adamslair.net:80/viewtopic.php?f=18&t=624
Page 1 of 3

Author:  misthema [ 2015/11/20, 22:50 ]
Post subject:  [WIP] TiledTilemap plugin

Hey guys!

I've started creating a plugin that will support TileD tilemaps in your games! So far I get all the XML-data loaded - I think?

See the sources at GitHub!

Here's a boring glimpse of the progress:
http://puu.sh/lsDZB/633ef803de.png

...and ^that data "compiles" to this:
http://puu.sh/lvXAt/cba4d0bedb.png

The plugin will be supporting everything TileD has to offer in it's format.

Feature list:
  • Tilesets used in the tilemap are loaded automatically (must be loaded in the Project View)
  • Terrain types: TileD's custom terrain types with collision detection - you can determine what type of terrain your object is walking on!
  • Collision detection - plugin will build necessary shapes for the tilemap, including custom terrain tiles!
  • Fast rendering - plugin will use the low-level stuff for performance!
  • More to come...

Will update more soon!

Author:  RockyTV [ 2015/11/21, 04:35 ]
Post subject:  Re: [WIP] TiledTilemap plugin

Nice! I was doing the same too a few months ago, but I got stuck at the rendering part. If you need, here's my wip plugin.

Author:  misthema [ 2015/11/21, 09:57 ]
Post subject:  Re: [WIP] TiledTilemap plugin

RockyTV wrote:
Nice! I was doing the same too a few months ago, but I got stuck at the rendering part. If you need, here's my wip plugin.

Great! As far as I looked the code, it'll help me with the Editor-side stuff which I'm completely lost with... :D
Thanks!

Author:  misthema [ 2015/11/21, 11:58 ]
Post subject:  Re: [WIP] TiledTilemap plugin

Created a git repository for the sources! Link updated in the first post.
All ideas and comments about the code are welcome!

Author:  hsnabn [ 2015/11/21, 12:48 ]
Post subject:  Re: [WIP] TiledTilemap plugin

Good stuff you've got here. It's going to be useful, since I do use Tiled.

I'm especially interested in the final rendering code, as I've got this SVG rendering side project I'm trying to get to work.

Author:  misthema [ 2015/11/23, 19:16 ]
Post subject:  Re: [WIP] TiledTilemap plugin

hsnabn wrote:
Good stuff you've got here. It's going to be useful, since I do use Tiled.

Thanks! Good to hear. :)

hsnabn wrote:
I'm especially interested in the final rendering code, as I've got this SVG rendering side project I'm trying to get to work.

I'm going with the low-level stuff Duality has to offer. Here's what I have so far (it's working, but only shows white tiles hehe):
Code:
public void Draw( IDrawDevice device )
{
   if( GameObj == null || !_loaded || Layers == null || Layers.Count == 0 )
      return;
   
   Vector3 tempPos = GameObj.Transform.Pos;
   float tempScale = 1f;
   device.PreprocessCoords( ref tempPos, ref tempScale );
   
   int halfMapW = W / 2;
   int halfMapH = H / 2;
   
   var vertices = new VertexC1P3T2[4];
   
   for (var i = Layers.Values.GetEnumerator(); i.MoveNext();) {
      
      var layer = i.Current;
      
      if (!layer.Visible)
         continue;
      
      for (int y = 0; y < H; y++) {
         
         for (int x = 0; x < W; x++) {
            
            // Is renderable tile available?
            int gid = layer.GetTile(x, y);
            if (gid <= 0)
               continue;
            
            // Get the correct tileset for this GID
            var tileset = TiledTileset.FindByGID(gid);
            if (tileset == null)
               continue;
            
            
            
            int tileX = (gid - tileset.FirstGID) % tileset.W;
            int tileY = (gid - tileset.FirstGID) / tileset.W;
            
            var uvRect = new Rect((tileX * TileW) / tileset.WPixel, (tileY * TileH) / tileset.HPixel, (tileX * TileW + TileW) / tileset.WPixel, (tileY * TileH + TileH) / tileset.HPixel);
            
            int posX = (x - halfMapW) * TileW;
            int posY = (y - halfMapH) * TileH;
            
            // Top-left
            vertices[0] = new VertexC1P3T2();
            vertices[0].Pos.X = posX - (TileW / 2);
            vertices[0].Pos.Y = posY - (TileH / 2);
            vertices[0].Pos.Z = tempPos.Z;
            vertices[0].TexCoord = uvRect.TopLeft;
            vertices[0].Color = ColorRgba.White;
            
            // Top-right
            vertices[1] = new VertexC1P3T2();
            vertices[1].Pos.X = posX + (TileW / 2);
            vertices[1].Pos.Y = posY - (TileH / 2);
            vertices[1].Pos.Z = tempPos.Z;
            vertices[1].TexCoord = uvRect.TopRight;
            vertices[1].Color = ColorRgba.White;
            
            // Bottom-right
            vertices[2] = new VertexC1P3T2();
            vertices[2].Pos.X = posX + (TileW / 2);
            vertices[2].Pos.Y = posY + (TileH / 2);
            vertices[2].Pos.Z = tempPos.Z;
            vertices[2].TexCoord = uvRect.BottomRight;
            vertices[2].Color = ColorRgba.White;
            
            // Bottom-left
            vertices[3] = new VertexC1P3T2();
            vertices[3].Pos.X = posX - (TileW / 2);
            vertices[3].Pos.Y = posY + (TileH / 2);
            vertices[3].Pos.Z = tempPos.Z;
            vertices[3].TexCoord = uvRect.BottomLeft;
            vertices[3].Color = ColorRgba.White;
            
            device.AddVertices(tileset.Image, VertexMode.Quads, vertices);
         }
      }
   }
}

Not sure if I'm doing this correctly, but at least the tiles are all in place. Just need a little tweaking with the UV-coordinates... :) Also, there's some funny bugs in it. :D
http://puu.sh/lvWZJ/f4d9b2c843.png

Also updated an image link to the first post about the data it stores atm!

Author:  hsnabn [ 2015/11/23, 23:17 ]
Post subject:  Re: [WIP] TiledTilemap plugin

Thanks for the code snippet: I'll try to check it out tomorrow, see if I can extract any information I can use for the SVG rendering project. It's kind of late right now.

Sorry, didn't mean to sidetrack this thread. As you were, folks.

Author:  misthema [ 2015/11/24, 19:03 ]
Post subject:  Re: [WIP] TiledTilemap plugin

Progress! I've been a bit dumb lately and just now figured out what's wrong with my rendering; the UV coordinates where like this:
Code:
var uvRect = new Rect(
    (tileX * TileW) / tileset.WPixel,
    (tileY * TileH) / tileset.HPixel,
    (tileX * TileW + TileW) / tileset.WPixel,
    (tileY * TileH + TileH) / tileset.HPixel
);


...when they should've just been like this:
Code:
var uvRect = new Rect(
    (tileX * TileW) / tileset.WPixel,
    (tileY * TileH) / tileset.HPixel,
    TileW / tileset.WPixel,
    TileH / tileset.HPixel
);


It's hard to remember the difference between a Rectangle and something else! :)

Here's where I stand at the moment:
http://puu.sh/lxayZ/016939fd5b.png

Author:  Adam [ 2015/11/24, 19:20 ]
Post subject:  Re: [WIP] TiledTilemap plugin

Looks neat! It's kind of nice to see you working on a very similar thing than I currently am, feels a bit like I have company working on this :D Maybe we'll even be able to inspire each other a bit when it comes to feature coverage and API design, so we both get the best of our implementation. Will keep an eye on your worklog.

Author:  misthema [ 2015/11/24, 20:20 ]
Post subject:  Re: [WIP] TiledTilemap plugin

Adam wrote:
Looks neat! It's kind of nice to see you working on a very similar thing than I currently am, feels a bit like I have company working on this :D Maybe we'll even be able to inspire each other a bit when it comes to feature coverage and API design, so we both get the best of our implementation. Will keep an eye on your worklog.

Thanks! I'd be more than happy to share my knowledge and vice versa. :)

Page 1 of 3 All times are UTC + 1 hour [ DST ]
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/