aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 4d21dae7bb0f64e6491165afbd7633e80bf02e09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const std = @import("std");
const rl = @import("raylib");
const z = @import("zgui");
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");
    @cInclude("rlImGui.h");
});

pub fn main() !void {
    cfg.colors = cfg.customColors();
    cfg.rules = rules.ruleMatrix(true, true);
    rules.printRules(cfg.rules);

    const allocator = std.heap.smp_allocator;
    // defer {
    //     const leaked = smp.deinit();
    //     if (leaked == .leak) {
    //         std.debug.print("LEAKY PROGRAM\n", .{});
    //     }
    // }
    // gpa.setRequestedMemoryLimit(8000000);
    // const allocator = smp.allocator();
    // var buffer: [80000000]u8 = undefined;
    // var fbuffer = std.heap.FixedBufferAllocator.init(&buffer);
    // const allocator = fbuffer.threadSafeAllocator();

    rl.initWindow(cfg.screenWidth, cfg.screenHeight, "Particle Simulator");
    defer rl.closeWindow();

    rl.setTargetFPS(60);
    rl.setWindowState(rl.ConfigFlags{ .window_resizable = true });

    c.rlImGuiSetup(true);
    defer c.rlImGuiShutdown();

    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(allocator, cfg.initialParticles);
    defer particles.deinit();
    var quadTree: quad.Quad(part.particle, cfg.quadSplitLimit) = undefined;
    // defer quadTree.deinit();

    const buf = try allocator.allocSentinel(u8, 128, 0);
    std.mem.copyForwards(u8, buf, "Absolute File Path" ++ .{0});
    defer allocator.free(buf);
    const pool = try allocator.alloc(std.Thread, cfg.numThreads);
    var particleArrs: [cfg.numThreads]std.ArrayList(part.particle) = undefined;
    for (0..cfg.numThreads) |i|
        particleArrs[i] = try std.ArrayList(part.particle).initCapacity(allocator, comptime @divFloor(cfg.particleMax, cfg.numThreads) + cfg.numThreads);
    defer allocator.free(pool);

    while (!rl.windowShouldClose()) {
        if (particles.items.len < cfg.particleCount) {
            for (0..@intCast(cfg.particleCount - @as(i32, @intCast(particles.items.len)))) |_| {
                _ = cfg.particleCount;
                try particles.append(part.createParticle());
            }
        }

        if (particles.items.len > cfg.particleCount) {
            particles.shrinkRetainingCapacity(@intCast(cfg.particleCount));
        }


        quadTree = try 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.q)) {
            quadTree.deinit();
            break;
        }
        rl.clearBackground(rl.getColor(0x1E1E2EFF));

        for (pool, 0..) |*thread, i| {
            thread.* = try std.Thread.spawn(.{}, part.updateVelocities, .{ particles, quadTree, i, &particleArrs[i] });
        }

        for (pool) |thread|
            thread.join();
        quadTree.deinit();


        part.updatePosition(&particles);
        part.draw(particles);
        try img.update(allocator, buf);
    }
}