diff --git a/packages/wolf_3d_dart/lib/src/data/wl_parser.dart b/packages/wolf_3d_dart/lib/src/data/wl_parser.dart index 69f7b9f..41db34d 100644 --- a/packages/wolf_3d_dart/lib/src/data/wl_parser.dart +++ b/packages/wolf_3d_dart/lib/src/data/wl_parser.dart @@ -196,13 +196,25 @@ abstract class WLParser { // --- Private Helpers --- static Sprite _parseWallChunk(ByteData vswap, int offset) { - final pixels = Uint8List(64 * 64); - for (int x = 0; x < 64; x++) { - for (int y = 0; y < 64; y++) { - // Flat 1D index: x * 64 + y - pixels[x * 64 + y] = vswap.getUint8(offset + (x * 64) + y); - } + // --- SAFETY CHECK --- + // Prevent crashes from corrupted VSWAP offsets or dummy EOF chunks + if (offset + 4096 > vswap.lengthInBytes) { + // Return a blank transparent sprite if the data is invalid + return Sprite(Uint8List(4096)..fillRange(0, 4096, 255)); } + + // --- OPTIMIZATION --- + // Instead of looping 4,096 times, we slice the exact memory block instantly. + // Because Wolf3D walls are natively stored in column-major order on disk, + // they perfectly match our 1D Sprite array layout without any math! + final rawView = vswap.buffer.asUint8List( + vswap.offsetInBytes + offset, + 4096, + ); + + // Create a new Uint8List copy so we don't hold the entire file in memory + final pixels = Uint8List.fromList(rawView); + return Sprite(pixels); } @@ -225,7 +237,6 @@ abstract class WLParser { } for (int x = leftPix; x <= rightPix; x++) { - // REVERTED to your original, correct math! int colOffset = vswap.getUint16( offset + 4 + ((x - leftPix) * 2), Endian.little, @@ -249,7 +260,6 @@ abstract class WLParser { if (endY == 0) break; endY ~/= 2; - // THE FIX: This MUST be a signed integer (getInt16) to handle DOS wraparound int pixelOfs = vswap.getInt16(cmdOffset + 2, Endian.little); int startY = vswap.getUint16(cmdOffset + 4, Endian.little);