From 08d357d5cefa8142acfe42b8bd34de2092b34371 Mon Sep 17 00:00:00 2001 From: Maxim Date: Tue, 24 May 2022 17:39:08 +0100 Subject: [PATCH] Fix for map screens crashing the game if they are "background-only" --- editor/Cartridge.cs | 2 +- editor/GameObjects/TileMap.cs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/editor/Cartridge.cs b/editor/Cartridge.cs index ca7ae33..54780e5 100644 --- a/editor/Cartridge.cs +++ b/editor/Cartridge.cs @@ -1701,7 +1701,7 @@ private void ReadAssets() { // We assume these are set second so we have to check if it's a set or overlay var tileMap = new TileMap(Memory, offset, asset.GetLength(Memory)); - if (tileMap.IsOverlay() && item.TileMap != null) + if (item.TileMap != null && item.TileMap.HasForeground) { item.TileMap.OverlayWith(tileMap); _assetsLookup[asset] = item.TileMap; // Point at the same object for both diff --git a/editor/GameObjects/TileMap.cs b/editor/GameObjects/TileMap.cs index 97a009b..e8e5906 100644 --- a/editor/GameObjects/TileMap.cs +++ b/editor/GameObjects/TileMap.cs @@ -25,10 +25,16 @@ public void SetAllForeground() { _data[i] |= 0x1000; } + HasForeground = true; } public void OverlayWith(TileMap other) { + // Extend data if smaller (should not happen) + if (other._data.Count > _data.Count) + { + _data.AddRange(Enumerable.Repeat((ushort)0, other._data.Count - _data.Count)); + } for (var i = 0; i < _data.Count; i++) { if (other._data[i] != 0xff) @@ -40,9 +46,10 @@ public void OverlayWith(TileMap other) public Bitmap GetImage(TileSet tileSet, Palette palette) { - var image = new Bitmap(256, 192, PixelFormat.Format8bppIndexed); - // We work in 8bpp again... - image.Palette = palette.ImagePalette; + var image = new Bitmap(256, 192, PixelFormat.Format8bppIndexed) + { + Palette = palette.ImagePalette + }; var data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); try { @@ -79,10 +86,11 @@ public Bitmap GetImage(TileSet tileSet, Palette palette) public int Offset { get; set; } public int ForegroundTileMapSize { get; private set; } public int BackgroundTileMapSize { get; private set; } + public bool HasForeground { get; private set; } public IList GetData() { - if (!_data.Any(x => x > 0xff)) + if (!HasForeground) { // Single tilemap mode var tileMap = Compression.CompressRle(_data.Select(x => (byte)x).ToArray());