Starting to move data loader to its own package

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-14 19:34:39 +01:00
parent 052b5afd2e
commit f225ca728e
21 changed files with 146 additions and 39 deletions

7
packages/wolf_3d_data/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

View File

@@ -0,0 +1,3 @@
## 1.0.0
- Initial version.

View File

@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/tools/pub/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/to/develop-packages).
-->
TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
## Features
TODO: List what your package can do. Maybe include images, gifs, or videos.
## Getting started
TODO: List prerequisites and provide or point to information on how to
start using the package.
## Usage
TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.
```dart
const like = 'sample';
```
## Additional information
TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.

View File

@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.
include: package:lints/recommended.yaml
# Uncomment the following section to specify additional rules.
# linter:
# rules:
# - camel_case_types
# analyzer:
# exclude:
# - path/to/excluded/files/**
# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options

View File

@@ -0,0 +1,3 @@
import 'matrix.dart';
typedef Level = Matrix<int>;

View File

@@ -0,0 +1 @@
typedef Matrix<T> = List<List<T>>;

View File

@@ -0,0 +1,3 @@
import 'matrix.dart';
typedef Sprite = Matrix<int>;

View File

@@ -0,0 +1,142 @@
import 'dart:typed_data';
import 'classes/sprite.dart';
class VswapParser {
/// Extracts the 64x64 wall textures from VSWAP.WL1
static List<Sprite> parseWalls(ByteData vswap) {
// 1. Read Header
int chunks = vswap.getUint16(0, Endian.little);
int spriteStart = vswap.getUint16(2, Endian.little);
// int soundStart = vswap.getUint16(4, Endian.little); // We don't need this yet
// 2. Read Offsets (Where does each chunk start in the file?)
List<int> offsets = [];
for (int i = 0; i < spriteStart; i++) {
offsets.add(vswap.getUint32(6 + (i * 4), Endian.little));
}
// 3. Extract the Wall Textures
List<Sprite> textures = [];
// Walls are chunks 0 through (spriteStart - 1)
for (int i = 0; i < spriteStart; i++) {
int offset = offsets[i];
if (offset == 0) continue; // Empty chunk
// Walls are always exactly 64x64 pixels (4096 bytes)
// Note: Wolf3D stores pixels in COLUMN-MAJOR order (Top to bottom, then left to right)
Sprite texture = List.generate(64, (_) => List.filled(64, 0));
for (int x = 0; x < 64; x++) {
for (int y = 0; y < 64; y++) {
int byteIndex = offset + (x * 64) + y;
texture[x][y] = vswap.getUint8(byteIndex);
}
}
textures.add(texture);
}
return textures;
}
/// Extracts the compiled scaled sprites from VSWAP.WL1
static List<Sprite> parseSprites(ByteData vswap) {
int chunks = vswap.getUint16(0, Endian.little);
int spriteStart = vswap.getUint16(2, Endian.little);
int soundStart = vswap.getUint16(4, Endian.little);
List<int> offsets = [];
for (int i = 0; i < chunks; i++) {
offsets.add(vswap.getUint32(6 + (i * 4), Endian.little));
}
List<Sprite> sprites = [];
// Sprites are located between the walls and the sounds
for (int i = spriteStart; i < soundStart; i++) {
int offset = offsets[i];
if (offset == 0) continue; // Some chunks are empty placeholders
// Initialize the 64x64 grid with 255 (The Magenta Transparency Color!)
Sprite sprite = List.generate(64, (_) => List.filled(64, 255));
int leftPix = vswap.getUint16(offset, Endian.little);
int rightPix = vswap.getUint16(offset + 2, Endian.little);
// Read the offsets for each vertical column of the sprite
List<int> colOffsets = [];
for (int x = leftPix; x <= rightPix; x++) {
colOffsets.add(
vswap.getUint16(offset + 4 + ((x - leftPix) * 2), Endian.little),
);
}
for (int x = leftPix; x <= rightPix; x++) {
int colOffset = colOffsets[x - leftPix];
if (colOffset == 0) continue;
int cmdOffset = offset + colOffset;
// Execute the column drawing commands
while (true) {
int endY = vswap.getUint16(cmdOffset, Endian.little);
if (endY == 0) break; // 0 marks the end of the column
endY ~/= 2; // Wolf3D stores Y coordinates multiplied by 2
int pixelOfs = vswap.getUint16(cmdOffset + 2, Endian.little);
int startY = vswap.getUint16(cmdOffset + 4, Endian.little);
startY ~/= 2;
for (int y = startY; y < endY; y++) {
// The Carmack 286 Hack: pixelOfs + y gives the exact byte address!
sprite[x][y] = vswap.getUint8(offset + pixelOfs + y);
}
cmdOffset += 6; // Move to the next 6-byte instruction
}
}
sprites.add(sprite);
}
return sprites;
}
/// Extracts digitized sound effects (PCM Audio) from VSWAP.WL1
static List<Uint8List> parseSounds(ByteData vswap) {
int chunks = vswap.getUint16(0, Endian.little);
int soundStart = vswap.getUint16(4, Endian.little);
List<int> offsets = [];
List<int> lengths = [];
// Offsets are 32-bit integers starting at byte 6
for (int i = 0; i < chunks; i++) {
offsets.add(vswap.getUint32(6 + (i * 4), Endian.little));
}
// Lengths are 16-bit integers immediately following the offset array
int lengthStart = 6 + (chunks * 4);
for (int i = 0; i < chunks; i++) {
lengths.add(vswap.getUint16(lengthStart + (i * 2), Endian.little));
}
List<Uint8List> sounds = [];
// Sounds start after the sprites and go to the end of the chunks
for (int i = soundStart; i < chunks; i++) {
int offset = offsets[i];
int length = lengths[i];
if (offset == 0 || length == 0) {
sounds.add(Uint8List(0)); // Empty placeholder
continue;
}
// Extract the raw 8-bit PCM audio bytes
final soundData = vswap.buffer.asUint8List(offset, length);
sounds.add(soundData);
}
return sounds;
}
}

View File

@@ -0,0 +1,8 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library;
export 'src/classes/level.dart' show Level;
export 'src/classes/sprite.dart' show Sprite;
export 'src/vswap_parser.dart' show VswapParser;

View File

@@ -0,0 +1,13 @@
name: wolf_3d_data
description: A starting point for Dart libraries or applications.
version: 1.0.0
# repository: https://github.com/my_org/my_repo
environment:
sdk: ^3.11.1
resolution: workspace
dev_dependencies:
lints: ^6.0.0
test: ^1.25.6