Get masking working with tile map output again

This commit is contained in:
Lucas Scharenbroich
2021-11-15 16:41:01 -06:00
parent 976ab3163b
commit d268b34e76
2 changed files with 72 additions and 37 deletions

View File

@@ -133,28 +133,7 @@ function getOptions(argv) {
return options;
}
async function main(argv) {
// try {
const png = await readPNG(argv[0]);
const options = getOptions(argv);
console.info(`; startIndex = ${options.startIndex}`);
if (png.colorType !== 3) {
console.warn('; PNG must be in palette color type');
return;
}
if (png.palette.length > 16) {
console.warn('; Too many colors. Must be 16 or less');
return;
}
if (options.palette && options.palette.length > 16) {
console.warn('; Too many colors on command line. Must be 16 or less');
return;
}
function getPaletteMap(options, png) {
// Get the RGB triplets from the palette
const sourcePalette = png.palette;
const targetPalette = options.targetPalette || sourcePalette;
@@ -191,6 +170,37 @@ async function main(argv) {
}
});
return {
paletteMap,
sourcePalette,
targetPalette
};
}
async function main(argv) {
// try {
const png = await readPNG(argv[0]);
const options = getOptions(argv);
console.info(`; startIndex = ${options.startIndex}`);
if (png.colorType !== 3) {
console.warn('; PNG must be in palette color type');
return;
}
if (png.palette.length > 16) {
console.warn('; Too many colors. Must be 16 or less');
return;
}
if (options.palette && options.palette.length > 16) {
console.warn('; Too many colors on command line. Must be 16 or less');
return;
}
// Get the RGB triplets from the palette
const { targetPalette, paletteMap } = getPaletteMap(options, png);
options.paletteMap = paletteMap;
// Dump the palette in IIgs hex format
@@ -298,6 +308,11 @@ function buildTile(options, buff, _mask, width, x, y) {
if (mask.some(h => h != 0)) {
tile.isSolid = false;
}
if (x === 120 && y === 8) {
console.warn(`isSolid: ${tile.isSolid}` );
console.warn(data.map(d => d.toString(16)), mask);
}
}
for (dy = 0; dy < 8; dy += 1) {
@@ -475,6 +490,7 @@ module.exports = {
buildMerlinCodeForTiles,
buildMerlinCodeForTile,
findColorIndex,
getPaletteMap,
paletteToIIgs,
pngToIIgsBuff,
readPNG,

View File

@@ -28,7 +28,8 @@ function hexToRbg(hex) {
async function readTileSet(workdir, tileset) {
// Load up the PNG image
const pngfile = path.resolve(path.join(workdir, tileset.image.source));
const imageSource = GLOBALS.options.tilesetImage || tileset.image.source;
const pngfile = path.resolve(path.join(workdir, imageSource));
console.log(`Reading PNG file from ${pngfile}`);
const png = await png2iigs.readPNG(pngfile);
@@ -38,7 +39,7 @@ async function readTileSet(workdir, tileset) {
if (tileset.image.trans) {
const color = hexToRbg(tileset.image.trans);
console.log(`Found color ${color} as transparent marker`);
transparentIndex = png2iigs.findColorIndex(GLOBALS.options, png, color);
[transparentIndex] = png2iigs.findColorIndex(GLOBALS.options, png, color);
if (typeof transparentIndex !== 'number') {
console.log('Could not find color in palette');
console.log(png.palette);
@@ -47,10 +48,15 @@ async function readTileSet(workdir, tileset) {
console.log(`Transparent color palette index is ${transparentIndex}`);
}
}
GLOBALS.options.transparentIndex = transparentIndex
console.log(`Converting PNG to IIgs bitmap format...`);
const [buff, mask] = png2iigs.pngToIIgsBuff(GLOBALS.options, png);
console.log(`Mapping source and target palettes`);
const { paletteMap } = png2iigs.getPaletteMap(GLOBALS.options, png);
GLOBALS.options.paletteMap = paletteMap;
console.log(`Building tiles...`);
const tiles = png2iigs.buildTiles(GLOBALS.options, buff, mask, png.width / 2, transparentIndex);
@@ -225,6 +231,7 @@ async function main(argv) {
const forceMasked = getArg(argv, '--force-masked', x => true, false);
const noGenTiles = getArg(argv, '--no-gen-tiles', x => true, false);
const emptyTile = getArg(argv, '--empty-tile', x => parseInt(x, 10), -1);
const tileSet = getArg(argv, '--tile-set', x => x, null);
console.log(`Reading Tiled JSON file from ${fullpath}`);
const raw = fs.readFileSync(fullpath);
@@ -263,6 +270,10 @@ async function main(argv) {
// Load up any/all tilesets
const tileSets = await Promise.all(doc.tilesets.map(tileset => loadTileset(workdir, tileset)));
if (tileSets.length === 1 && tileSet !== null) {
tileSets[0].tileset.image.source = tileSet;
}
// Create a global reference object
GLOBALS = {
...GLOBALS,
@@ -449,6 +460,14 @@ function convertTileID(tileId, tileset) {
}
const mask_bit = (!tileset[tileIndex - 1].isSolid || tileIndex === GLOBALS.emptyTile) && ((GLOBALS.tileLayers.length !== 1) || GLOBALS.forceMasked);
if (tileIndex === 48) {
console.warn('isSolid: ', tileset[tileIndex - 1].isSolid);
console.warn('GLOBALS.emptyTile: ', GLOBALS.emptyTile);
console.warn('GLOBALS.tileLayers.length: ', GLOBALS.tileLayers.length);
console.warn('GLOBALS.forceMasked: ', GLOBALS.forceMasked);
console.warn('mask_bit: ', mask_bit);
}
// Build up a partial set of control bits
let control_bits = (mask_bit ? GTE_MASK_BIT : 0) + (hflip ? GTE_HFLIP_BIT : 0) + (vflip ? GTE_VFLIP_BIT : 0);