Implemented that each road, node in managers will be stored as pointers (allocated in heap)
This commit is contained in:
@@ -4,7 +4,7 @@ const clr = @import("raylib").Color;
|
||||
pub const WIDTH = 1366;
|
||||
/// Screen Height
|
||||
pub const HEIGHT = 768;
|
||||
pub const BACKGROUND_COLOR = clr.dark_gray;
|
||||
pub const BACKGROUND_COLOR = clr.light_gray;
|
||||
pub const ALLOC_SIZE = 50;
|
||||
|
||||
/// Base node radius
|
||||
|
||||
@@ -7,7 +7,7 @@ const Road = @import("road.zig").Road;
|
||||
|
||||
pub const NodeManager = struct {
|
||||
next_id: usize,
|
||||
nodes: std.ArrayList(Node),
|
||||
nodes: std.ArrayList(*Node),
|
||||
temp_node: ?*Node,
|
||||
|
||||
pub fn init() NodeManager {
|
||||
@@ -19,8 +19,9 @@ pub const NodeManager = struct {
|
||||
}
|
||||
|
||||
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void {
|
||||
for (self.nodes.items) |*node| {
|
||||
for (self.nodes.items) |node| {
|
||||
node.deinit(allocator);
|
||||
allocator.destroy(node);
|
||||
}
|
||||
self.nodes.deinit(allocator);
|
||||
}
|
||||
@@ -43,15 +44,17 @@ pub const NodeManager = struct {
|
||||
}
|
||||
|
||||
pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node {
|
||||
for (self.nodes.items) |*node| {
|
||||
for (self.nodes.items) |node| {
|
||||
if (node.posWithinRadius(pos)) return node;
|
||||
}
|
||||
|
||||
// No node is within that position, so we must create a new one
|
||||
const node: Node = .init(self.getNextID(), pos);
|
||||
try self.nodes.append(allocator, node);
|
||||
const node_ptr = try allocator.create(Node);
|
||||
node_ptr.* = node;
|
||||
try self.nodes.append(allocator, node_ptr);
|
||||
|
||||
return &self.nodes.items[self.nodes.items.len - 1];
|
||||
return self.nodes.items[self.nodes.items.len - 1];
|
||||
}
|
||||
|
||||
fn getNextID(self: *NodeManager) usize {
|
||||
@@ -63,8 +66,9 @@ pub const NodeManager = struct {
|
||||
|
||||
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
|
||||
self.temp_node = null;
|
||||
for (self.nodes.items) |*node| {
|
||||
for (self.nodes.items) |node| {
|
||||
node.deinit(allocator);
|
||||
allocator.destroy(node);
|
||||
}
|
||||
self.nodes.clearRetainingCapacity();
|
||||
self.next_id = 0;
|
||||
|
||||
@@ -4,7 +4,7 @@ const Node = @import("node.zig").Node;
|
||||
|
||||
pub const RoadManager = struct {
|
||||
next_id: usize,
|
||||
roads: std.ArrayList(Road),
|
||||
roads: std.ArrayList(*Road),
|
||||
|
||||
pub fn init() RoadManager {
|
||||
return .{
|
||||
@@ -14,6 +14,9 @@ pub const RoadManager = struct {
|
||||
}
|
||||
|
||||
pub fn deinit(self: *RoadManager, allocator: std.mem.Allocator) void {
|
||||
for (self.roads.items) |road| {
|
||||
allocator.destroy(road);
|
||||
}
|
||||
self.roads.deinit(allocator);
|
||||
}
|
||||
|
||||
@@ -25,9 +28,11 @@ pub const RoadManager = struct {
|
||||
|
||||
pub fn addRoad(self: *RoadManager, allocator: std.mem.Allocator, start: *Node, end: *Node) !void {
|
||||
const road: Road = .init(self.getNextID(), start, end);
|
||||
try self.roads.append(allocator, road);
|
||||
const road_ptr = try allocator.create(Road);
|
||||
road_ptr.* = road;
|
||||
try self.roads.append(allocator, road_ptr);
|
||||
|
||||
const ref = &self.roads.items[self.roads.items.len - 1];
|
||||
const ref = self.roads.items[self.roads.items.len - 1];
|
||||
try start.referenceRoad(allocator, ref);
|
||||
try end.referenceRoad(allocator, ref);
|
||||
}
|
||||
@@ -39,7 +44,10 @@ pub const RoadManager = struct {
|
||||
return id;
|
||||
}
|
||||
|
||||
pub fn clear(self: *RoadManager) void {
|
||||
pub fn clear(self: *RoadManager, allocator: std.mem.Allocator) void {
|
||||
for (self.roads.items) |road| {
|
||||
allocator.destroy(road);
|
||||
}
|
||||
self.roads.clearRetainingCapacity();
|
||||
self.next_id = 0;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ const c = @import("constants.zig");
|
||||
const Simulator = @import("simulator.zig").Simulator;
|
||||
|
||||
// TODO PLANS
|
||||
// Implement that roads, nodes in managers will be stored as pointers (allocated in heap)
|
||||
// Implement that each road, node in managers will be stored as pointers (allocated in heap)
|
||||
// So when appending causes the list to relocate, nothing will have to change
|
||||
// since the allocated nodes/roads will remain where they were
|
||||
// Additionally add more robust error and test handling
|
||||
|
||||
@@ -56,6 +56,7 @@ pub const Simulator = struct {
|
||||
};
|
||||
|
||||
if (self.node_man.temp_node) |temp| {
|
||||
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});
|
||||
};
|
||||
@@ -68,7 +69,7 @@ pub const Simulator = struct {
|
||||
}
|
||||
|
||||
fn clear(self: *Simulator) void {
|
||||
self.road_man.clear();
|
||||
self.road_man.clear(self.allocator);
|
||||
self.node_man.clear(self.allocator);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user