Implemented a few debugging/inspecting tools

This commit is contained in:
2026-04-08 11:22:19 +02:00
parent 64463e82b2
commit fea5a0ea5f
5 changed files with 59 additions and 18 deletions

View File

@@ -43,13 +43,7 @@ pub const Node = struct {
/// Returns bool whether the road passed is part of the roads list /// Returns bool whether the road passed is part of the roads list
fn roadInList(self: *const Node, road_to_check: *const Road) ?usize { fn roadInList(self: *const Node, road_to_check: *const Road) ?usize {
for (self.roads.items, 0..) |road, i| { for (self.roads.items, 0..) |road, i| {
std.debug.print("Road id = {d}\n", .{road.id});
std.debug.print("Road to check id = {d}\n", .{road_to_check.id});
// TODO fix why doesn't 0 == 0 return i
std.debug.print("{}\n", .{road.id == road_to_check.id});
const res = road.id == road_to_check.id;
if (road.id == road_to_check.id) return i; if (road.id == road_to_check.id) return i;
if (res) return i;
} }
return null; return null;

View File

@@ -7,11 +7,13 @@ const Node = @import("node.zig").Node;
pub const NodeManager = struct { pub const NodeManager = struct {
temp_node: ?*Node, temp_node: ?*Node,
nodes: std.ArrayList(Node), nodes: std.ArrayList(Node),
highlighted_node: ?*Node,
pub fn init(allocator: std.mem.Allocator) !NodeManager { pub fn init(allocator: std.mem.Allocator) !NodeManager {
return .{ return .{
.temp_node = null, .temp_node = null,
.nodes = try .initCapacity(allocator, c.ALLOC_SIZE), .nodes = try .initCapacity(allocator, c.ALLOC_SIZE),
.highlighted_node = null,
}; };
} }
@@ -91,7 +93,7 @@ pub const NodeManager = struct {
} }
/// Removes the node from the list with all appropriate checks /// Removes the node from the list with all appropriate checks
pub fn removeNode(self: *NodeManager, node_to_remove: *Node) !void { pub fn remove(self: *NodeManager, node_to_remove: *Node) !void {
// In case the node has references to the existing roads we can not remove it // In case the node has references to the existing roads we can not remove it
// This also means we don't have to deinit it, since it has no elements // This also means we don't have to deinit it, since it has no elements
if (node_to_remove.roads.items.len > 0) return; if (node_to_remove.roads.items.len > 0) return;
@@ -105,4 +107,6 @@ pub const NodeManager = struct {
return error.NodeNotExist; return error.NodeNotExist;
} }
}; };

View File

@@ -21,9 +21,7 @@ pub const RoadManager = struct {
self.roads.deinit(allocator); self.roads.deinit(allocator);
} }
pub fn draw(self: *RoadManager, display_highlighted_road: bool) void { pub fn draw(self: *const RoadManager, display_highlighted_road: bool) void {
self.highlighted_road = self.getSelectedRoad(rl.getMousePosition());
for (self.roads.items) |road| { for (self.roads.items) |road| {
var colour: rl.Color = .black; var colour: rl.Color = .black;
if (self.highlighted_road) |hr| { if (self.highlighted_road) |hr| {

View File

@@ -28,6 +28,7 @@ pub fn main() !void {
rl.beginDrawing(); rl.beginDrawing();
defer rl.endDrawing(); defer rl.endDrawing();
sim.update();
sim.handleInput(); sim.handleInput();
sim.draw(); sim.draw();

View File

@@ -66,6 +66,7 @@ pub const Simulator = struct {
.b => .BUILD, .b => .BUILD,
else => self.mode, else => self.mode,
}; };
if (self.mode != .BUILD) self.cancelBuildingRoad();
} }
/// every mouse event is checked here /// every mouse event is checked here
@@ -76,10 +77,7 @@ pub const Simulator = struct {
self.leftClickEvent(pos); self.leftClickEvent(pos);
} }
else if (self.mode == .BUILD and rl.isMouseButtonReleased(.right)) { else if (self.mode == .BUILD and rl.isMouseButtonReleased(.right)) {
if (self.node_man.temp_node) |node| { self.cancelBuildingRoad();
// todo proper error handling
self.node_man.removeNode(node) catch {};
}
} }
else if (self.mode == .DELETE and rl.isMouseButtonReleased(.left) and self.road_man.highlighted_road != null) { else if (self.mode == .DELETE and rl.isMouseButtonReleased(.left) and self.road_man.highlighted_road != null) {
const nodes = self.road_man.highlighted_road.?.nodes; const nodes = self.road_man.highlighted_road.?.nodes;
@@ -87,9 +85,12 @@ pub const Simulator = struct {
std.debug.panic("Failed to remove the road: {}\n", .{err}); std.debug.panic("Failed to remove the road: {}\n", .{err});
}; };
// todo proper error handling self.node_man.remove(nodes[0]) catch |err| {
self.node_man.removeNode(nodes[0]) catch {}; std.debug.panic("Failed to remove the first node of the road to be deleted: {}\n", .{err});
self.node_man.removeNode(nodes[1]) catch {}; };
self.node_man.remove(nodes[1]) catch |err| {
std.debug.panic("Failed to remove the second node of the road to be deleted: {}\n", .{err});
};
} }
} }
@@ -111,8 +112,17 @@ pub const Simulator = struct {
} }
} }
fn cancelBuildingRoad(self: *Simulator) void {
if (self.node_man.temp_node == null) return;
self.node_man.remove(self.node_man.temp_node.?) catch |err| {
std.debug.panic("Node doesn't exist: {}\n", .{err});
};
self.node_man.temp_node = null;
}
/// The main drawing function that is exposed upwards /// The main drawing function that is exposed upwards
pub fn draw(self: *Simulator) void { pub fn draw(self: *const Simulator) void {
self.road_man.draw(self.mode == .DELETE); self.road_man.draw(self.mode == .DELETE);
self.node_man.draw(self.display_details); self.node_man.draw(self.display_details);
self.drawUI(); self.drawUI();
@@ -147,6 +157,13 @@ pub const Simulator = struct {
// Displays the mode the simulation is currently in // Displays the mode the simulation is currently in
rl.drawText(@tagName(self.mode), 10, c.HEIGHT - c.TEXT_SIZE, c.TEXT_SIZE, .black); rl.drawText(@tagName(self.mode), 10, c.HEIGHT - c.TEXT_SIZE, c.TEXT_SIZE, .black);
// displays id of the element we hover over over
const entityInfo = self.getHighlightedEntityInfo() catch |err| {
std.debug.panic("Failed to capture highlighted entity info: {}\n", .{err});
};
rl.drawText(entityInfo, c.WIDTH - (c.TEXT_SIZE / 2) * @as(i32, @intCast(entityInfo.len)), c.TEXT_SIZE, c.TEXT_SIZE, .black);
} }
/// Gets list of pointers of all roads that 'collide' with the road bounded by the nodes we pass into it /// Gets list of pointers of all roads that 'collide' with the road bounded by the nodes we pass into it
@@ -170,4 +187,31 @@ pub const Simulator = struct {
if (self.node_man.getNodeWithinRadius(intersection.point) == null) try self.debug_intersection_data.append(self.allocator, intersection); if (self.node_man.getNodeWithinRadius(intersection.point) == null) try self.debug_intersection_data.append(self.allocator, intersection);
} }
} }
// TODO verify pointers
// TODO display road/node id
fn verifyPointerIntegrity() void {
}
pub fn update(self: *Simulator) void {
const pos = rl.getMousePosition();
self.road_man.highlighted_road = self.road_man.getSelectedRoad(pos);
self.node_man.highlighted_node = self.node_man.getNodeWithinRadius(pos);
}
/// Get ID info of the highlighted info and returns it
fn getHighlightedEntityInfo(self: *const Simulator) ![:0]u8 {
var buf: [10 * 1024]u8 = undefined;
if (self.node_man.highlighted_node) |node|
return std.fmt.bufPrintZ(&buf, "Node ID: {d}\nRoad refs: {d}", .{node.id, node.roads.items.len});
if (self.road_man.highlighted_road) |road|
return std.fmt.bufPrintZ(&buf, "Road ID: {d}", .{road.id});
return std.fmt.bufPrintZ(&buf, "", .{});
}
}; };