shithub: rgbds

Download patch

ref: 57ac07b03ebd6c82e5df843b380feaccca593277
parent: c521233499b18b6c20d05ea9c6590827c7e15170
author: ISSOtm <eldredhabert0@gmail.com>
date: Sun Apr 24 13:15:27 EDT 2022

Correctly handle fully-transparent tiles when outputting unoptimized tilemap

--- a/src/gfx/process.cpp
+++ b/src/gfx/process.cpp
@@ -439,6 +439,11 @@
 };
 
 struct AttrmapEntry {
+	/**
+	 * This field can either be a proto-palette ID, or `transparent` to indicate that the
+	 * corresponding tile is fully transparent. If you are looking to get the palette ID for this
+	 * attrmap entry while correctly handling the above, use `getPalID`.
+	 */
 	size_t protoPaletteID; // Only this field is used when outputting "unoptimized" data
 	uint8_t tileID; // This is the ID as it will be output to the tilemap
 	bool bank;
@@ -446,6 +451,10 @@
 	bool xFlip;
 
 	static constexpr decltype(protoPaletteID) transparent = SIZE_MAX;
+
+	size_t getPalID(DefaultInitVec<size_t> const &mappings) const {
+		return protoPaletteID == transparent ? 0 : mappings[protoPaletteID];
+	}
 };
 
 static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
@@ -681,10 +690,8 @@
 
 	auto iter = attrmap.begin();
 	for (auto tile : png.visitAsTiles(options.columnMajor)) {
-		size_t protoPaletteID = iter->protoPaletteID;
 		// If the tile is fully transparent, default to palette 0
-		Palette const &palette =
-		    palettes[protoPaletteID != AttrmapEntry::transparent ? mappings[protoPaletteID] : 0];
+		Palette const &palette = palettes[iter->getPalID(mappings)];
 		for (uint32_t y = 0; y < 8; ++y) {
 			uint16_t bitplanes = TileData::rowBitplanes(tile, palette, y);
 			output.sputc(bitplanes & 0xFF);
@@ -729,7 +736,7 @@
 			tilemapOutput->sputc(tileID + options.baseTileIDs[bank]);
 		}
 		if (attrmapOutput.has_value()) {
-			uint8_t palID = mappings[iter->protoPaletteID] & 7;
+			uint8_t palID = iter->getPalID(mappings) & 7;
 			attrmapOutput->sputc(palID | bank << 3); // The other flags are all 0
 			++iter;
 		}