import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; import { applyMagentaTintToImageData } from "/shared/render/tint.js"; export const UTILITY_IMAGES = [ { name: "night-shade", url: shadeDataUrl("#000000") }, { name: "chunk-edge", url: shadeDataUrl("#e23d3d") }, { name: "spawn-range", url: shadeDataUrl("rgba(72, 180, 210, 0.32)") }, { name: "wander-range", url: shadeDataUrl("rgba(210, 170, 72, 0.22)") } ]; export function playImagesFromCatalog (catalog) { return [...(catalog?.images ?? []), ...UTILITY_IMAGES]; } export async function loadSpriteCatalog () { const response = await fetch("/assets/catalog.json"); if (!response.ok) { throw new Error("Failed to load sprite catalog."); } return response.json(); } /** * Load catalog images without aborting the whole client when one file is missing. * Failed entries are logged and omitted from `game.images`. */ export async function preloadCatalogImages (game, catalog) { const failed = []; await Promise.all(playImagesFromCatalog(catalog).map((imageData) => loadOneImage(game, imageData).catch((error) => { console.error(error.message ?? error); failed.push({ name: imageData.name, url: imageData.url }); }))); if (failed.length) { console.warn(`Skipped ${failed.length} missing/broken sprite(s).`, failed.map((entry) => entry.name)); } return failed; } function loadOneImage (game, imageData) { return new Promise((resolve, reject) => { const img = new Image(); img.onload = () => { const source = imageData.tint ? tintedHtmlImage(img, imageData.tint) : img; const finalImage = new image( source, imageData.subImgTotal, imageData.perRow, imageData.loop, imageData.chain ); finalImage.name = imageData.name; game.images[imageData.name] = finalImage; resolve(finalImage); }; img.onerror = () => { reject(new Error(`Failed to load image '${imageData.name}' from ${imageData.url}`)); }; img.src = imageData.url; }); } function tintedHtmlImage (sourceImg, tintRgb) { const canvas = document.createElement("canvas"); canvas.width = sourceImg.naturalWidth || sourceImg.width; canvas.height = sourceImg.naturalHeight || sourceImg.height; const ctx = canvas.getContext("2d"); ctx.imageSmoothingEnabled = false; ctx.drawImage(sourceImg, 0, 0); const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); applyMagentaTintToImageData(imageData, tintRgb); ctx.putImageData(imageData, 0, 0); return canvas; } export function applySpriteSheets (images, catalog) { for (const entry of catalog?.images ?? []) { const sprite = images[entry.name]; if (!sprite) { continue; } sprite._animationSpeed = entry.animationSpeed ?? 0; const frames = entry.subImgTotal || 1; if (frames > 1) { sprite.addAnimationCycle( "idle", Array.from({ length: frames }, (_, index) => index), true ); } } } export function shadeDataUrl (fill = "#000000") { const canvas = document.createElement("canvas"); canvas.width = SPRITE_SIZE_PX; canvas.height = SPRITE_SIZE_PX; const context = canvas.getContext("2d"); context.fillStyle = fill; context.fillRect(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX); return canvas.toDataURL("image/png"); } function setGlNearest (gl, texture) { if (!gl || !texture) { return; } gl.bindTexture(gl.TEXTURE_2D, texture); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); } /** Framework defaults to LINEAR; patch uploads so pixel art stays crisp when scaled. */ export function patchNearestNeighbourTextures (scene) { const glContext = scene?.glContext; if (!glContext || glContext._nearestPatched) { return; } const original = glContext.getTexture.bind(glContext); glContext.getTexture = (htmlImage) => { const texture = original(htmlImage); if (texture) { setGlNearest(glContext.gl, texture); } return texture; }; glContext._nearestPatched = true; } /** Canvas atlases and dynamic sprites — call after invalidate/reupload. */ export function setNearestFilter (scene, source) { if (!source) { return; } patchNearestNeighbourTextures(scene); const glContext = scene?.glContext; if (!glContext) { return; } const htmlImage = source.g_image ?? source; const texture = glContext.getTexture(htmlImage); if (texture) { setGlNearest(glContext.gl, texture); } } export function applyNearestToAllTextures (scene, images) { patchNearestNeighbourTextures(scene); const glContext = scene?.glContext; if (!glContext) { return; } for (const sprite of Object.values(images ?? {})) { const htmlImage = sprite?.g_image ?? sprite; if (htmlImage) { glContext.getTexture(htmlImage); } } } export function useNearestNeighbour (scene, images) { applyNearestToAllTextures(scene, images); }