Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plasma effect showcase #61

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions showcase/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const root = @import("root");
const carts = .{
.{ "zeroman", @import("zeroman") },
.{ "blobs", @import("blobs") },
.{ "plasma", @import("plasma") },
.{ "metalgear_timer", @import("metalgear_timer") },
};

Expand Down
1 change: 1 addition & 0 deletions showcase/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// Carts go here
.zeroman = .{ .path = "carts/zeroman" },
.blobs = .{ .path = "carts/blobs" },
.plasma = .{ .path = "carts/plasma" },
.metalgear_timer = .{ .path = "carts/metalgear-timer" },
},
.paths = .{
Expand Down
19 changes: 19 additions & 0 deletions showcase/carts/plasma/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const std = @import("std");
const sycl_badge = @import("sycl_badge");

pub const author_name = "Fabio Arnold";
pub const author_handle = "fabioarnold";
pub const cart_title = "plasma";
pub const description = "Plasma effect - computes plasma blobs and cycles their hue";

pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const sycl_badge_dep = b.dependency("sycl_badge", .{});

const cart = sycl_badge.add_cart(sycl_badge_dep, b, .{
.name = "plasma",
.optimize = optimize,
.root_source_file = b.path("src/plasma.zig"),
});
cart.install(b);
}
12 changes: 12 additions & 0 deletions showcase/carts/plasma/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.{
.name = "plasma",
.version = "0.0.0",
.dependencies = .{
.sycl_badge = .{ .path = "../../.." },
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}
99 changes: 99 additions & 0 deletions showcase/carts/plasma/src/plasma.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const std = @import("std");
const cart = @import("cart-api");
const gfx = @import("gfx");

export fn start() void {}

export fn update() void {
updatePlasma();
}

const hue_colors = init_hue_colors: {
var initial_hue_colors: [256]cart.DisplayColor = undefined;
for (0..initial_hue_colors.len) |i| {
const fi: f32 = @floatFromInt(i);
const rgb = hsv2rgb(fi * 360.0 / 256.0, 1, 1);
initial_hue_colors[i] = .{
.r = @intFromFloat(rgb.r * 31),
.g = @intFromFloat(rgb.g * 63),
.b = @intFromFloat(rgb.b * 31),
};
}
break :init_hue_colors initial_hue_colors;
};

var plasma_buffer = init_plasma_buffer: {
@setEvalBranchQuota(100000);
var initial_plasma_buffer: [cart.framebuffer.len]u8 = undefined;
var y: usize = 0;
while (y < cart.screen_height) : (y += 1) {
var x: usize = 0;
while (x < cart.screen_width) : (x += 1) {
const fx: f32 = @floatFromInt(x);
const fy: f32 = @floatFromInt(y);
var value: f32 = @sin(fx / 16.0);
value += @sin(fy / 8.0);
value += @sin((fx + fy) / 16.0);
value += @sin(@sqrt(fx * fx + fy * fy) / 8.0);
// shift range from -4 .. 4 to 0 .. 255
value = std.math.clamp((value + 4) * 32, 0, 255);
initial_plasma_buffer[y * cart.screen_width + x] = @intFromFloat(value);
}
}
break :init_plasma_buffer initial_plasma_buffer;
};

fn updatePlasma() void {
for (0..cart.framebuffer.len) |i| {
plasma_buffer[i] +%= 2;
cart.framebuffer[i] = hue_colors[plasma_buffer[i]];
}
}

const RGB = struct {
r: f32,
g: f32,
b: f32,
};

pub fn hsv2rgb(h: f32, s: f32, v: f32) RGB {
const c = v * s;
const x = c * (1 - @abs(@mod(h / 60.0, 2) - 1));
const m = v - c;

var r: f32 = 0.0;
var g: f32 = 0.0;
var b: f32 = 0.0;

if (h < 60.0) {
r = c;
g = x;
b = 0.0;
} else if (h < 120.0) {
r = x;
g = c;
b = 0.0;
} else if (h < 180.0) {
r = 0.0;
g = c;
b = x;
} else if (h < 240.0) {
r = 0.0;
g = x;
b = c;
} else if (h < 300.0) {
r = x;
g = 0.0;
b = c;
} else {
r = c;
g = 0.0;
b = x;
}

return RGB{
.r = r + m,
.g = g + m,
.b = b + m,
};
}
Loading