diff options
| author | Gabriel Schneider <[email protected]> | 2026-07-30 23:35:45 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-08-01 15:02:07 -0300 |
| commit | 19f7322100062b7c1adcde3376063ce6c1d8c72d (patch) | |
| tree | b76347dffe0347c29a38b4cad6031678683dba74 /src/gui/crt.zig | |
| parent | 753d5451ca7d09dbc5ff44f3c2dcf9a47fb87f96 (diff) | |
| download | pardes-19f7322100062b7c1adcde3376063ce6c1d8c72d.tar.gz pardes-19f7322100062b7c1adcde3376063ce6c1d8c72d.zip | |
structure: backends into src/{tty,gui,lsp}, pane kinds and builtins into their own files
The core now lies FLAT at src/ and every subdirectory is one backend, so a
file being in no directory at all is what says it is core. Pane-kind bodies
leave pardes.zig for term_pane.zig / file_pane.zig / output_pane.zig, leaving
it the layout, the event/effect machine and the generic render loop.
Builtins are one struct each in builtins.zig, and the enum is folded out of
the file's own declaration list at comptime — a zig file IS a struct, so the
list of builtins and the builtins themselves are the same text. Adding one is
writing a struct. Key paths deliberately stay one table for the config pass.
Pure refactor: no golden moved.
Diffstat (limited to 'src/gui/crt.zig')
| -rw-r--r-- | src/gui/crt.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/gui/crt.zig b/src/gui/crt.zig new file mode 100644 index 00000000..9cf81593 --- /dev/null +++ b/src/gui/crt.zig @@ -0,0 +1,43 @@ +//! The Crt builtin's screen geometry, shared between the gui shell's input +//! mapping and its shader: crt.frag.glsl shows the scene point +//! uv' = cc*(1 + barrel*r2) at screen point cc, so a physical mouse position +//! must pass through the SAME forward transform before it becomes a cell. +//! Pure std, so the unit-test step can pin it against the shader formula. + +const std = @import("std"); + +/// MUST match BARREL in shaders/crt.frag.glsl +pub const barrel: f32 = 0.012; + +pub const Point = struct { x: f32, y: f32 }; + +/// physical window pixels -> the scene pixels the shader displays there, +/// clamped to the window. The shader's phosphor quantization and chromatic +/// offset displace at most a few pixels — well under a cell — and are +/// ignored here. +pub fn map(x: f32, y: f32, w: f32, h: f32) Point { + const ccx = x / w * 2.0 - 1.0; + const ccy = y / h * 2.0 - 1.0; + const s = 1.0 + barrel * (ccx * ccx + ccy * ccy); + return .{ + .x = std.math.clamp((ccx * s + 1.0) * 0.5 * w, 0, w - 1), + .y = std.math.clamp((ccy * s + 1.0) * 0.5 * h, 0, h - 1), + }; +} + +test "map matches the shader's barrel formula" { + // center is a fixed point + try std.testing.expectApproxEqAbs(@as(f32, 500), map(500, 250, 1000, 500).x, 0.001); + try std.testing.expectApproxEqAbs(@as(f32, 250), map(500, 250, 1000, 500).y, 0.001); + // corners: cc=(±1,±1), r2=2, scale 1.024 pushes past the tube -> clamped + try std.testing.expectEqual(@as(f32, 0), map(0, 0, 1000, 500).x); + try std.testing.expectEqual(@as(f32, 0), map(0, 0, 1000, 500).y); + try std.testing.expectEqual(@as(f32, 999), map(1000, 500, 1000, 500).x); + try std.testing.expectEqual(@as(f32, 499), map(1000, 500, 1000, 500).y); + // (100,100) of 1000x500: cc=(-0.8,-0.6), r2=1.0, s=1.012 -> + // cc'=(-0.8096,-0.6072) -> (95.2, 98.2) + try std.testing.expectApproxEqAbs(@as(f32, 95.2), map(100, 100, 1000, 500).x, 0.01); + try std.testing.expectApproxEqAbs(@as(f32, 98.2), map(100, 100, 1000, 500).y, 0.01); + // (900,250): cc=(0.8,0), r2=0.64, s=1.00768 -> x'=903.072 + try std.testing.expectApproxEqAbs(@as(f32, 903.072), map(900, 250, 1000, 500).x, 0.01); +} |
