fable/val Public Public: anyone can read this; nobody can change it.
forked from benjivers/val
workbenchthis machine
Files 03302de7b925b15e6e7d266e9a2ca7c5b4a55296
| 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, 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, 0); |
| 12 | expect(size.width).toBe(960); |
| 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 come out of the width, and this is |
| 16 | // three times fewer of them. |
| 17 | expect(3024 / size.width).toBeGreaterThan(3); |
| 18 | }); |
| 19 | |
| 20 | test("every window starts at the same width: the canvas size is not the knob", () => { |
| 21 | expect(renderSize(1400, 887, 0).width).toBe(960); |
| 22 | expect(renderSize(2400, 1520, 0).width).toBe(960); |
| 23 | expect(renderSize(5120, 3240, 0).width).toBe(960); |
| 24 | }); |
| 25 | |
| 26 | test("the world is never drawn wider than the canvas has pixels for", () => { |
| 27 | // A phone: 360 CSS pixels at dpr 3 is a 1080px canvas, and a level whose |
| 28 | // rung is wider than that would be paying for columns nobody can see. |
| 29 | expect(renderSize(1080, 684, 0).width).toBe(960); |
| 30 | expect(renderSize(720, 456, 0).width).toBe(720); |
| 31 | expect(renderSize(600, 380, 0).width).toBe(600); |
| 32 | expect(renderSize(320, 203, 0).width).toBe(320); |
| 33 | }); |
| 34 | |
| 35 | test("every rung is coarser than the one before, and the ladder ends", () => { |
| 36 | const widths = WIDTHS.map((_, i) => renderSize(4000, 2500, i).width); |
| 37 | expect(widths).toEqual(WIDTHS); |
| 38 | for (let i = 1; i < widths.length; i++) expect(widths[i]!).toBeLessThan(widths[i - 1]!); |
| 39 | // Past the end it holds at the coarsest rung rather than vanishing. |
| 40 | expect(renderSize(4000, 2500, 99).width).toBe(WIDTHS[WIDTHS.length - 1]); |
| 41 | expect(renderSize(4000, 2500, -3).width).toBe(WIDTHS[0]); |
| 42 | }); |
| 43 | |
| 44 | test("a size is always drawable: no zero, no NaN, whatever it is handed", () => { |
| 45 | for (const [w, h] of [[0, 0], [NaN, 100], [-10, -10], [10, 0]] as [number, number][]) { |
| 46 | const size = renderSize(w, h, 0); |
| 47 | expect(Number.isFinite(size.width)).toBe(true); |
| 48 | expect(Number.isFinite(size.height)).toBe(true); |
| 49 | expect(size.width).toBeGreaterThan(0); |
| 50 | expect(size.height).toBeGreaterThan(0); |
| 51 | } |
| 52 | }); |