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

forked from benjivers/val

workbenchthis machine
Files 2e870ef8994867709476cca0f461c0ff071a8c69
41 lines · 1.9 KB · ~563 tok Raw History
1
/**
2
 * How big the world is drawn, before it is scaled onto the screen.
3
 *
4
 * A raycaster does not need device pixels. Every column is a ray marched
5
 * through the grid, so the cost of the frame is the width of the canvas, and
6
 * a retina window asks for three thousand of them — 1512 rays and as many
7
 * fills at every frame, sixty times a second, for a picture made of flat
8
 * shaded columns that upscales almost invisibly.
9
 *
10
 * So the world is drawn into an offscreen canvas 960 columns wide however
11
 * big the window is, and blitted up; the HUD, the crosshair and the minimap
12
 * stay on the visible canvas at full resolution, where sharpness actually
13
 * shows. Fidelity is cheap here and frame rate is not: this game can be
14
 * stick figures if that is what runs.
15
 *
16
 * The ladder is the knob the frame governor turns: a rung coarser is about a
17
 * fifth fewer rays, and the rungs are in order of what they cost, so going
18
 * down one can never cost more. Nothing here touches what is sent, predicted
19
 * or simulated — it is a count of columns out of a canvas size and a level.
20
 */
21
22
/** Internal widths, finest first. The first rung is what every window gets. */
23
export const WIDTHS = [960, 800, 672, 544, 448];
24
25
const clamp = (n, low, high) => (n < low ? low : n > high ? high : n);
26
27
/**
28
 * The size to draw the world at: never wider than the canvas actually has
29
 * pixels for — upscaling a 448px render onto a 360px phone would be paying
30
 * for columns nobody can see — and never so small it stops being a picture.
31
 * The aspect is the canvas's own, so the blit only ever scales.
32
 */
33
export function renderSize(deviceWidth, deviceHeight, level) {
34
  const want = WIDTHS[clamp(level | 0, 0, WIDTHS.length - 1)];
35
  const width = Math.max(240, Math.min(want, Math.round(deviceWidth) || 240));
36
  const height = Math.max(
37
    1,
38
    Math.round(width * (deviceHeight > 0 && deviceWidth > 0 ? deviceHeight / deviceWidth : 0.6)),
39
  );
40
  return { width, height };
41
}
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts