Skip to content

Commit 651887a

Browse files
authored
Merge pull request #8 from eriscorp/map-rendering-fix
Map rendering fix
2 parents 421b073 + ed9f1e2 commit 651887a

File tree

6 files changed

+189
-108
lines changed

6 files changed

+189
-108
lines changed

DALib/DALib.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<!--suppress MsbuildTargetFrameworkTagInspection -->
44
<TargetFrameworks>net8.0</TargetFrameworks>
55
<PackageId>DALib</PackageId>
6-
<PackageVersion>0.1.0</PackageVersion>
6+
<PackageVersion>0.5.2</PackageVersion>
77
<PackageReadmeFile>README.md</PackageReadmeFile>
88
<PackageLicenseExpression>MIT</PackageLicenseExpression>
99
<PackageIcon>hybrasyl-512x512.png</PackageIcon>
1010
<ProjectUrl>https://github.com/eriscorp/dalib</ProjectUrl>
11-
<Version>0.1.0</Version>
11+
<Version>0.5.2</Version>
1212
<IncludeSymbols>true</IncludeSymbols>
1313
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
14-
<Authors>See CONTRIBUTORS.md</Authors>
14+
<Authors>Kyle Speck and contributors</Authors>
1515
<Copyright>See CONTRIBUTORS.md</Copyright>
1616
<RepositoryUrl>https://github.com/eriscorp/dalib</RepositoryUrl>
1717
<Tags>hybrasyl; dalib; epf; spf; hpf; doomvas;</Tags>
@@ -38,4 +38,4 @@
3838
<SupportedPlatform Include="macOS"/>
3939
<SupportedPlatform Include="Windows"/>
4040
</ItemGroup>
41-
</Project>
41+
</Project>

DALib/Data/MapFile.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System.IO;
2-
using System.Text;
3-
using DALib.Abstractions;
1+
using DALib.Abstractions;
42
using DALib.Drawing;
53
using DALib.Extensions;
4+
using System.IO;
5+
using System.Text;
66

77
namespace DALib.Data;
88

