wren/val Public Public: anyone can read this; nobody can change it.

forked from benjivers/val

workbenchthis machine
Files 51e1b3de2c93c9d19656cbce7937019e452ac771
1
/**
2
 * The internal render size, as arithmetic. A retina window is the case that
3
 * started this: 1512 CSS pixels across is a 3024px canvas, and the renderer
4
 * was marching a ray for every other one of them.
5
 */
6
7
import { expect, test } from "bun:test";
8
import { renderSize, startLevel, WIDTHS } from "../static/resolution.js";
9
10
test("a retina window draws the world at a fixed width, not at its pixels", () => {
11
  const size = renderSize(3024, 1915, startLevel(1512));
12
  expect(size.width).toBe(1280);
13
  // The picture keeps the canvas's shape, so the blit only ever scales.
14
  expect(size.height / size.width).toBeCloseTo(1915 / 3024, 3);
15
  // Which is what the frame costs: rays are the width, and this is 2.4x fewer.
16
  expect(3024 / size.width).toBeGreaterThan(2.3);
17
});
18
19
test("a narrower window starts narrower still", () => {
20
  expect(renderSize(1400, 887, startLevel(700)).width).toBe(960);
21
  expect(startLevel(1200)).toBe(0);
22
  expect(startLevel(1199)).toBe(2);
23
});
24
25
test("the world is never drawn wider than the canvas has pixels for", () => {
26
  // A phone: 360 CSS pixels at dpr 3 is a 1080px canvas, and a level whose
27
  // rung is wider than that would be paying for columns nobody can see.
28
  expect(renderSize(1080, 684, startLevel(360)).width).toBe(960);
29
  expect(renderSize(600, 380, 0).width).toBe(600);
30
  expect(renderSize(320, 203, 0).width).toBe(320);
31
});
32
33
test("every rung is coarser than the one before, and the ladder ends", () => {
34
  const widths = WIDTHS.map((_, i) => renderSize(4000, 2500, i).width);
35
  expect(widths).toEqual(WIDTHS);
36
  for (let i = 1; i < widths.length; i++) expect(widths[i]!).toBeLessThan(widths[i - 1]!);
37
  // Past the end it holds at the coarsest rung rather than vanishing.
38
  expect(renderSize(4000, 2500, 99).width).toBe(WIDTHS[WIDTHS.length - 1]);
39
  expect(renderSize(4000, 2500, -3).width).toBe(WIDTHS[0]);
40
});
41
42
test("a size is always drawable: no zero, no NaN, whatever it is handed", () => {
43
  for (const [w, h] of [[0, 0], [NaN, 100], [-10, -10], [10, 0]] as [number, number][]) {
44
    const size = renderSize(w, h, 0);
45
    expect(Number.isFinite(size.width)).toBe(true);
46
    expect(Number.isFinite(size.height)).toBe(true);
47
    expect(size.width).toBeGreaterThan(0);
48
    expect(size.height).toBeGreaterThan(0);
49
  }
50
});
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts