aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2025-10-12 19:18:04 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2025-10-12 19:18:04 -0500
commita22bf34ccdb9ba063438f27a8f9bdb6d5bffb28c (patch)
tree23b9eab54d787553a4df8f7a4bc410fef41cdc50 /src/main.zig
parentae555d16a0a355563c7da3d95c8ebbd192f52cf2 (diff)
downloadparticle-sim-a22bf34ccdb9ba063438f27a8f9bdb6d5bffb28c.tar.gz
Quadtree optimization complete
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig55
1 files changed, 37 insertions, 18 deletions
diff --git a/src/main.zig b/src/main.zig
index 00dd477..f7ec0a0 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -5,6 +5,7 @@ const part = @import("particle.zig");
const cfg = @import("config.zig");
const img = @import("imgui.zig");
const rules = @import("rules.zig");
+const quad = @import("quad.zig");
const c = @cImport({
@cDefine("NO_FONT_AWESOME", "1");
@@ -13,11 +14,21 @@ const c = @cImport({
pub fn main() !void {
cfg.colors = cfg.customColors();
- cfg.rules = rules.ruleMatrix();
+ cfg.rules = rules.ruleMatrix(true, true);
rules.printRules(cfg.rules);
- var gpa = std.heap.GeneralPurposeAllocator(.{}){};
- defer _ = gpa.deinit();
+ var gpa = std.heap.GeneralPurposeAllocator(.{
+ // .safety = true,
+ // .thread_safe = true,
+ // .verbose_log = false,
+ }){};
+ defer {
+ const leaked = gpa.deinit();
+ if (leaked == .leak) {
+ std.debug.print("LEAKY PROGRAM\n", .{});
+ }
+ }
+ const allocator = gpa.allocator();
rl.initWindow(cfg.screenWidth, cfg.screenHeight, "Particle Simulator");
defer rl.closeWindow();
@@ -28,49 +39,57 @@ pub fn main() !void {
c.rlImGuiSetup(true);
defer c.rlImGuiShutdown();
- z.initNoContext(gpa.allocator());
+ z.initNoContext(allocator);
defer z.deinitNoContext();
const imgui_width: f32 = cfg.screenWidth / 2;
const imgui_height: f32 = cfg.screenHeight / 3;
z.setNextWindowPos(.{ .x = 0, .y = 0 });
z.setNextWindowSize(.{ .w = imgui_width, .h = imgui_height });
- var particles = try part.initParticles(gpa.allocator(), cfg.initialParticles);
- defer particles.deinit(gpa.allocator());
+ var particles = try part.initParticles(allocator, cfg.initialParticles);
+ defer particles.deinit();
+ var quadTree: quad.Quad(part.particle, cfg.quadSplitLimit) = undefined;
- const buf = try gpa.allocator().allocSentinel(u8, 128, 0);
+ const buf = try allocator.allocSentinel(u8, 128, 0);
std.mem.copyForwards(u8, buf, "Absolute File Path" ++ .{0});
- defer gpa.allocator().free(buf);
- const pool = try gpa.allocator().alloc(std.Thread, cfg.numThreads);
- defer gpa.allocator().free(pool);
+ defer allocator.free(buf);
+ const pool = try allocator.alloc(std.Thread, cfg.numThreads);
+ defer allocator.free(pool);
+ var frameCounter: u32 = 0;
while (!rl.windowShouldClose()) {
- if (particles.items(.x).len < cfg.particleCount) {
- for (0..@intCast(cfg.particleCount - @as(i32, @intCast(particles.items(.x).len)))) |_| {
+ if (particles.items.len < cfg.particleCount) {
+ for (0..@intCast(cfg.particleCount - @as(i32, @intCast(particles.items.len)))) |_| {
//std.debug.print("without this print statement it breaks on arm idk why {d}\n", .{cfg.particleCount});
_ = cfg.particleCount;
- try particles.append(gpa.allocator(), part.createParticle());
+ try particles.append(part.createParticle());
}
}
- if (particles.items(.x).len > cfg.particleCount) {
+ if (particles.items.len > cfg.particleCount) {
particles.shrinkRetainingCapacity(@intCast(cfg.particleCount));
}
+ quadTree = quad.Quad(part.particle, cfg.quadSplitLimit).init(allocator,
+ .{ .x = 0, .y = 0 }, .{ .x = rl.getScreenWidth(), .y = rl.getScreenHeight()});
+ defer quadTree.deinit();
+ for (particles.items) |p| try quadTree.insert(.{ .pos = p.pos, .data = p});
+
rl.beginDrawing();
defer rl.endDrawing();
if (rl.isKeyPressed(rl.KeyboardKey.key_q)) break;
rl.clearBackground(rl.getColor(0x1E1E2EFF));
for (pool, 0..) |*thread, i|
- thread.* = try std.Thread.spawn(.{}, part.updateVelocities, .{ &particles, i });
+ thread.* = try std.Thread.spawn(.{}, part.updateVelocities, .{ particles, quadTree, i });
for (pool) |thread|
thread.join();
+ frameCounter += 1;
+
- // part.updateVelocities(particles, cfg.rules);
- part.updatePosition(particles);
+ part.updatePosition(&particles);
part.draw(particles);
- try img.update(gpa.allocator(), buf);
+ try img.update(allocator, buf);
}
}