All files / player-browser/src/media lrx-bga-draw.ts

100% Statements 17/17
100% Branches 25/25
100% Functions 4/4
100% Lines 17/17

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                                6x 6x 6x                                           7x   1x   1x   1x   1x   3x                               2x 2x 2x 2x 2x           8x 8x 8x      
/**
 * Pure pieces of the LRX BGA compositor (`lrx-bga-compositor.ts`): where a note rotates about, and how it blends.
 */
 
/**
 * The point a note rotates about, from its `ctr` (plugout's numeric-keypad layout: 7 top left, 8 top, 9 top right, 4
 * left, 5 or 0 the middle, 6 right, 1 bottom left, 2 bottom, 3 bottom right) on its destination rectangle.
 */
export function resolveLRXBGAPivot(
  center: number,
  x: number,
  y: number,
  w: number,
  h: number,
): readonly [number, number] {
  const column =
    center === 1 || center === 4 || center === 7 ? 0 : center === 3 || center === 6 || center === 9 ? 1 : 0.5;
  const row = center === 7 || center === 8 || center === 9 ? 0 : center === 1 || center === 2 || center === 3 ? 1 : 0.5;
  return [x + w * column, y + h * row];
}
 
/** A WebGL blend state: the equation and the source and destination factors, as WebGL names them. */
export interface LRXBGABlend {
  readonly equation: 'FUNC_ADD' | 'FUNC_REVERSE_SUBTRACT';
  readonly source: 'ONE' | 'SRC_ALPHA' | 'DST_COLOR';
  readonly destination: 'ONE_MINUS_SRC_ALPHA' | 'ONE_MINUS_SRC_COLOR' | 'SRC_COLOR';
}
 
/**
 * The blend state a note's `bld` draws with, as plugout's Direct3D 9 driver sets it for a 2D image (`plugout4.exe`
 * 0x592c5c, `D3DRS_BLENDOP` / `SRCBLEND` / `DESTBLEND`). The source is the picture times the note's colour, alpha
 * included (texture stage 0 modulates both):
 *
 * - 1 (Add): `src + dst × (1 − src)`, the source's alpha unused.
 * - 2 (Sub): `dst × (1 − src) − src × α`, a reverse subtraction clamped at 0.
 * - 3 (Mul): `src × α + dst × src`.
 * - 4: `src × dst + dst × (1 − α)`.
 * - Anything else, 0 (Alpha) among them: `src × α + dst × (1 − α)`.
 */
export function resolveLRXBGABlend(blend: number): LRXBGABlend {
  switch (blend) {
    case 1:
      return { equation: 'FUNC_ADD', source: 'ONE', destination: 'ONE_MINUS_SRC_COLOR' };
    case 2:
      return { equation: 'FUNC_REVERSE_SUBTRACT', source: 'SRC_ALPHA', destination: 'ONE_MINUS_SRC_COLOR' };
    case 3:
      return { equation: 'FUNC_ADD', source: 'SRC_ALPHA', destination: 'SRC_COLOR' };
    case 4:
      return { equation: 'FUNC_ADD', source: 'DST_COLOR', destination: 'ONE_MINUS_SRC_ALPHA' };
    default:
      return { equation: 'FUNC_ADD', source: 'SRC_ALPHA', destination: 'ONE_MINUS_SRC_ALPHA' };
  }
}
 
/**
 * The four corners of a destination rectangle turned by `degrees` (clockwise on the y-down screen) about the pivot:
 * top left, top right, bottom left, bottom right.
 */
export function resolveLRXBGAQuad(
  x: number,
  y: number,
  w: number,
  h: number,
  pivot: readonly [number, number],
  degrees: number,
): readonly [number, number][] {
  const radians = (degrees * Math.PI) / 180;
  const cos = Math.cos(radians);
  const sin = Math.sin(radians);
  const [px, py] = pivot;
  return [
    [x, y],
    [x + w, y],
    [x, y + h],
    [x + w, y + h],
  ].map(([cx, cy]) => {
    const dx = cx! - px;
    const dy = cy! - py;
    return [px + dx * cos - dy * sin, py + dx * sin + dy * cos] as [number, number];
  });
}