Skip to content

Commit

Permalink
Fix for map screens crashing the game if they are "background-only"
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-zhao committed May 24, 2022
1 parent 71b340b commit 08d357d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion editor/Cartridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions editor/GameObjects/TileMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
{
Expand Down Expand Up @@ -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<byte> GetData()
{
if (!_data.Any(x => x > 0xff))
if (!HasForeground)
{
// Single tilemap mode
var tileMap = Compression.CompressRle(_data.Select(x => (byte)x).ToArray());
Expand Down

0 comments on commit 08d357d

Please sign in to comment.