/// <summary> /// Converts and adds the specified bitmap to the current tilemap, adding new tiles to the specified tileset. /// </summary> /// <param name="bmp">The bitmap to convert.</param> /// <param name="tileset">The tileset to use.</param> /// <param name="format">The tilemap format to use.</param> /// <param name="reduce">If true, reduces the tilemap; otherwise, does not reduce.</param> public void AddBitmap(Bitmap bmp, Tileset tileset, BitmapFormat format, bool reduce) { if (bmp == null) { throw new ArgumentNullException(nameof(bmp)); } if (tileset == null) { throw new ArgumentNullException(nameof(tileset)); } if (format == null) { throw new ArgumentNullException(nameof(format)); } TileCutter cutter = new TileCutter(tileset.TileWidth, tileset.TileHeight); foreach (Tile tile in cutter.CutTiles(bmp)) { // Find existing tileset entry if reducing. if (!reduce || !tileset.TryFindTileEntry(tile, out TileEntry te)) { // Otherwise, create a new tileset entry. te = tileset.AddTile(tile, format.CanFlipHorizontal, format.CanFlipVertical); } // Create new tilemap entry with original tile number. te = new TileEntry(te.TileNumber, te.HFlip, te.VFlip, tile.PaletteNumber); this.TileEntries.Add(te); } // Update the color format of the tileset tileset.ColorFormat = format.ColorFormat; tileset.IsIndexed = false; }
/// <summary> /// Creates a new empty tilemap with the given bitmap format. /// </summary> /// <param name="mapFmt">The bitmap format to use.</param> public Tilemap(BitmapFormat mapFmt) { if (mapFmt == null) { throw new ArgumentNullException(nameof(mapFmt)); } this.BitmapFormat = mapFmt; this.TileEntries = new List <TileEntry>(); }
public void AddBitmapIndexed(Bitmap bmp, Tileset tileset, PaletteSet palettes, BitmapFormat format, bool reduce) { if (bmp == null) { throw new ArgumentNullException(nameof(bmp)); } if (tileset == null) { throw new ArgumentNullException(nameof(tileset)); } if (palettes == null) { throw new ArgumentNullException(nameof(palettes)); } if (format == null) { throw new ArgumentNullException(nameof(format)); } TileCutter cutter = new TileCutter(tileset.TileWidth, tileset.TileHeight); Tile[] indexedTiles = new Tile[palettes.Count]; foreach (Tile tile in cutter.CutTiles(bmp)) { // Generate indexed tiles for each palette. int indexedCount = tile.GenerateIndexedTiles(palettes, indexedTiles); TileEntry?te = null; if (reduce) { // See if this tile can match an already indexed tile. for (int i = 0; i < indexedCount; i++) { Tile indexedTile = indexedTiles[i]; if (tileset.TryFindTileEntry(indexedTile, out TileEntry candidate)) { // Create new tilemap entry with original tile number and flips, but new palette. te = new TileEntry(candidate.TileNumber, candidate.HFlip, candidate.VFlip, indexedTile.PaletteNumber); break; } } } if (te == null && indexedCount > 0) { Tile indexedTile = indexedTiles[0].Clone(); TileEntry newEntry = tileset.AddTile(indexedTile, format.CanFlipHorizontal, format.CanFlipVertical); te = new TileEntry(newEntry.TileNumber, newEntry.HFlip, newEntry.VFlip, indexedTile.PaletteNumber); } if (te == null) { // Could not index tile. te = default(TileEntry); } this.Add((TileEntry)te); } // Update the color format of the tileset to the indexed format. tileset.ColorFormat = format.ColorFormat; tileset.IsIndexed = true; }