Skip to content

Commit

Permalink
Basic conversion of Tiled files to bitmaps.
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisTheBrave committed Oct 22, 2018
1 parent bbcb505 commit 9327c97
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
16 changes: 16 additions & 0 deletions DeBroglie.Console/BitmapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,21 @@ public static Color ColorAverage(IEnumerable<Color> colors)
}
return Color.FromArgb(alpha / n, red / n, green / n, blue / n);
}

public static Bitmap Slice(Bitmap b, int x, int y, int width, int height)
{
var newImage = new Bitmap(width, height);
Blit(newImage, b, 0, 0, x, y, width, height);
return newImage;
}

public static void Blit(Bitmap dest, Bitmap src, int destX, int destY, int srcX, int srcY, int width, int height)
{
var graphics = Graphics.FromImage(dest);
var size = new SizeF(width, height);
var dstRect = new RectangleF(new PointF(destX, destY), size);
var srcRect = new RectangleF(new PointF(srcX, srcY), size);
graphics.DrawImage(src, dstRect, srcRect, GraphicsUnit.Pixel);
}
}
}
31 changes: 31 additions & 0 deletions DeBroglie.Console/Export/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DeBroglie.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -14,6 +15,15 @@ public static class Exporter
public static void Export(TileModel model, TilePropagator tilePropagator, string filename, DeBroglieConfig config, ExportOptions exportOptions)
{
var exporter = GetExporter(filename);
// Handle conversions
if(exporter is BitmapExporter && exportOptions is TiledExportOptions)
{
if (tilePropagator.Topology.Directions.Type != Topo.DirectionsType.Cartesian2d)
throw new NotSupportedException("Converting from Tiled format to bitmaps only supported for square grids.");

exportOptions = ConvertToBitmaps(exportOptions as TiledExportOptions);
}

exporter.Export(model, tilePropagator, filename, config, exportOptions);
}

Expand Down Expand Up @@ -49,5 +59,26 @@ private static IExporter GetExporter(string filename)
}
}

private static BitmapSetExportOptions ConvertToBitmaps(TiledExportOptions teo)
{
var bseo = new BitmapSetExportOptions();
bseo.TileWidth = teo.Template.CellWidth;
bseo.TileHeight = teo.Template.CellHeight;
bseo.Bitmaps = new Dictionary<Tile, Bitmap>();
var basePath = Path.GetDirectoryName(teo.SrcFileName);
foreach (var tileset in teo.Template.Tilesets)
{
var tilesetBitmap = new Bitmap(Path.Combine(basePath, tileset.ImagePath));
for(var i=0;i<tileset.TileCount;i++)
{
var gid = i + tileset.FirstGid;
var tile = tileset[gid];
var tileBitmap = BitmapUtils.Slice(tilesetBitmap, tile.Left, tile.Top, tile.Width, tile.Height);
bseo.Bitmaps[new Tile(gid)] = tileBitmap;
}
}
return bseo;
}

}
}
2 changes: 1 addition & 1 deletion docs/articles/config_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Not all formats can be converted between. You must pick file extensions for
<tr><td></td><td>Bitmap (.png)</td><td>Tiled Map(.tmx)</td><td>Tiled Tileset (.tsx)</td><td>MagicaVoxel (.vox)</td><td>File set of Bitmap (.png)</td><td>File set of MagicaVoxel (.vox)</td></tr>
<tr><td>Outputs</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>.csv</td><td>&#x2713;</td><td>&#x2713;</td><td>&#x2713;</td><td>&#x2713;</td><td>&#x2713;</td><td>&#x2713;</td></tr>
<tr><td>.png</td><td>&#x2713;</td><td></td><td></td><td></td><td>&#x2713;</td><td></td></tr>
<tr><td>.png</td><td>&#x2713;</td><td>&#x2713;</td><td>&#x2713;</td><td></td><td>&#x2713;</td><td></td></tr>
<tr><td>.tmx</td><td></td><td>&#x2713;</td><td>&#x2713;</td><td></td><td></td><td></td></tr>
<tr><td>.vox</td><td></td><td></td><td></td><td>&#x2713;</td><td></td><td>&#x2713;</td></tr>
</table>
Expand Down

0 comments on commit 9327c97

Please sign in to comment.