All files / src/lib/sprites readSpriteData.ts

8.89% Statements 8/90
0% Branches 0/11
0% Functions 0/2
7.23% Lines 6/83

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 1992x 2x 2x                     2x                           2x                                                     2x                                                                                                                                                                                                                                                                                                
import { TILE_SIZE } from "consts";
import { readFileToIndexedImage } from "lib/tiles/readFileToTiles";
import {
  OptimisedTile,
  blitIndexedImageData,
  indexedImageTo2bppSpriteData,
  indexedUnknownToTransparent,
  isBlankIndexedImage,
  isIndexedImageEqual,
  mergeIndexedImages,
  removeIndexedImageMask,
  spriteDataIndexFn,
} from "shared/lib/sprites/spriteData";
import {
  IndexedImage,
  flipIndexedImageX,
  flipIndexedImageY,
  makeIndexedImage,
  sliceIndexedImage,
} from "shared/lib/tiles/indexedImage";
import type { MetaspriteTile } from "shared/lib/entities/entitiesTypes";
 
/**
 * Read an image filename into a GB 2bpp data array
 * @param filename Tiles image filename
 * @returns Uint8Array of 2bpp tile data
 */
export const readFileToSpriteTilesData = async (
  filename: string
): Promise<Uint8Array> => {
  const img = await readFileToIndexedImage(filename, spriteDataIndexFn);
  const xTiles = Math.floor(img.width / TILE_SIZE);
  const yTiles = Math.floor(img.height / TILE_SIZE);
  const size = xTiles * yTiles * 16;
  const output = new Uint8Array(size);
  let index = 0;
  for (let txi = 0; txi < xTiles; txi++) {
    for (let tyi = 0; tyi < yTiles; tyi++) {
      const tileData = indexedImageTo2bppSpriteData(
        sliceIndexedImage(
          img,
          txi * TILE_SIZE,
          tyi * TILE_SIZE,
          TILE_SIZE,
          TILE_SIZE
        )
      );
      output.set(tileData, index);
      index += tileData.length;
    }
  }
  return output;
};
 
export const optimiseTiles = async (
  filename: string,
  spriteWidth: number,
  spriteHeight: number,
  metasprites: MetaspriteTile[][]
): Promise<{
  tiles: IndexedImage[];
  lookup: Record<string, OptimisedTile | undefined>;
}> => {
  const tileLookup: Record<string, number> = {};
  const allTiles: IndexedImage[] = [];
  const uniqTiles: IndexedImage[] = [];
  const uniqTileData: IndexedImage[] = [];
  const tileIds: string[] = [];
  const optimisedLookup2: Record<string, OptimisedTile | undefined> = {};
  const indexedImage = await readFileToIndexedImage(
    filename,
    spriteDataIndexFn
  );
 
  for (const myTiles of metasprites) {
    let mask = makeIndexedImage(spriteWidth, spriteHeight);
    for (let ti = myTiles.length - 1; ti >= 0; ti--) {
      const tileDef = myTiles[ti];
      let slicedTile = sliceIndexedImage(
        indexedImage,
        tileDef.sliceX,
        tileDef.sliceY,
        8,
        16
      );
      Iif (tileDef.flipX) {
        slicedTile = flipIndexedImageX(slicedTile);
      }
      Iif (tileDef.flipY) {
        slicedTile = flipIndexedImageY(slicedTile);
      }
 
      const visibleTile = removeIndexedImageMask(
        slicedTile,
        mask,
        spriteWidth / 2 - 8 + tileDef.x,
        spriteHeight - 16 - tileDef.y
      );
 
      mask = blitIndexedImageData(
        mask,
        slicedTile,
        spriteWidth / 2 - 8 + tileDef.x,
        spriteHeight - 16 - tileDef.y
      );
 
      tileLookup[tileDef.id] = allTiles.length;
      allTiles.push(visibleTile);
      tileIds.push(tileDef.id);
    }
  }
 
  for (let i = 0; i < allTiles.length; i++) {
    let found = false;
    const tile = allTiles[i];
 
    Iif (isBlankIndexedImage(tile)) {
      // If tile is empty (e.g. completely obscured)
      // then don't add to unique tiles
      const id = tileIds[i];
      optimisedLookup2[id] = undefined;
      continue;
    }
 
    for (let ui = 0; ui < uniqTiles.length; ui++) {
      const uniqTile = uniqTiles[ui];
      const tileFX = flipIndexedImageX(tile);
      const tileFY = flipIndexedImageY(tile);
      const tileFXY = flipIndexedImageX(flipIndexedImageY(tile));
 
      if (isIndexedImageEqual(tile, uniqTile)) {
        found = true;
        const id = tileIds[i];
        uniqTiles[ui] = mergeIndexedImages(uniqTile, tile);
        optimisedLookup2[id] = {
          tile: ui * 2,
          flipX: false,
          flipY: false,
        };
        break;
      } else if (isIndexedImageEqual(tileFX, uniqTile)) {
        found = true;
        const id = tileIds[i];
        uniqTiles[ui] = mergeIndexedImages(uniqTile, tileFX);
        optimisedLookup2[id] = {
          tile: ui * 2,
          flipX: true,
          flipY: false,
        };
        break;
      } else if (isIndexedImageEqual(tileFY, uniqTile)) {
        found = true;
        const id = tileIds[i];
        uniqTiles[ui] = mergeIndexedImages(uniqTile, tileFY);
        optimisedLookup2[id] = {
          tile: ui * 2,
          flipX: false,
          flipY: true,
        };
        break;
      } else Iif (isIndexedImageEqual(tileFXY, uniqTile)) {
        found = true;
        const id = tileIds[i];
        uniqTiles[ui] = mergeIndexedImages(uniqTile, tileFXY);
        optimisedLookup2[id] = {
          tile: ui * 2,
          flipX: true,
          flipY: true,
        };
        break;
      }
    }
 
    Iif (!found) {
      const id = tileIds[i];
      optimisedLookup2[id] = {
        tile: uniqTiles.length * 2,
        flipX: false,
        flipY: false,
      };
      uniqTiles.push(tile);
    }
  }
 
  for (const tile of uniqTiles) {
    uniqTileData.push(
      indexedUnknownToTransparent(sliceIndexedImage(tile, 0, 0, 8, 8))
    );
    uniqTileData.push(
      indexedUnknownToTransparent(sliceIndexedImage(tile, 0, 8, 8, 8))
    );
  }
 
  return {
    tiles: uniqTileData,
    lookup: optimisedLookup2,
  };
};