Crusader_Decomp/psx-map-exporter/src/render.js

215 lines
7.2 KiB
JavaScript
Raw Normal View History

2026-04-13 15:59:50 +02:00
import { encodePng } from './bundles.js';
const DEFAULT_BACKGROUND = { red: 18, green: 18, blue: 18, alpha: 255 };
const GLYPHS = {
'0': ['111', '101', '101', '101', '111'],
'1': ['010', '110', '010', '010', '111'],
'2': ['111', '001', '111', '100', '111'],
'3': ['111', '001', '111', '001', '111'],
'4': ['101', '101', '111', '001', '001'],
'5': ['111', '100', '111', '001', '111'],
'6': ['111', '100', '111', '101', '111'],
'7': ['111', '001', '001', '001', '001'],
'8': ['111', '101', '111', '101', '111'],
'9': ['111', '101', '111', '001', '111'],
2026-04-18 14:38:40 +02:00
'a': ['010', '101', '111', '101', '101'],
'b': ['110', '101', '110', '101', '110'],
'c': ['111', '100', '100', '100', '111'],
'd': ['110', '101', '101', '101', '110'],
'e': ['111', '100', '110', '100', '111'],
'f': ['111', '100', '110', '100', '100'],
'p': ['110', '101', '110', '100', '100'],
't': ['111', '010', '010', '010', '010'],
'#': ['101', '111', '101', '111', '101'],
':': ['000', '010', '000', '010', '000'],
'/': ['001', '001', '010', '100', '100'],
'-': ['000', '000', '111', '000', '000'],
2026-04-13 15:59:50 +02:00
};
function clearCanvas(width, height, background = null) {
const canvas = Buffer.alloc(width * height * 4, 0);
if (!background) {
return canvas;
}
fillRect(
canvas,
width,
height,
0,
0,
width,
height,
background.red ?? 0,
background.green ?? 0,
background.blue ?? 0,
background.alpha ?? 255,
);
return canvas;
}
function fillRect(canvas, canvasWidth, canvasHeight, x, y, width, height, red, green, blue, alpha) {
const startX = Math.max(0, x);
const startY = Math.max(0, y);
const endX = Math.min(canvasWidth, x + width);
const endY = Math.min(canvasHeight, y + height);
for (let drawY = startY; drawY < endY; drawY += 1) {
for (let drawX = startX; drawX < endX; drawX += 1) {
const target = ((drawY * canvasWidth) + drawX) * 4;
canvas[target + 0] = red;
canvas[target + 1] = green;
canvas[target + 2] = blue;
canvas[target + 3] = alpha;
}
}
}
function drawGlyph(canvas, canvasWidth, canvasHeight, glyph, x, y, red, green, blue, alpha) {
const rows = GLYPHS[glyph];
if (!rows) {
return;
}
for (let rowIndex = 0; rowIndex < rows.length; rowIndex += 1) {
const row = rows[rowIndex];
for (let columnIndex = 0; columnIndex < row.length; columnIndex += 1) {
if (row[columnIndex] !== '1') {
continue;
}
fillRect(canvas, canvasWidth, canvasHeight, x + columnIndex, y + rowIndex, 1, 1, red, green, blue, alpha);
}
}
}
function drawLabel(canvas, canvasWidth, canvasHeight, text, x, y) {
const label = String(text);
const glyphWidth = 3;
const glyphHeight = 5;
const spacing = 1;
const boxWidth = (label.length * (glyphWidth + spacing)) - spacing + 2;
const boxHeight = glyphHeight + 2;
fillRect(canvas, canvasWidth, canvasHeight, x, y, boxWidth, boxHeight, 0, 0, 0, 220);
let cursorX = x + 1;
for (const glyph of label) {
drawGlyph(canvas, canvasWidth, canvasHeight, glyph, cursorX, y + 1, 255, 255, 0, 255);
cursorX += glyphWidth + spacing;
}
}
2026-04-18 14:38:40 +02:00
function drawBoundingBox(canvas, canvasWidth, canvasHeight, x, y, width, height, alpha = 220) {
if (width <= 0 || height <= 0) {
return;
}
// Top + bottom edges
fillRect(canvas, canvasWidth, canvasHeight, x, y, width, 1, 255, 255, 255, alpha);
fillRect(canvas, canvasWidth, canvasHeight, x, y + height - 1, width, 1, 255, 255, 255, alpha);
// Left + right edges
fillRect(canvas, canvasWidth, canvasHeight, x, y, 1, height, 255, 255, 255, alpha);
fillRect(canvas, canvasWidth, canvasHeight, x + width - 1, y, 1, height, 255, 255, 255, alpha);
}
2026-04-13 15:59:50 +02:00
function blitRgba(canvas, canvasWidth, canvasHeight, sprite, dstX, dstY, flipped = false) {
for (let y = 0; y < sprite.height; y += 1) {
const canvasY = dstY + y;
if (canvasY < 0 || canvasY >= canvasHeight) {
continue;
}
for (let x = 0; x < sprite.width; x += 1) {
const canvasX = dstX + x;
if (canvasX < 0 || canvasX >= canvasWidth) {
continue;
}
const sourceX = flipped ? (sprite.width - 1 - x) : x;
const source = ((y * sprite.width) + sourceX) * 4;
const alpha = sprite.rgba[source + 3];
if (alpha === 0) {
continue;
}
const target = ((canvasY * canvasWidth) + canvasX) * 4;
canvas[target + 0] = sprite.rgba[source + 0];
canvas[target + 1] = sprite.rgba[source + 1];
canvas[target + 2] = sprite.rgba[source + 2];
canvas[target + 3] = alpha;
}
}
}
export function renderMap(items, options = {}) {
if (items.length === 0) {
throw new Error('No renderable scene items were produced.');
}
const bounds = items.reduce(
(state, item) => ({
minX: Math.min(state.minX, item.drawX),
minY: Math.min(state.minY, item.drawY),
maxX: Math.max(state.maxX, item.drawX + item.width),
maxY: Math.max(state.maxY, item.drawY + item.height),
}),
{ minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity }
);
const padding = 16;
const width = Math.max(1, (bounds.maxX - bounds.minX) + (padding * 2));
const height = Math.max(1, (bounds.maxY - bounds.minY) + (padding * 2));
const canvas = clearCanvas(width, height, options.background ?? DEFAULT_BACKGROUND);
for (const item of items) {
blitRgba(
canvas,
width,
height,
item.sprite,
item.drawX - bounds.minX + padding,
item.drawY - bounds.minY + padding,
Boolean(item.flipped)
);
}
if (options.drawLabels) {
2026-04-18 14:38:40 +02:00
// Cap labels at 10 instances per (typeWord, bundleAbsoluteOffset) pair so
// dense root layers (e.g. floor tiles) do not bury the labels we actually
// need to read. The bounding box is also suppressed once the label budget
// is exhausted so duplicate-instance clusters do not redraw white outlines
// on every tile and obscure surrounding art.
const labelBudgetPerBundle = 10;
const labelTally = new Map();
2026-04-13 15:59:50 +02:00
for (const item of items) {
2026-04-18 14:38:40 +02:00
const tallyKey = `${item.typeWord ?? '?'}|${item.bundleAbsoluteOffset ?? '?'}`;
const used = labelTally.get(tallyKey) ?? 0;
if (used >= labelBudgetPerBundle) {
continue;
}
labelTally.set(tallyKey, used + 1);
const boxX = item.drawX - bounds.minX + padding;
const boxY = item.drawY - bounds.minY + padding;
const boxW = item.width ?? item.sprite?.width ?? 0;
const boxH = item.height ?? item.sprite?.height ?? 0;
drawBoundingBox(canvas, width, height, boxX, boxY, boxW, boxH);
// Compose a useful label: typeWord (hex) + bundle absolute offset (last 4 hex)
const labelParts = [];
if (Number.isInteger(item.typeWord)) {
labelParts.push('t' + item.typeWord.toString(16));
}
if (Number.isInteger(item.bundleAbsoluteOffset)) {
labelParts.push('b' + (item.bundleAbsoluteOffset & 0xffff).toString(16).padStart(4, '0'));
}
if (labelParts.length === 0 && (item.labelId ?? item.id) !== undefined) {
labelParts.push('#' + (item.labelId ?? item.id));
}
drawLabel(canvas, width, height, labelParts.join(':'), boxX, boxY);
2026-04-13 15:59:50 +02:00
}
}
return {
width,
height,
bounds,
png: encodePng(canvas, width, height),
};
}