Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 14427x 3x 6561x 4366x 2195x 1296x 899x 3x 29x 29x 3x 29x 17x 12x 12x 12x 3x 3x 3x 3x 3x 29x 18296x 3x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 6x 29x 2x 29x 1x 29x 29x 29x 2116x 2116x 29x 18296x 18296x 18296x 17291x 1005x 1005x 1005x 18296x 1233x 17063x 29x 3x 15x 24x 24x 24x 17x 12x 12x 3x | import { assetFilename } from "shared/lib/helpers/assets";
import {
tileArrayToTileData,
tilesAndLookupToTilemap,
toTileLookup,
} from "shared/lib/tiles/tileData";
import {
readFileToTilesDataArray,
indexedImageToTilesDataArray,
} from "lib/tiles/readFileToTiles";
import {
BackgroundData,
Palette,
TilesetData,
} from "shared/lib/entities/entitiesTypes";
import promiseLimit from "lib/helpers/promiseLimit";
import { FLAG_VRAM_BANK_1 } from "consts";
import { fileExists } from "lib/helpers/fs/fileExists";
import {
readFileToPalettes,
readFileToPalettesUsingTiles,
} from "lib/tiles/readFileToPalettes";
import type { ColorModeSetting } from "store/features/settings/settingsState";
import l10n from "shared/lib/lang/l10n";
import { monoOverrideForFilename } from "shared/lib/assets/backgrounds";
import { ColorCorrectionSetting } from "shared/lib/resources/types";
import { ReferencedBackground } from "./precompile/determineUsedAssets";
import { HexPalette } from "shared/lib/tiles/autoColor";
import { divisibleBy8 } from "shared/lib/helpers/8bit";
import { MAX_BACKGROUND_TILES, MAX_BACKGROUND_TILES_CGB } from "consts";
const TILE_FIRST_CHUNK_SIZE = 128;
const TILE_BANK_SIZE = 192;
const MAX_IMAGE_WIDTH = 2040;
const MAX_IMAGE_HEIGHT = 2040;
const MAX_PIXELS = 16380 * 64;
type PrecompiledBackgroundData = BackgroundData & {
commonTilesetId?: string;
vramData: [number[], number[]];
tilemap: number[];
attr: number[];
autoPalettes?: Palette[];
is360: boolean;
colorMode: ColorModeSetting;
tilesetLength: number;
};
type CompileImageOptions = {
warnings: (msg: string) => void;
};
type ImageTileAllocationStrategy = (
tileIndex: number,
numTiles: number,
image: BackgroundData,
) => { tileIndex: number; inVRAM2: boolean };
/**
* Allocates an image tile for to default DMG location.
*
* @param {number} tileIndex - The index of the tile to allocate.
* @returns {{ tileIndex: number, inVRAM2: boolean }} Updated tile index and flag which is set if tile has been reallocated to VRAM bank2.
*/
export const imageTileAllocationDefault: ImageTileAllocationStrategy = (
tileIndex,
) => {
return {
tileIndex,
inVRAM2: false,
};
};
/**
* Allocates an image tile for color-only mode and adjusts the tile index based on VRAM bank allocation.
*
* @param {number} tileIndex - The index of the tile to allocate.
* @returns {{ tileIndex: number, inVRAM2: boolean }} Updated tile index and flag which is set if tile has been reallocated to VRAM bank2.
*/
export const imageTileAllocationColorOnly: ImageTileAllocationStrategy = (
tileIndex: number,
): { tileIndex: number; inVRAM2: boolean } => {
// First 128 tiles go into vram bank 1
if (tileIndex < 128) {
return {
tileIndex,
inVRAM2: false,
};
// Next 128 tiles go into vram bank 2
} else if (tileIndex < 256) {
return {
tileIndex: tileIndex - 128,
inVRAM2: true,
};
}
// After that split evenly between bank 1 and 2
return {
tileIndex: 128 + Math.floor((tileIndex - 256) / 2),
inVRAM2: tileIndex % 2 !== 0,
};
};
const padArrayEnd = <T>(arr: T[], len: number, padding: T) => {
Iif (arr.length > len) {
return arr.slice(0, len);
}
return arr.concat(Array(len - arr.length).fill(padding));
};
const mergeCommonTiles = async (
tileData: Uint8Array[],
commonTileset: TilesetData | undefined,
projectPath: string,
) => {
if (!commonTileset) {
return tileData;
}
const commonFilename = assetFilename(projectPath, "tilesets", commonTileset);
const commonTileData = await readFileToTilesDataArray(commonFilename);
return [...commonTileData, ...tileData];
};
enum ImageColorMode {
MANUAL,
AUTO_COLOR,
AUTO_COLOR_WITH_DMG,
}
const buildAttr = (
tileColors: number[],
autoTileColors: number[],
tileMapSize: number,
) => {
return padArrayEnd(tileColors || [], tileMapSize, 0).map(
(manualAttr, index) => {
return autoTileColors[index] !== undefined
? (manualAttr & 0xf8) + (autoTileColors[index] & 0x7)
: manualAttr;
},
);
};
export const compileImage = async (
img: BackgroundData,
commonTileset: TilesetData | undefined,
is360: boolean,
uiPalette: HexPalette | undefined,
colorMode: ColorModeSetting,
colorCorrection: ColorCorrectionSetting,
projectPath: string,
{ warnings }: CompileImageOptions,
): Promise<PrecompiledBackgroundData> => {
let autoColorMode = ImageColorMode.MANUAL;
const cgbOnly = colorMode === "color";
const filename = assetFilename(projectPath, "backgrounds", img);
const dmgFilename = monoOverrideForFilename(filename);
const useDmgImg = img.monoOverrideId && (await fileExists(dmgFilename));
Iif (img.autoColor && colorMode !== "mono") {
autoColorMode = useDmgImg
? ImageColorMode.AUTO_COLOR_WITH_DMG
: ImageColorMode.AUTO_COLOR;
}
const tilesFileName = useDmgImg ? dmgFilename : filename;
const tileAllocationStrategy = cgbOnly
? imageTileAllocationColorOnly
: imageTileAllocationDefault;
let tileData: Uint8Array[] = [];
let autoTileColors: number[] = [];
let autoPalettes: Palette[] | undefined = undefined;
Iif (autoColorMode === ImageColorMode.AUTO_COLOR) {
// Extract both tiles and colors from color PNG
const paletteData = await readFileToPalettes(
filename,
colorCorrection,
uiPalette,
);
tileData = indexedImageToTilesDataArray(paletteData.indexedImage);
autoTileColors = paletteData.map;
autoPalettes = paletteData.palettes.map((colors, index) => ({
id: `${img.id}_p${index}`,
name: `${img.name} Palette ${index}`,
colors,
}));
} else Iif (autoColorMode === ImageColorMode.AUTO_COLOR_WITH_DMG) {
// Extract colors from color PNG and tiles from .mono PNG
const paletteData = await readFileToPalettesUsingTiles(
filename,
tilesFileName,
colorCorrection,
uiPalette,
);
tileData = indexedImageToTilesDataArray(paletteData.indexedImage);
autoTileColors = paletteData.map;
autoPalettes = paletteData.palettes.map((colors, index) => ({
id: `${img.id}_p${index}`,
name: `${img.name} Palette ${index}`,
colors,
}));
} else {
// Extract tiles from PNG and use manual color data
tileData = await readFileToTilesDataArray(tilesFileName);
}
// Warn if auto palettes extracted too many unique palettes
Iif (autoPalettes && autoPalettes.length > 8) {
warnings(
`${img.filename}: ${l10n("WARNING_BACKGROUND_TOO_MANY_PALETTES", {
paletteLength: autoPalettes.length,
maxPaletteLength: 8,
})}`,
);
}
Iif (is360) {
const tilemap = Array.from(Array(360)).map((_, i) => i);
const tiles = tileArrayToTileData(tileData);
const attr = buildAttr(img.tileColors, autoTileColors, tilemap.length);
return {
...img,
vramData: [[...tiles], []],
tilemap,
attr,
autoPalettes,
is360,
colorMode,
tilesetLength: 360,
};
}
const tileDataWithCommon = await mergeCommonTiles(
tileData,
commonTileset,
projectPath,
);
const tilesetLookup = toTileLookup(tileDataWithCommon) ?? {};
const uniqueTiles = Object.values(tilesetLookup);
const tilemap = tilesAndLookupToTilemap(tileData, tilesetLookup);
const tilesetLength = Object.keys(tilesetLookup).length;
Iif (img.imageWidth < 160 || img.imageHeight < 144) {
warnings(l10n("WARNING_BACKGROUND_TOO_SMALL"));
}
Iif (img.imageWidth > MAX_IMAGE_WIDTH) {
warnings(
l10n("WARNING_BACKGROUND_TOO_WIDE", {
width: img.imageWidth,
maxWidth: MAX_IMAGE_WIDTH,
}),
);
}
Iif (img.imageHeight > MAX_IMAGE_HEIGHT) {
warnings(
l10n("WARNING_BACKGROUND_TOO_TALL", {
height: img.imageHeight,
maxHeight: MAX_IMAGE_HEIGHT,
}),
);
}
Iif (img.imageWidth * img.imageHeight > MAX_PIXELS) {
warnings(
l10n("WARNING_BACKGROUND_TOO_MANY_PIXELS", {
width: img.imageWidth,
height: img.imageHeight,
numPixels: img.imageWidth * img.imageHeight,
maxPixels: MAX_PIXELS,
}),
);
}
if (!divisibleBy8(img.imageWidth) || !divisibleBy8(img.imageHeight)) {
warnings(l10n("WARNING_BACKGROUND_NOT_MULTIPLE_OF_8"));
}
if (tilesetLength > MAX_BACKGROUND_TILES && !is360 && !cgbOnly) {
warnings(
l10n("WARNING_BACKGROUND_TOO_MANY_TILES", {
tilesetLength,
maxTilesetLength: MAX_BACKGROUND_TILES,
}),
);
}
if (tilesetLength > MAX_BACKGROUND_TILES_CGB && !is360 && cgbOnly) {
warnings(
l10n("WARNING_BACKGROUND_TOO_MANY_TILES", {
tilesetLength,
maxTilesetLength: MAX_BACKGROUND_TILES_CGB,
}),
);
}
Iif (is360 && (img.imageWidth !== 160 || img.imageHeight !== 144)) {
warnings(
l10n("WARNING_LOGO_WRONG_SIZE", {
width: img.imageWidth,
height: img.imageHeight,
}),
);
}
const vramData: [number[], number[]] = [[], []];
// Split tiles into VRAM banks based on allocation strategy
uniqueTiles.forEach((tile, i, tiles) => {
const { inVRAM2 } = tileAllocationStrategy(i, tiles.length, img);
vramData[inVRAM2 ? 1 : 0].push(...tile);
});
// Determine tilemap attrs
const attr = buildAttr(img.tileColors, autoTileColors, tilemap.length).map(
(attr, index) => {
const tile = tilemap[index];
const { inVRAM2, tileIndex } = tileAllocationStrategy(
tile,
uniqueTiles.length,
img,
);
// Reallocate tilemap based on strategy
if (tileIndex < TILE_FIRST_CHUNK_SIZE) {
tilemap[index] = tileIndex;
} else {
// tile index > 128 is allocated with an unused tile offset
// to allow as much tiles as possible for sprite data
const bankSize = vramData[inVRAM2 ? 1 : 0].length / 16;
const offset = Math.max(TILE_BANK_SIZE - bankSize, 0);
tilemap[index] = tileIndex + offset;
}
if (inVRAM2) {
return attr | FLAG_VRAM_BANK_1;
}
return attr;
},
);
return {
...img,
symbol: commonTileset
? `${img.symbol}_${commonTileset.symbol}`
: img.symbol,
commonTilesetId: commonTileset?.id,
vramData,
tilemap,
attr,
autoPalettes,
is360,
colorMode,
tilesetLength,
};
};
const compileImages = async (
imgs: ReferencedBackground[],
commonTilesetsLookup: Record<string, TilesetData[]>,
colorCorrection: ColorCorrectionSetting,
projectPath: string,
{ warnings }: CompileImageOptions,
): Promise<PrecompiledBackgroundData[]> => {
return promiseLimit(
10,
imgs
.map((img) => {
const commonTilesets = commonTilesetsLookup[img.id] ?? [];
// If this background has never been used with a common tileset
// or has been referenced without a common tileset at any point
// it's tiles should always be generated
const forceGenerateTileset =
commonTilesets.length === 0 || img.forceTilesetGeneration;
return [
// Generate background with no common tilesets applied
...(forceGenerateTileset
? [
() =>
compileImage(
img,
undefined,
img.is360,
img.uiPalette,
img.colorMode,
colorCorrection,
projectPath,
{ warnings },
),
]
: []),
// Generate background with each used common tileset
...commonTilesets.map((commonTileset) => {
return () =>
compileImage(
img,
commonTileset,
img.is360,
img.uiPalette,
img.colorMode,
colorCorrection,
projectPath,
{ warnings },
);
}),
];
})
.flat(),
);
};
export default compileImages;
|