aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--build.zig50
-rw-r--r--build.zig.zon36
-rw-r--r--src/main.zig82
4 files changed, 170 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ee7098f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+zig-out/
+zig-cache/
diff --git a/build.zig b/build.zig
new file mode 100644
index 0000000..e1266e6
--- /dev/null
+++ b/build.zig
@@ -0,0 +1,50 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+ const target = b.standardTargetOptions(.{});
+
+ const optimize = b.standardOptimizeOption(.{});
+
+ const exe = b.addExecutable(.{
+ .name = "particle-sim",
+ .root_source_file = b.path("src/main.zig"),
+ .target = target,
+ .optimize = optimize,
+ });
+
+ b.installArtifact(exe);
+
+ const run_cmd = b.addRunArtifact(exe);
+
+ run_cmd.step.dependOn(b.getInstallStep());
+
+ if (b.args) |args| {
+ run_cmd.addArgs(args);
+ }
+
+ const run_step = b.step("run", "Run the app");
+ run_step.dependOn(&run_cmd.step);
+
+ const exe_unit_tests = b.addTest(.{
+ .root_source_file = b.path("src/main.zig"),
+ .target = target,
+ .optimize = optimize,
+ });
+
+ const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
+
+ const test_step = b.step("test", "Run unit tests");
+ test_step.dependOn(&run_exe_unit_tests.step);
+
+ const raylib_dep = b.dependency("raylib-zig", .{
+ .target = target,
+ .optimize = optimize,
+ });
+
+ const raylib = raylib_dep.module("raylib"); // main raylib module
+ const raygui = raylib_dep.module("raygui"); // raygui module
+ const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library
+ exe.linkLibrary(raylib_artifact);
+ exe.root_module.addImport("raylib", raylib);
+ exe.root_module.addImport("raygui", raygui);
+}
diff --git a/build.zig.zon b/build.zig.zon
new file mode 100644
index 0000000..93588c2
--- /dev/null
+++ b/build.zig.zon
@@ -0,0 +1,36 @@
+.{
+ .name = "particle-sim",
+ // This is a [Semantic Version](https://semver.org/).
+ // In a future version of Zig it will be used for package deduplication.
+ .version = "0.0.0",
+
+ // This field is optional.
+ // This is currently advisory only; Zig does not yet do anything
+ // with this value.
+ //.minimum_zig_version = "0.11.0",
+
+ // This field is optional.
+ // Each dependency must either provide a `url` and `hash`, or a `path`.
+ // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
+ // Once all dependencies are fetched, `zig build` no longer requires
+ // internet connectivity.
+ .dependencies = .{
+ .@"raylib-zig" = .{
+ .url = "https://github.com/Not-Nik/raylib-zig/archive/devel.tar.gz",
+ .hash = "12204a223b19043e17b79300413d02f60fc8004c0d9629b8d8072831e352a78bf212",
+ },
+ },
+ .paths = .{
+ // This makes *all* files, recursively, included in this package. It is generally
+ // better to explicitly list the files and directories instead, to insure that
+ // fetching from tarballs, file system paths, and version control all result
+ // in the same contents hash.
+ "",
+ // For example...
+ //"build.zig",
+ //"build.zig.zon",
+ //"src",
+ //"LICENSE",
+ //"README.md",
+ },
+}
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..f021181
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,82 @@
+const std = @import("std");
+const rl = @import("raylib");
+var gpa = std.heap.GeneralPurposeAllocator(.{}){};
+
+const particle = struct {
+ color: rl.Color,
+ attrs: particleAttrs,
+ x: i32,
+ y: i32,
+ xvel: i32,
+ yvel: i32,
+};
+
+const particleAttrs = struct {};
+const screenWidth = 1920;
+const screenHeight = 1080;
+
+pub fn main() !void {
+ rl.initWindow(screenWidth, screenHeight, "Particle Simulator");
+ defer rl.closeWindow();
+
+ rl.setTargetFPS(60);
+
+ var particles = try initParticles(200);
+ defer particles.deinit();
+ defer {
+ switch (gpa.deinit()) {
+ std.heap.Check.leak => std.debug.print("\n\nLEAKS!!!!\n\n", .{}),
+ std.heap.Check.ok => std.debug.print("No leaks :3", .{}),
+ }
+ }
+ std.debug.print("{}\n", .{particles.items.len});
+
+ while (!rl.windowShouldClose()) {
+ rl.beginDrawing();
+ if (rl.isKeyPressed(rl.KeyboardKey.key_q)) break;
+
+ defer rl.endDrawing();
+
+ for (0..particles.items.len) |i| {
+ updateParticle(&particles.items[i]);
+ drawParticle(particles.items[i]);
+ }
+
+ rl.clearBackground(rl.Color.white);
+ }
+}
+
+inline fn drawParticle(p: particle) void {
+ rl.drawCircle(p.x, p.y, 1, p.color);
+}
+
+fn updateParticle(p: *particle) void {
+ p.x = @mod((p.x + p.xvel), screenWidth);
+ p.y = @mod((p.y + p.yvel), screenHeight);
+}
+
+fn createParticle(x: i32, y: i32, color: rl.Color, attrs: particleAttrs) particle {
+ return particle{
+ .color = color,
+ .attrs = attrs,
+ .x = x,
+ .y = y,
+ .xvel = 5,
+ .yvel = -7,
+ };
+}
+
+fn initParticles(amnt: u32) !std.ArrayList(particle) {
+ const colors = [_]rl.Color{ rl.Color.red, rl.Color.blue, rl.Color.green };
+ const seed = @as(u64, @truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))));
+ var prng = std.rand.DefaultPrng.init(seed);
+
+ var particles = std.ArrayList(particle).init(gpa.allocator());
+ for (0..amnt) |_| {
+ const x = prng.random().uintLessThan(u32, screenWidth);
+ const y = prng.random().uintLessThan(u32, screenHeight);
+ const color = colors[prng.random().uintLessThan(u32, 3)];
+ try particles.append(createParticle(@intCast(x), @intCast(y), color, .{}));
+ }
+ return particles;
+}