Using implicit pointer derefs + fixing crashes on clearing entities

This commit is contained in:
2026-04-29 17:16:12 +02:00
parent 9defec8448
commit 7348861145
3 changed files with 11 additions and 10 deletions

View File

@@ -44,7 +44,7 @@ pub const NodeManager = struct {
const road: Road = .init(0, node, &cur_node);
road.draw(false);
node.*.draw(c.NODE_TEMP_COLOUR);
node.draw(c.NODE_TEMP_COLOUR);
cur_node.draw(c.NODE_CURSOR_COLOUR);
}
}
@@ -77,6 +77,7 @@ pub const NodeManager = struct {
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) !void {
self.temp_node = null;
for (self.nodes.items) |node| {
node.roads.clearRetainingCapacity();
try node.deinit(allocator);
}
self.nodes.clearRetainingCapacity();

View File

@@ -33,8 +33,8 @@ pub const Road = struct {
const start = self.nodes[0];
const end = self.nodes[1];
const x_diff = end.*.pos.x - start.*.pos.x;
const y_diff = end.*.pos.y - start.*.pos.y;
const x_diff = end.pos.x - start.pos.x;
const y_diff = end.pos.y - start.pos.y;
const square_diff = x_diff * x_diff + y_diff * y_diff;
return @sqrt(square_diff);
@@ -46,7 +46,7 @@ pub const Road = struct {
/// it will gradually become more complex
pub fn draw(self: *const Road, highlighted: bool) void {
const colour = if (highlighted) c.ROAD_HIGHLIGHTED_COLOUR else c.ROAD_COLOUR;
rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, colour);
rl.drawLineEx(self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE, colour);
}
/// Important: after this function executes, this road is no longer reachable from its bounding nodes
@@ -59,7 +59,7 @@ pub const Road = struct {
/// Checks whether pos coordinate is on the referenced road
pub fn isHighlighted(self: *const Road, pos: rl.Vector2) bool {
return rl.checkCollisionPointLine(pos, self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE);
return rl.checkCollisionPointLine(pos, self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE);
}
};
@@ -74,7 +74,7 @@ test "valid road nodes" {
const start: Node = .init(34, .{.x = 500, .y = 500});
const start_ptr = try allocator.create(Node);
defer {
start_ptr.*.deinit(allocator);
start_ptr.deinit(allocator);
allocator.destroy(start_ptr);
}
start_ptr.* = start;
@@ -82,7 +82,7 @@ test "valid road nodes" {
const end: Node = .init(227, .{.x = 600, .y = 500});
const end_ptr = try allocator.create(Node);
defer {
end_ptr.*.deinit(allocator);
end_ptr.deinit(allocator);
allocator.destroy(end_ptr);
}
end_ptr.* = end;

View File

@@ -90,7 +90,7 @@ pub const Simulator = struct {
};
if (self.node_man.temp_node) |temp| {
if (temp.*.id == cur_node.*.id) return;
if (temp.id == cur_node.id) return;
self.road_man.addRoad(self.allocator, temp, cur_node) catch |err| {
std.debug.panic("Failed to add a new road or assigning its nodes: {}\n", .{err});
};
@@ -107,8 +107,8 @@ pub const Simulator = struct {
if (self.road_man.highlighted_road == null) return;
const h_road = self.road_man.highlighted_road.?;
const start_node = h_road.*.nodes[0];
const end_node = h_road.*.nodes[1];
const start_node = h_road.nodes[0];
const end_node = h_road.nodes[1];
self.road_man.deleteRoad(self.allocator, h_road) catch |err| {
std.debug.panic("Road deletion failed: {}\n", .{err});