WIP: Mapping sprite IDs (broken)

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-13 18:06:40 +01:00
parent af17a0aaf7
commit bf8d9d7eb1
7 changed files with 220 additions and 31 deletions

View File

@@ -43,7 +43,7 @@ class VswapParser {
/// Extracts the compiled scaled sprites from VSWAP.WL1
static List<Matrix<int>> parseSprites(ByteData vswap) {
int chunks = vswap.getUint16(0, Endian.little);
int spriteStart = vswap.getUint16(2, Endian.little);
int spriteStart = vswap.getUint16(2, Endian.little); // 64
int soundStart = vswap.getUint16(4, Endian.little);
List<int> offsets = [];

View File

@@ -1,293 +0,0 @@
import 'dart:math';
import 'package:flutter/material.dart';
extension WolfPaletteMatch on Color {
/// Finds the index of the closest color in the wolfPalette
int findClosestIndex(List<Color> palette) {
int closestIndex = 0;
double minDistance = double.infinity;
for (int i = 0; i < palette.length; i++) {
final Color pColor = palette[i];
// Calculate squared Euclidean distance (skipping sqrt for performance)
double distance =
pow(r - pColor.r, 2).toDouble() +
pow(g - pColor.g, 2).toDouble() +
pow(b - pColor.b, 2).toDouble();
if (distance < minDistance) {
minDistance = distance;
closestIndex = i;
}
}
return closestIndex;
}
/// Returns the actual Color object from the palette that matches best
Color toWolfColor(List<Color> palette) {
return palette[findClosestIndex(palette)];
}
}
abstract class ColorPalette {
static const List<Color> vga = [
Color(0xFF000000),
Color(0xFF0000AA),
Color(0xFF00AA00),
Color(0xFF00AAAA),
Color(0xFFAA0000),
Color(0xFFAA00AA),
Color(0xFFAA5500),
Color(0xFFAAAAAA),
Color(0xFF555555),
Color(0xFF5555FF),
Color(0xFF55FF55),
Color(0xFF55FFFF),
Color(0xFFFF5555),
Color(0xFFFF55FF),
Color(0xFFFFFF55),
Color(0xFFFFFFFF),
Color(0xFFEEEEEE),
Color(0xFFDEDEDE),
Color(0xFFD2D2D2),
Color(0xFFC2C2C2),
Color(0xFFB6B6B6),
Color(0xFFAAAAAA),
Color(0xFF999999),
Color(0xFF8D8D8D),
Color(0xFF7D7D7D),
Color(0xFF717171),
Color(0xFF656565),
Color(0xFF555555),
Color(0xFF484848),
Color(0xFF383838),
Color(0xFF2C2C2C),
Color(0xFF202020),
Color(0xFFFF0000),
Color(0xFFEE0000),
Color(0xFFE20000),
Color(0xFFD60000),
Color(0xFFCA0000),
Color(0xFFBE0000),
Color(0xFFB20000),
Color(0xFFA50000),
Color(0xFF990000),
Color(0xFF890000),
Color(0xFF7D0000),
Color(0xFF710000),
Color(0xFF650000),
Color(0xFF590000),
Color(0xFF4C0000),
Color(0xFF400000),
Color(0xFFFFDADA),
Color(0xFFFFBABA),
Color(0xFFFF9D9D),
Color(0xFFFF7D7D),
Color(0xFFFF5D5D),
Color(0xFFFF4040),
Color(0xFFFF2020),
Color(0xFFFF0000),
Color(0xFFFFAA5D),
Color(0xFFFF9940),
Color(0xFFFF8920),
Color(0xFFFF7900),
Color(0xFFE66D00),
Color(0xFFCE6100),
Color(0xFFB65500),
Color(0xFF9D4C00),
Color(0xFFFFFFDA),
Color(0xFFFFFFBA),
Color(0xFFFFFF9D),
Color(0xFFFFFF7D),
Color(0xFFFFFA5D),
Color(0xFFFFF640),
Color(0xFFFFF620),
Color(0xFFFFF600),
Color(0xFFE6DA00),
Color(0xFFCEC600),
Color(0xFFB6AE00),
Color(0xFF9D9D00),
Color(0xFF858500),
Color(0xFF716D00),
Color(0xFF595500),
Color(0xFF404000),
Color(0xFFD2FF5D),
Color(0xFFC6FF40),
Color(0xFFB6FF20),
Color(0xFFA1FF00),
Color(0xFF91E600),
Color(0xFF81CE00),
Color(0xFF75B600),
Color(0xFF619D00),
Color(0xFFDAFFDA),
Color(0xFFBEFFBA),
Color(0xFF9DFF9D),
Color(0xFF81FF7D),
Color(0xFF61FF5D),
Color(0xFF40FF40),
Color(0xFF20FF20),
Color(0xFF00FF00),
Color(0xFF00FF00),
Color(0xFF00EE00),
Color(0xFF00E200),
Color(0xFF00D600),
Color(0xFF04CA00),
Color(0xFF04BE00),
Color(0xFF04B200),
Color(0xFF04A500),
Color(0xFF049900),
Color(0xFF048900),
Color(0xFF047D00),
Color(0xFF047100),
Color(0xFF046500),
Color(0xFF045900),
Color(0xFF044C00),
Color(0xFF044000),
Color(0xFFDAFFFF),
Color(0xFFBAFFFF),
Color(0xFF9DFFFF),
Color(0xFF7DFFFA),
Color(0xFF5DFFFF),
Color(0xFF40FFFF),
Color(0xFF20FFFF),
Color(0xFF00FFFF),
Color(0xFF00E6E6),
Color(0xFF00CECE),
Color(0xFF00B6B6),
Color(0xFF009D9D),
Color(0xFF008585),
Color(0xFF007171),
Color(0xFF005959),
Color(0xFF004040),
Color(0xFF5DBEFF),
Color(0xFF40B2FF),
Color(0xFF20AAFF),
Color(0xFF009DFF),
Color(0xFF008DE6),
Color(0xFF007DCE),
Color(0xFF006DB6),
Color(0xFF005D9D),
Color(0xFFDADADA),
Color(0xFFBABEFF),
Color(0xFF9D9DFF),
Color(0xFF7D81FF),
Color(0xFF5D61FF),
Color(0xFF4040FF),
Color(0xFF2024FF),
Color(0xFF0004FF),
Color(0xFF0000FF),
Color(0xFF0000EE),
Color(0xFF0000E2),
Color(0xFF0000D6),
Color(0xFF0000CA),
Color(0xFF0000BE),
Color(0xFF0000B2),
Color(0xFF0000A5),
Color(0xFF000099),
Color(0xFF000089),
Color(0xFF00007D),
Color(0xFF000071),
Color(0xFF000065),
Color(0xFF000059),
Color(0xFF00004C),
Color(0xFF000040),
Color(0xFF282828),
Color(0xFFFFE234),
Color(0xFFFFD624),
Color(0xFFFFCE18),
Color(0xFFFFC208),
Color(0xFFFFB600),
Color(0xFFB620FF),
Color(0xFFAA00FF),
Color(0xFF9900E6),
Color(0xFF8100CE),
Color(0xFF7500B6),
Color(0xFF61009D),
Color(0xFF500085),
Color(0xFF440071),
Color(0xFF340059),
Color(0xFF280040),
Color(0xFFFFDAFF),
Color(0xFFFFBAFF),
Color(0xFFFF9DFF),
Color(0xFFFF7DFF),
Color(0xFFFF5DFF),
Color(0xFFFF40FF),
Color(0xFFFF20FF),
Color(0xFFFF00FF),
Color(0xFFE200E6),
Color(0xFFCA00CE),
Color(0xFFB600B6),
Color(0xFF9D009D),
Color(0xFF850085),
Color(0xFF6D0071),
Color(0xFF590059),
Color(0xFF400040),
Color(0xFFFFEADE),
Color(0xFFFFE2D2),
Color(0xFFFFDAC6),
Color(0xFFFFD6BE),
Color(0xFFFFCEB2),
Color(0xFFFFC6A5),
Color(0xFFFFBE9D),
Color(0xFFFFBA91),
Color(0xFFFFB281),
Color(0xFFFA571F),
Color(0xFFFF9D61),
Color(0xFFF2955D),
Color(0xFFEA8D59),
Color(0xFFDE8955),
Color(0xFFD28150),
Color(0xFFCA7D4C),
Color(0xFFBE7948),
Color(0xFFB67144),
Color(0xFFAA6940),
Color(0xFFA1653C),
Color(0xFF9D6138),
Color(0xFF915D34),
Color(0xFF895930),
Color(0xFF81502C),
Color(0xFF754C28),
Color(0xFF6D4824),
Color(0xFF5D4020),
Color(0xFF553C1C),
Color(0xFF483818),
Color(0xFF403018),
Color(0xFF382C14),
Color(0xFF28200C),
Color(0xFF610065),
Color(0xFF006565),
Color(0xFF006161),
Color(0xFF00001C),
Color(0xFF00002C),
Color(0xFF302410),
Color(0xFF480048),
Color(0xFF500050),
Color(0xFF000034),
Color(0xFF1C1C1C),
Color(0xFF4C4C4C),
Color(0xFF5D5D5D),
Color(0xFF404040),
Color(0xFF303030),
Color(0xFF343434),
Color(0xFFDAF6F6),
Color(0xFFBAEAEA),
Color(0xFF9DDEDE),
Color(0xFF75CACA),
Color(0xFF48C2C2),
Color(0xFF20B6B6),
Color(0xFF20B2B2),
Color(0xFF00A5A5),
Color(0xFF009999),
Color(0xFF008D8D),
Color(0xFF008585),
Color(0xFF007D7D),
Color(0xFF007979),
Color(0xFF007575),
Color(0xFF007171),
Color(0xFF006D6D),
Color(0xFF990089),
];
}

View File

@@ -1,9 +1,10 @@
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:wolf_dart/classes/color_palette.dart';
import 'package:wolf_dart/classes/linear_coordinates.dart';
import 'package:wolf_dart/classes/matrix.dart';
import 'package:wolf_dart/features/renderer/color_palette.dart';
import 'package:wolf_dart/classes/sprite.dart';
class RaycasterPainter extends CustomPainter {
final Matrix<int> map;
@@ -12,6 +13,8 @@ class RaycasterPainter extends CustomPainter {
final double playerAngle;
final double fov;
final Map<String, double> doorOffsets;
final List<Matrix<int>> sprites;
final List<Sprite> entities;
RaycasterPainter({
required this.map,
@@ -20,6 +23,8 @@ class RaycasterPainter extends CustomPainter {
required this.playerAngle,
required this.fov,
required this.doorOffsets,
required this.sprites,
required this.entities,
});
@override
@@ -36,11 +41,15 @@ class RaycasterPainter extends CustomPainter {
int screenWidth = size.width.toInt();
// The 1D Z-Buffer
List<double> zBuffer = List.filled(screenWidth, 0.0);
double dirX = math.cos(playerAngle);
double dirY = math.sin(playerAngle);
double planeX = -dirY * math.tan(fov / 2);
double planeY = dirX * math.tan(fov / 2);
// --- 1. CAST WALLS ---
for (int x = 0; x < screenWidth; x++) {
double cameraX = 2 * x / screenWidth - 1.0;
double rayDirX = dirX + planeX * cameraX;
@@ -51,23 +60,18 @@ class RaycasterPainter extends CustomPainter {
double sideDistX;
double sideDistY;
double deltaDistX = (rayDirX == 0) ? 1e30 : (1.0 / rayDirX).abs();
double deltaDistY = (rayDirY == 0) ? 1e30 : (1.0 / rayDirY).abs();
double perpWallDist;
int stepX;
int stepY;
bool hit = false;
bool hitOutOfBounds = false;
int side = 0;
int hitWallId = 0;
double doorOffset =
0.0; // Track the slide amount to pass to the texture drawer
Set<String> ignoredDoors =
{}; // Prevent the ray from checking the same door gap twice
double doorOffset = 0.0;
Set<String> ignoredDoors = {};
if (rayDirX < 0) {
stepX = -1;
@@ -84,7 +88,6 @@ class RaycasterPainter extends CustomPainter {
sideDistY = (mapY + 1.0 - player.y) * deltaDistY;
}
// 3. The True DDA Loop
while (!hit) {
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
@@ -103,13 +106,10 @@ class RaycasterPainter extends CustomPainter {
hit = true;
hitOutOfBounds = true;
} else if (map[mapY][mapX] > 0) {
// NEW: DOOR PASS-THROUGH LOGIC
String doorKey = '$mapX,$mapY';
if (map[mapY][mapX] >= 90 && !ignoredDoors.contains(doorKey)) {
double currentOffset = doorOffsets[doorKey] ?? 0.0;
if (currentOffset > 0.0) {
// Calculate exactly where the ray hit the block
double perpWallDistTemp = (side == 0)
? (sideDistX - deltaDistX)
: (sideDistY - deltaDistY);
@@ -117,23 +117,18 @@ class RaycasterPainter extends CustomPainter {
? player.y + perpWallDistTemp * rayDirY
: player.x + perpWallDistTemp * rayDirX;
wallXTemp -= wallXTemp.floor();
// If the hit coordinate is less than the open amount, the ray passes through!
if (wallXTemp < currentOffset) {
ignoredDoors.add(doorKey);
continue; // Skip the rest of this loop iteration and keep raycasting!
continue;
}
}
doorOffset =
currentOffset; // Save the offset so we can slide the texture
doorOffset = currentOffset;
}
hit = true;
hitWallId = map[mapY][mapX];
}
}
// If the ray escaped the map, don't draw garbage, just draw the background!
if (hitOutOfBounds) continue;
if (side == 0) {
@@ -142,6 +137,9 @@ class RaycasterPainter extends CustomPainter {
perpWallDist = (sideDistY - deltaDistY);
}
// STORE THE DISTANCE IN THE Z-BUFFER!
zBuffer[x] = perpWallDist;
double wallX;
if (side == 0) {
wallX = player.y + perpWallDist * rayDirY;
@@ -162,6 +160,89 @@ class RaycasterPainter extends CustomPainter {
doorOffset,
);
}
// --- 2. DRAW SPRITES ---
// Sort sprites from furthest to closest (Painter's Algorithm)
List<Sprite> activeSprites = List.from(entities);
activeSprites.sort((a, b) {
double distA =
math.pow(player.x - a.x, 2) + math.pow(player.y - a.y, 2).toDouble();
double distB =
math.pow(player.x - b.x, 2) + math.pow(player.y - b.y, 2).toDouble();
return distB.compareTo(distA);
});
for (Sprite sprite in activeSprites) {
// Translate sprite position to relative to camera
double spriteX = sprite.x - player.x;
double spriteY = sprite.y - player.y;
// Inverse camera matrix (Transform to screen space)
double invDet = 1.0 / (planeX * dirY - dirX * planeY);
double transformX = invDet * (dirY * spriteX - dirX * spriteY);
double transformY = invDet * (-planeY * spriteX + planeX * spriteY);
// print("Sprite at ${sprite.x}, ${sprite.y} transformY: $transformY");
// Is the sprite in front of the camera?
if (transformY > 0) {
// print(
// "Attempting to draw Sprite Index: ${sprite.spriteIndex} (Total Sprites Loaded: ${sprites.length})",
// );
int spriteScreenX = ((screenWidth / 2) * (1 + transformX / transformY))
.toInt();
// Calculate height and width (Sprites are 64x64 squares)
int spriteHeight = (size.height / transformY).abs().toInt();
int spriteWidth = spriteHeight;
int drawStartX = -spriteWidth ~/ 2 + spriteScreenX;
int drawEndX = spriteWidth ~/ 2 + spriteScreenX;
// Clip to screen boundaries
int clipStartX = math.max(0, drawStartX);
int clipEndX = math.min(screenWidth - 1, drawEndX);
for (int stripe = clipStartX; stripe < clipEndX; stripe++) {
// THE Z-BUFFER CHECK!
// Only draw if the sprite is closer to the camera than the wall at this pixel column
if (transformY < zBuffer[stripe]) {
int texX = ((stripe - drawStartX) * 64 / spriteWidth).toInt().clamp(
0,
63,
);
double startY = (size.height / 2) - (spriteHeight / 2);
double stepY = spriteHeight / 64.0;
// Safeguard against bad sprite indices
int safeIndex = sprite.spriteIndex.clamp(0, sprites.length - 1);
Matrix<int> spritePixels = sprites[safeIndex];
for (int ty = 0; ty < 64; ty++) {
int colorByte = spritePixels[texX][ty];
// Only draw if the pixel is NOT 255 (Magenta Transparency)
if (colorByte != 255) {
double endY = startY + stepY;
if (endY > 0 && startY < size.height) {
canvas.drawLine(
Offset(stripe.toDouble(), startY),
Offset(stripe.toDouble(), endY),
Paint()
..color = ColorPalette.vga[colorByte]
..strokeWidth = 1.1,
);
}
}
startY += stepY;
}
}
}
}
}
}
void _drawTexturedColumn(

View File

@@ -5,11 +5,20 @@ import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:wolf_dart/classes/linear_coordinates.dart';
import 'package:wolf_dart/classes/matrix.dart';
import 'package:wolf_dart/classes/sprite.dart';
import 'package:wolf_dart/features/map/wolf_map.dart';
import 'package:wolf_dart/features/renderer/raycast_painter.dart';
import 'package:wolf_dart/sprite_gallery.dart';
class WolfRenderer extends StatefulWidget {
const WolfRenderer({super.key});
const WolfRenderer({
super.key,
this.showSpriteGallery = false,
this.isDemo = true,
});
final bool showSpriteGallery;
final bool isDemo;
@override
State<WolfRenderer> createState() => _WolfRendererState();
@@ -30,6 +39,8 @@ class _WolfRendererState extends State<WolfRenderer>
bool _isLoading = true;
bool _spaceWasPressed = false;
List<Sprite> entities = [];
// Track door animations
// Key is "X,Y" coordinate. Value is how far open it is (0.0 to 1.0)
Map<String, double> doorOffsets = {};
@@ -50,28 +61,49 @@ class _WolfRendererState extends State<WolfRenderer>
final Matrix<int> objectLevel = gameMap.levels[0].objectGrid;
// 1. SCAN FOR PLAYER SPAWN
// Adjusted mapping for WL1 Shareware
// These numbers represent the delta from the FIRST sprite chunk in VSWAP
Map<int, int> staticObjects = {
23: widget.isDemo ? 0 : 23, // Water Puddle
24: widget.isDemo ? 1 : 24, // Green Barrel
25: widget.isDemo ? 2 : 25, // Chairs
26: widget.isDemo ? 3 : 26, // Green Plant
27: widget.isDemo ? 4 : 27, // Skeleton / Bones
28: widget.isDemo ? 5 : 28, // Blue Lamp
29: widget.isDemo ? 6 : 29, // Chandelier
30: widget.isDemo ? 7 : 30, // Dog food
31: widget.isDemo ? 8 : 31, // White pillar
32: widget.isDemo ? 9 : 32, // Hanged man
50: widget.isDemo ? 42 : 95, // Standing Guard (Front-facing frame)
};
// 1. SCAN FOR PLAYER SPAWN & ENTITIES
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
int objId = objectLevel[y][x];
// In Wolf3D, IDs 19-22 represent the player's spawn point and facing direction.
// Player Spawn
if (objId >= 19 && objId <= 22) {
// Place the player perfectly in the center of the block
player = (x: x + 0.5, y: y + 0.5);
// Map the ID to standard radians
switch (objId) {
case 19:
playerAngle = 3 * math.pi / 2; // North (Facing up the Y-axis)
playerAngle = 3 * math.pi / 2;
case 20:
playerAngle = 0.0; // East (Facing right)
playerAngle = 0.0;
case 21:
playerAngle = math.pi / 2; // South (Facing down)
playerAngle = math.pi / 2;
case 22:
playerAngle = math.pi; // West (Facing left)
playerAngle = math.pi;
}
}
// NEW: Populate the Entities!
else if (staticObjects.containsKey(objId)) {
entities.add((
x: x + 0.5,
y: y + 0.5,
spriteIndex: staticObjects[objId]!,
));
}
}
}
@@ -236,6 +268,10 @@ class _WolfRendererState extends State<WolfRenderer>
return const Center(child: CircularProgressIndicator(color: Colors.teal));
}
if (widget.showSpriteGallery) {
return SpriteGallery(sprites: gameMap.sprites);
}
return Scaffold(
backgroundColor: Colors.black,
body: KeyboardListener(
@@ -253,6 +289,8 @@ class _WolfRendererState extends State<WolfRenderer>
playerAngle: playerAngle,
fov: fov,
doorOffsets: doorOffsets,
entities: entities,
sprites: gameMap.sprites,
),
);
},