@@ -29,6 +29,9 @@ public sealed class MapFile(int width, int height) : ISavable
2929
private MapFile(Stream stream, int width, int height)
3030
: this(width, height)
3131
{
32+
if (stream.Length != width * height * 6)
33+
throw new InvalidDataException("Invalid map file");
34+
3235
using var reader = new BinaryReader(stream, Encoding.Default, true);
3336

3437
for (var y = 0; y < Height; ++y)

DALib/Drawing/Graphics.cs

Lines changed: 119 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using System.Text;
3-
using DALib.Data;
1+
using DALib.Data;
42
using DALib.Definitions;
53
using DALib.Extensions;
64
using DALib.Memory;
75
using DALib.Utility;
86
using SkiaSharp;
7+
using System;
8+
using System.Text;
99

1010
namespace DALib.Drawing;
1111

@@ -179,146 +179,171 @@ public static SKImage RenderImage(EfaFrame efa, EfaBlendingType efaBlendingType
179179
/// <param name="iaDat">
180180
/// The IA archive.
181181
/// </param>
182-
public static SKImage RenderMap(MapFile map, DataArchive seoDat, DataArchive iaDat)
182+
/// <param name="foregroundPadding">
183+
/// The amount of padding to add to the height of the file and beginning rendering position
184+
/// </param>
185+
/// <param name="cache">
186+
/// A <see cref="MapImageCache"/> that can be reused to share <see cref="SKImageCache{TKey}"/> caches between multiple map renderings.
187+
/// </param>
188+
189+
public static SKImage RenderMap(MapFile map, DataArchive seoDat, DataArchive iaDat, int foregroundPadding = 512,
190+
MapImageCache? cache = null, bool dotGuides = false)
183191
=> RenderMap(
184192
map,
185193
Tileset.FromArchive("tilea", seoDat),
186194
PaletteLookup.FromArchive("mpt", seoDat)
187-
.Freeze(),
195+
.Freeze(),
188196
PaletteLookup.FromArchive("stc", iaDat)
189-
.Freeze(),
190-
iaDat);
197+
.Freeze(),
198+
iaDat, foregroundPadding, cache, dotGuides);
191199

192200
/// <summary>
193201
/// Renders a MapFile, given already extracted information
194202
/// </summary>
195203
/// <param name="map">
196-
/// The MapFile to render
204+
/// The <see cref="MapFile"/> to render
197205
/// </param>
198206
/// <param name="tiles">
199-
/// A collection of background tiles
207+
/// A <see cref="Tileset"/> representing a collection of background tiles
200208
/// </param>
201209
/// <param name="bgPaletteLookup">
202-
/// PaletteLookup for background tiles
210+
/// <see cref="PaletteLookup"/> for background tiles
203211
/// </param>
204212
/// <param name="fgPaletteLookup">
205-
/// PaletteLookup for foreground tiles
213+
/// <see cref="PaletteLookup"/> for foreground tiles
206214
/// </param>
207215
/// <param name="iaDat">
208-
/// IA archive for reading foreground tile files
216+
/// IA <see cref="DataArchive"/> for reading foreground tile files
217+
/// </param>
218+
/// <param name="foregroundPadding">
219+
/// The amount of padding to add to the height of the file and beginning rendering position
220+
/// </param>
221+
/// <param name="cache">
222+
/// A <see cref="MapImageCache"/> that can be reused to share <see cref="SKImageCache{TKey}"/> caches between multiple map renderings.
209223
/// </param>
210224
public static SKImage RenderMap(
211225
MapFile map,
212226
Tileset tiles,
213227
PaletteLookup bgPaletteLookup,
214228
PaletteLookup fgPaletteLookup,
215-
DataArchive iaDat)
229+
DataArchive iaDat,
230+
int foregroundPadding = 512,
231+
MapImageCache? cache = null)
216232
{
217-
const int FOREGROUND_PADDING = 512;
233+
var dispose = cache is null;
234+
cache ??= new MapImageCache();
218235

219236
//create lookups so we only render each tile piece once
220237
using var bgCache = new SKImageCache<int>();
221238
using var lfgCache = new SKImageCache<int>();
222239
using var rfgCache = new SKImageCache<int>();
223240

224-
//calculate width and height based on isometric view
225-
var width = map.Width * CONSTANTS.TILE_WIDTH;
226-
var height = map.Height * (CONSTANTS.TILE_HEIGHT + 1) + FOREGROUND_PADDING;
241+
//calculate width and height
242+
var width = (map.Width + map.Height + 1) * CONSTANTS.HALF_TILE_WIDTH;
243+
var height = (map.Width + map.Height +1 ) * CONSTANTS.HALF_TILE_HEIGHT + foregroundPadding;
227244
using var bitmap = new SKBitmap(width, height);
228245
using var canvas = new SKCanvas(bitmap);
229246

230247
//the first tile drawn is the center tile at the top (0, 0)
231-
var bgInitialDrawX = width / 2 - CONSTANTS.HALF_TILE_WIDTH;
232-
var bgInitialDrawY = FOREGROUND_PADDING;
233-
234-
//render background tiles and draw them to the canvas
235-
for (var y = 0; y < map.Height; y++)
248+
var bgInitialDrawX = map.Height - 1 * CONSTANTS.HALF_TILE_WIDTH;
249+
var bgInitialDrawY = foregroundPadding;
250+
try
236251
{
237-
for (var x = 0; x < map.Width; x++)
252+
//render background tiles and draw them to the canvas
253+
for (var y = 0; y < map.Height; y++)
238254
{
239-
var bgIndex = map.Tiles[x, y].Background;
255+
for (var x = 0; x < map.Width; x++)
256+
{
257+
var bgIndex = map.Tiles[x, y].Background;
240258

241-
if (bgIndex > 0)
242-
--bgIndex;
259+
if (bgIndex > 0)
260+
--bgIndex;
243261

244-
var bgImage = bgCache.GetOrCreate(
245-
bgIndex,
246-
index =>
247-
{
248-
var palette = bgPaletteLookup.GetPaletteForId(index + 2);
262+
var bgImage = cache.BackgroundCache.GetOrCreate(
263+
bgIndex,
264+
index =>
265+
{
266+
var palette = bgPaletteLookup.GetPaletteForId(index + 2);
249267

250-
return RenderTile(tiles[index], palette);
251-
});
268+
return RenderTile(tiles[index], palette);
269+
});
252270

253-
//for each X axis iteration, we want to move the draw position half a tile to the right and down from the initial draw position
254-
var drawX = bgInitialDrawX + x * CONSTANTS.HALF_TILE_WIDTH;
255-
var drawY = bgInitialDrawY + x * CONSTANTS.HALF_TILE_HEIGHT;
271+
//for each X axis iteration, we want to move the draw position half a tile to the right and down from the initial draw position
272+
var drawX = bgInitialDrawX + x * CONSTANTS.HALF_TILE_WIDTH;
273+
var drawY = bgInitialDrawY + x * CONSTANTS.HALF_TILE_HEIGHT;
274+
canvas.DrawImage(bgImage, drawX, drawY);
275+
}
256276

257-
canvas.DrawImage(bgImage, drawX, drawY);
277+
//for each Y axis iteration, we want to move the draw position half a tile to the left and down from the initial draw position
278+
bgInitialDrawX -= CONSTANTS.HALF_TILE_WIDTH;
279+
bgInitialDrawY += CONSTANTS.HALF_TILE_HEIGHT;
258280
}
259281

260-
//for each Y axis iteration, we want to move the draw position half a tile to the left and down from the initial draw position
261-
bgInitialDrawX -= CONSTANTS.HALF_TILE_WIDTH;
262-
bgInitialDrawY += CONSTANTS.HALF_TILE_HEIGHT;
263-
}
264-
265-
//render left and right foreground tiles and draw them to the canvas
266-
var fgInitialDrawX = width / 2 - CONSTANTS.HALF_TILE_WIDTH;
267-
var fgInitialDrawY = FOREGROUND_PADDING;
282+
//render left and right foreground tiles and draw them to the canvas
283+
var fgInitialDrawX = map.Height * CONSTANTS.HALF_TILE_WIDTH;
284+
var fgInitialDrawY = foregroundPadding;
268285

269-
for (var y = 0; y < map.Height; y++)
270-
{
271-
for (var x = 0; x < map.Width; x++)
286+
for (var y = 0; y < map.Height; y++)
272287
{
273-
var tile = map.Tiles[x, y];
274-
var lfgIndex = tile.LeftForeground;
275-
var rfgIndex = tile.RightForeground;
276-
277-
//render left foreground
278-
var lfgImage = lfgCache.GetOrCreate(
279-
lfgIndex,
280-
index =>
281-
{
282-
var hpf = HpfFile.FromArchive($"stc{index:D5}.hpf", iaDat);
283-
var palette = fgPaletteLookup.GetPaletteForId(index + 1);
284-
285-
return RenderImage(hpf, palette);
286-
});
287-
288-
//for each X axis iteration, we want to move the draw position half a tile to the right and down from the initial draw position
289-
var lfgDrawX = fgInitialDrawX + x * CONSTANTS.HALF_TILE_WIDTH;
290-
291-
var lfgDrawY = fgInitialDrawY + (x + 1) * CONSTANTS.HALF_TILE_HEIGHT - lfgImage.Height + CONSTANTS.HALF_TILE_HEIGHT;
292-
293-
if ((lfgIndex % 10000) > 1)
294-
canvas.DrawImage(lfgImage, lfgDrawX, lfgDrawY);
295-
296-
//render right foreground
297-
var rfgImage = rfgCache.GetOrCreate(
298-
rfgIndex,
299-
index =>
300-
{
301-
var hpf = HpfFile.FromArchive($"stc{index:D5}.hpf", iaDat);
302-
var palette = fgPaletteLookup.GetPaletteForId(index + 1);
303-
304-
return RenderImage(hpf, palette);
305-
});
306-
307-
//for each X axis iteration, we want to move the draw position half a tile to the right and down from the initial draw position
308-
var rfgDrawX = fgInitialDrawX + (x + 1) * CONSTANTS.HALF_TILE_WIDTH;
309-
310-
var rfgDrawY = fgInitialDrawY + (x + 1) * CONSTANTS.HALF_TILE_HEIGHT - rfgImage.Height + CONSTANTS.HALF_TILE_HEIGHT;
311-
312-
if ((rfgIndex % 10000) > 1)
313-
canvas.DrawImage(rfgImage, rfgDrawX, rfgDrawY);
288+
for (var x = 0; x < map.Width; x++)
289+
{
290+
var tile = map.Tiles[x, y];
291+
var lfgIndex = tile.LeftForeground;
292+
var rfgIndex = tile.RightForeground;
293+
294+
//render left foreground
295+
var lfgImage = cache.LeftForegroundCache.GetOrCreate(
296+
lfgIndex,
297+
index =>
298+
{
299+
var hpf = HpfFile.FromArchive($"stc{index:D5}.hpf", iaDat);
300+
var palette = fgPaletteLookup.GetPaletteForId(index + 1);
301+
302+
return RenderImage(hpf, palette);
303+
});
304+
305+
//for each X axis iteration, we want to move the draw position half a tile to the right and down from the initial draw position
306+
var lfgDrawX = fgInitialDrawX + x * CONSTANTS.HALF_TILE_WIDTH;
307+
308+
var lfgDrawY = fgInitialDrawY + (x + 1) * CONSTANTS.HALF_TILE_HEIGHT - lfgImage.Height +
309+
CONSTANTS.HALF_TILE_HEIGHT;
310+
311+
if ((lfgIndex % 10000) > 1)
312+
canvas.DrawImage(lfgImage, lfgDrawX, lfgDrawY);
313+
314+
//render right foreground
315+
var rfgImage = cache.RightForegroundCache.GetOrCreate(
316+
rfgIndex,
317+
index =>
318+
{
319+
var hpf = HpfFile.FromArchive($"stc{index:D5}.hpf", iaDat);
320+
var palette = fgPaletteLookup.GetPaletteForId(index + 1);
321+
322+
return RenderImage(hpf, palette);
323+
});
324+
325+
//for each X axis iteration, we want to move the draw position half a tile to the right and down from the initial draw position
326+
var rfgDrawX = fgInitialDrawX + (x + 1) * CONSTANTS.HALF_TILE_WIDTH;
327+
328+
var rfgDrawY = fgInitialDrawY + (x + 1) * CONSTANTS.HALF_TILE_HEIGHT - rfgImage.Height +
329+
CONSTANTS.HALF_TILE_HEIGHT;
330+
331+
if ((rfgIndex % 10000) > 1)
332+
canvas.DrawImage(rfgImage, rfgDrawX, rfgDrawY);
333+
}
334+
335+
//for each Y axis iteration, we want to move the draw position half a tile to the left and down from the initial draw position
336+
fgInitialDrawX -= CONSTANTS.HALF_TILE_WIDTH;
337+
fgInitialDrawY += CONSTANTS.HALF_TILE_HEIGHT;
314338
}
315-
316-
//for each Y axis iteration, we want to move the draw position half a tile to the left and down from the initial draw position
317-
fgInitialDrawX -= CONSTANTS.HALF_TILE_WIDTH;
318-
fgInitialDrawY += CONSTANTS.HALF_TILE_HEIGHT;
339+
return SKImage.FromBitmap(bitmap);
340+
}
341+
finally
342+
{
343+
if (dispose)
344+
cache.Dispose();
319345
}
320346

321-
return SKImage.FromBitmap(bitmap);
322347
}
323348

324349
/// <summary>

DALib/Drawing/Tileset.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System.Collections.Generic;
2-
using System.Collections.ObjectModel;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Text;
6-
using DALib.Abstractions;
1+
using DALib.Abstractions;
72
using DALib.Data;
83
using DALib.Definitions;
94
using DALib.Extensions;
105
using DALib.Utility;
116
using SkiaSharp;
7+
using System.Collections.Generic;
8+
using System.Collections.ObjectModel;
9+
using System.IO;
10+
using System.Linq;
11+
using System.Text;
1212

1313
namespace DALib.Drawing;
1414

0 commit comments

Comments
 (0)