All files / src/lib/compiler tileAllocation.ts

100% Statements 21/21
100% Branches 8/8
100% Functions 5/5
100% Lines 16/16

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                                      6x     20635x                       6x       7806x 5221x         2585x 1426x           1159x                         6x     275x                         6x           388x     388x 388x                         6x 128x          
import { SpriteModeSetting } from "shared/lib/resources/types";
 
type ImageTileAllocationStrategy = (
  tileIndex: number,
  numTiles: number,
) => { tileIndex: number; inVRAM2: boolean };
 
type SpriteTileAllocationStrategy = (
  tileIndex: number,
  numTiles: number,
  spriteMode: SpriteModeSetting,
) => { 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,
  };
};
 
/**
 * Allocates a sprite tile for to default DMG location.
 *
 * @param {number} tileIndex - The index of the sprite tile to allocate.
 * @param {number} numTiles - The total number of tiles available for allocation.
 * @returns {{ tileIndex: number, inVRAM2: boolean }} Updated tile index and flag which is set if tile has been reallocated to VRAM bank2.
 */
export const spriteTileAllocationDefault: SpriteTileAllocationStrategy = (
  tileIndex,
) => {
  return {
    tileIndex,
    inVRAM2: false,
  };
};
 
/**
 * Allocates a sprite tile for color-only sprites and adjusts the tile index based on VRAM bank allocation.
 *
 * @param {number} tileIndex - The index of the sprite tile to allocate.
 * @param {number} numTiles - The total number of tiles available for allocation.
 * @returns {{ tileIndex: number, inVRAM2: boolean }} Updated tile index and flag which is set if tile has been reallocated to VRAM bank2.
 */
export const spriteTileAllocationColorOnly: SpriteTileAllocationStrategy = (
  tileIndex,
  numTiles,
  spriteMode,
) => {
  const bank1NumTiles =
    spriteMode === "8x16"
      ? Math.ceil(numTiles / 4) * 2
      : Math.ceil(numTiles / 2);
  const inVRAM2 = tileIndex >= bank1NumTiles;
  return {
    tileIndex: inVRAM2 ? tileIndex - bank1NumTiles : tileIndex,
    inVRAM2: tileIndex >= bank1NumTiles,
  };
};
 
/**
 * Dummy sprite tile allocation strategy for testing purposes only allocates all sprite tiles to VRAM bank 2.
 *
 * @param {number} tileIndex - The index of the sprite tile to allocate.
 * @param {number} numTiles - The total number of tiles available for allocation.
 * @returns {{ tileIndex: number, inVRAM2: boolean }} Updated tile index and flag which is set if tile has been reallocated to VRAM bank2.
 */
export const spriteTileAllocationVRAM2Only = (tileIndex: number) => {
  return {
    tileIndex,
    inVRAM2: true,
  };
};