psx map standalone exporter
This commit is contained in:
parent
a9153546ae
commit
2f243976b6
16 changed files with 3254 additions and 5 deletions
169
psx-map-exporter/src/render.js
Normal file
169
psx-map-exporter/src/render.js
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
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'],
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
for (const item of items) {
|
||||
drawLabel(
|
||||
canvas,
|
||||
width,
|
||||
height,
|
||||
item.labelId ?? item.id,
|
||||
item.drawX - bounds.minX + padding,
|
||||
item.drawY - bounds.minY + padding
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
bounds,
|
||||
png: encodePng(canvas, width, height),
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue