From a18fcb44ac116430ef399601d9233876790151bb Mon Sep 17 00:00:00 2001 From: Szymon Nowakowski Date: Tue, 22 Sep 2026 02:24:59 +0200 Subject: [PATCH] Signed coord display in HUD --- src/Player.zig | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Player.zig b/src/Player.zig index 8180925..df4e2ff 100644 --- a/src/Player.zig +++ b/src/Player.zig @@ -421,30 +421,35 @@ pub fn update(self: *Player, dt: f32, game: *Game) void { const margin: f16 = 8; const font_size: f32 = 16; + // NOTE We have to do the number formatting semi-manually, because zig's + // std fmt is weird with signed numbers + + var number_buf: [9]u8 = undefined; // sign ('-' or ' ') and 8 hex digits + gui.pushBox(.{ .background_color = .init(0, 0, 0, 0.75), .border_color = .init(0, 0, 0, 1), .position_sspx = .init(0, 0), - .size_px = .init(157, 4 * margin + 3 * font_size), + .size_px = .init(164, 4 * margin + 3 * font_size), .border_width_px = 0, .border_radius_px = 0, }); - gui.printLine("X {X:0>8}", .{@as(u32, @bitCast(self.position_sv.x))}, .{ + gui.printLine("X {s}", .{formatHexCoord(self.position_sv.x, &number_buf)}, .{ .position_sspx = .init(margin, margin + font_size), .font_id = @enumFromInt(0), .cap_height_px = font_size, .color = .init(1, 1, 1, 1), }); - gui.printLine("Y {X:0>8}", .{@as(u32, @bitCast(self.position_sv.y))}, .{ + gui.printLine("Y {s}", .{formatHexCoord(self.position_sv.y, &number_buf)}, .{ .position_sspx = .init(margin, 2 * (margin + font_size)), .font_id = @enumFromInt(0), .cap_height_px = font_size, .color = .init(1, 1, 1, 1), }); - gui.printLine("Z {X:0>8}", .{@as(u32, @bitCast(self.position_sv.z))}, .{ + gui.printLine("Z {s}", .{formatHexCoord(self.position_sv.z, &number_buf)}, .{ .position_sspx = .init(margin, 3 * (margin + font_size)), .font_id = @enumFromInt(0), .cap_height_px = font_size, @@ -508,6 +513,19 @@ pub fn update(self: *Player, dt: f32, game: *Game) void { } } +fn formatHexCoord(coord: i32, buf: *[9]u8) []const u8 { + buf[0] = ' '; + const coord_abs = @abs(coord); + _ = std.fmt.bufPrint(buf[1..9], "{X:>8}", .{coord_abs}) catch unreachable; + + if (coord < 0) { + const i = std.mem.findScalarLast(u8, buf, ' ') orelse unreachable; + buf[i] = '-'; + } + + return buf[0..]; +} + fn resetAllButtons(self: *Player) void { inline for (@typeInfo(Player).@"struct".fields) |field| { if (field.type == Button) {