Signed coord display in HUD

This commit is contained in:
2026-09-22 02:24:59 +02:00
parent 609321af2a
commit a18fcb44ac

View File

@@ -421,30 +421,35 @@ pub fn update(self: *Player, dt: f32, game: *Game) void {
const margin: f16 = 8; const margin: f16 = 8;
const font_size: f32 = 16; 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(.{ gui.pushBox(.{
.background_color = .init(0, 0, 0, 0.75), .background_color = .init(0, 0, 0, 0.75),
.border_color = .init(0, 0, 0, 1), .border_color = .init(0, 0, 0, 1),
.position_sspx = .init(0, 0), .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_width_px = 0,
.border_radius_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), .position_sspx = .init(margin, margin + font_size),
.font_id = @enumFromInt(0), .font_id = @enumFromInt(0),
.cap_height_px = font_size, .cap_height_px = font_size,
.color = .init(1, 1, 1, 1), .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)), .position_sspx = .init(margin, 2 * (margin + font_size)),
.font_id = @enumFromInt(0), .font_id = @enumFromInt(0),
.cap_height_px = font_size, .cap_height_px = font_size,
.color = .init(1, 1, 1, 1), .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)), .position_sspx = .init(margin, 3 * (margin + font_size)),
.font_id = @enumFromInt(0), .font_id = @enumFromInt(0),
.cap_height_px = font_size, .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 { fn resetAllButtons(self: *Player) void {
inline for (@typeInfo(Player).@"struct".fields) |field| { inline for (@typeInfo(Player).@"struct".fields) |field| {
if (field.type == Button) { if (field.type == Button) {