aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: f7ec0a04e4d10a021c4e21522017dbdfbb051f4d (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
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);

    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();

    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;

    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);
    defer allocator.free(pool);
    var frameCounter: u32 = 0;

    while (!rl.windowShouldClose()) {
        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(part.createParticle());
            }
        }

        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, quadTree, i });

        for (pool) |thread|
            thread.join();
        frameCounter += 1;


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