From a17d7e6d41f0b7a81590411db591e9cb9f363905 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 3 Nov 2022 03:34:07 -0300 Subject: [PATCH] fix(window): proper inRange impl for window window wrap now works (it's pretty slow though?) --- src/core/ppu.zig | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/core/ppu.zig b/src/core/ppu.zig index d7135ee..2d54185 100644 --- a/src/core/ppu.zig +++ b/src/core/ppu.zig @@ -962,24 +962,35 @@ const Window = struct { } fn inRange(self: *const Self, comptime id: u1, x: u9, y: u8) bool { - const h = self.h[id]; - const v = self.v[id]; + const winh = self.h[id]; + const winv = self.v[id]; - const y1 = v.y1.read(); - const y2 = if (y1 > v.y2.read()) 160 else std.math.min(160, v.y2.read()); - - if (y1 <= y and y < y2) { - // Within Y bounds - const x1 = h.x1.read(); - const x2 = if (x1 > h.x2.read()) 240 else std.math.min(240, h.x2.read()); + if (isYInRange(winv, y)) { + const x1 = winh.x1.read(); + const x2 = winh.x2.read(); // Within X Bounds - return x1 <= x and x < x2; + return if (x1 < x2) blk: { + break :blk x >= x1 and x < x2; + } else blk: { + break :blk x >= x1 or x < x2; + }; } return false; } + inline fn isYInRange(winv: io.WinV, y: u9) bool { + const y1 = winv.y1.read(); + const y2 = winv.y2.read(); + + if (y1 < y2) { + return y >= y1 and y < y2; + } else { + return y >= y1 or y < y2; + } + } + pub fn setH(self: *Self, value: u32) void { self.h[0].raw = @truncate(u16, value); self.h[1].raw = @truncate(u16, value >> 16);