update to zig 0.14.0 and clay 0.13

This commit is contained in:
johan0A 2025-03-06 22:13:31 +01:00
parent c26ef9d4b6
commit dff7f01dcd
22 changed files with 167 additions and 196 deletions

View file

@ -1,86 +0,0 @@
const std = @import("std");
const B = std.Build;
pub fn build(b: *B) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
{
const exe = b.addExecutable(.{
.name = "zclay-example",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(exe, b, target, 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,
});
addDependencies(exe_unit_tests, b, target, 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 exe_check = b.addExecutable(.{
.name = "check",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(exe_check, b, target, optimize);
const tests_check = b.addTest(.{
.name = "check",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(tests_check, b, target, optimize);
const check = b.step("check", "Check if exe and tests compile");
check.dependOn(&exe_check.step);
check.dependOn(&tests_check.step);
}
}
fn addDependencies(
compile_step: *B.Step.Compile,
b: *B,
target: B.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) void {
const zclay_dep = b.dependency("zclay", .{
.target = target,
.optimize = optimize,
});
const zclay = zclay_dep.module("zclay");
compile_step.root_module.addImport("zclay", zclay);
const raylib_dep = b.dependency("raylib-zig", .{
.target = target,
.optimize = optimize,
});
const raylib = raylib_dep.module("raylib");
compile_step.root_module.addImport("raylib", raylib);
const raylib_artifact = raylib_dep.artifact("raylib");
compile_step.linkLibrary(raylib_artifact);
}

View file

@ -0,0 +1,64 @@
const std = @import("std");
const B = std.Build;
pub fn build(b: *B) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(root_module, b, target, optimize);
{
const exe = b.addExecutable(.{ .name = "zclay-example", .root_module = root_module });
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_module = root_module });
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 exe_check = b.addExecutable(.{ .name = "check", .root_module = root_module });
const tests_check = b.addTest(.{ .name = "check", .root_module = root_module });
const check = b.step("check", "Check if exe and tests compile");
check.dependOn(&exe_check.step);
check.dependOn(&tests_check.step);
}
}
fn addDependencies(
module: *B.Module,
b: *B,
target: B.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) void {
const zclay_dep = b.dependency("zclay", .{
.target = target,
.optimize = optimize,
});
module.addImport("zclay", zclay_dep.module("zclay"));
const raylib_dep = b.dependency("raylib_zig", .{
.target = target,
.optimize = optimize,
});
module.addImport("raylib", raylib_dep.module("raylib"));
module.linkLibrary(raylib_dep.artifact("raylib"));
}

View file

@ -1,13 +1,13 @@
.{
.name = "zig-exe-template",
.name = .clay_website_example,
.version = "0.0.0",
.dependencies = .{
.zclay = .{
.path = "../../",
},
.@"raylib-zig" = .{
.url = "https://github.com/Not-Nik/raylib-zig/archive/ab6f1566bcb21f98b06fbccf17c57a9c4711482b.tar.gz",
.hash = "12207f719c1fa181d79ff174f476281e6b13610e4430d8e5b8453b43d6e62712b45f",
.raylib_zig = .{
.url = "https://github.com/Not-Nik/raylib-zig/archive/57a8a21b486af47d62cb8ce39a0c7902019a86d2.tar.gz",
.hash = "raylib_zig-5.6.0-dev-KE8RELUtBQD9ynf9BONdwukHlR4Ib8k_hZZUkqUPO7uJ",
},
},
.paths = .{
@ -17,4 +17,5 @@
"LICENSE",
"README.md",
},
.fingerprint = 0x5a04d39cd5b4f3dd,
}

View file

@ -93,7 +93,6 @@ fn landingPageDesktop() void {
.border = .{ .width = .{ .left = 2, .right = 2 }, .color = COLOR_RED },
})({
landingPageBlob(0, 30, FONT_ID_BODY_30, COLOR_ZIG_LOGO, 64, 510, "The official Clay website recreated with zclay: clay-zig-bindings", &zig_logo_image6);
cl.UI()(.{
.id = .ID("ClayPresentation"),
.layout = .{
@ -527,18 +526,18 @@ fn createLayout(lerp_value: f32) cl.ClayArray(cl.RenderCommand) {
return cl.endLayout();
}
fn loadFont(file_data: ?[]const u8, font_id: u16, font_size: i32) void {
renderer.raylib_fonts[font_id] = rl.loadFontFromMemory(".ttf", file_data, font_size * 2, null);
fn loadFont(file_data: ?[]const u8, font_id: u16, font_size: i32) !void {
renderer.raylib_fonts[font_id] = try rl.loadFontFromMemory(".ttf", file_data, font_size * 2, null);
rl.setTextureFilter(renderer.raylib_fonts[font_id].?.texture, .bilinear);
}
fn loadImage(comptime path: [:0]const u8) rl.Texture2D {
const texture = rl.loadTextureFromImage(rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path)));
fn loadImage(comptime path: [:0]const u8) !rl.Texture2D {
const texture = try rl.loadTextureFromImage(try rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path)));
rl.setTextureFilter(texture, .bilinear);
return texture;
}
pub fn main() anyerror!void {
pub fn main() !void {
const allocator = std.heap.page_allocator;
// init clay
@ -559,24 +558,24 @@ pub fn main() anyerror!void {
rl.setTargetFPS(60);
// load assets
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_56, 56);
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_52, 52);
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_48, 48);
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_36, 36);
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_32, 32);
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_36, 36);
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_30, 30);
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_28, 28);
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_24, 24);
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_16, 16);
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_56, 56);
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_52, 52);
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_48, 48);
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_36, 36);
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_32, 32);
try loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_36, 36);
try loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_30, 30);
try loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_28, 28);
try loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_24, 24);
try loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_16, 16);
syntaxImage = loadImage("resources/declarative.png");
checkImage1 = loadImage("resources/check_1.png");
checkImage2 = loadImage("resources/check_2.png");
checkImage3 = loadImage("resources/check_3.png");
checkImage4 = loadImage("resources/check_4.png");
checkImage5 = loadImage("resources/check_5.png");
zig_logo_image6 = loadImage("resources/zig-mark.png");
syntaxImage = try loadImage("resources/declarative.png");
checkImage1 = try loadImage("resources/check_1.png");
checkImage2 = try loadImage("resources/check_2.png");
checkImage3 = try loadImage("resources/check_3.png");
checkImage4 = try loadImage("resources/check_4.png");
checkImage5 = try loadImage("resources/check_5.png");
zig_logo_image6 = try loadImage("resources/zig-mark.png");
var animation_lerp_value: f32 = -1.0;
var debug_mode_enabled = false;
@ -615,7 +614,7 @@ pub fn main() anyerror!void {
var render_commands = createLayout(if (animation_lerp_value < 0) animation_lerp_value + 1 else 1 - animation_lerp_value);
rl.beginDrawing();
renderer.clayRaylibRender(&render_commands, allocator);
try renderer.clayRaylibRender(&render_commands, allocator);
rl.endDrawing();
}
}

View file

@ -14,7 +14,7 @@ pub fn clayColorToRaylibColor(color: cl.Color) rl.Color {
pub var raylib_fonts: [10]?rl.Font = .{null} ** 10;
pub fn clayRaylibRender(render_commands: *cl.ClayArray(cl.RenderCommand), allocator: std.mem.Allocator) void {
pub fn clayRaylibRender(render_commands: *cl.ClayArray(cl.RenderCommand), allocator: std.mem.Allocator) !void {
var i: usize = 0;
while (i < render_commands.length) : (i += 1) {
const render_command = cl.renderCommandArrayGet(render_commands, @intCast(i));
@ -25,13 +25,13 @@ pub fn clayRaylibRender(render_commands: *cl.ClayArray(cl.RenderCommand), alloca
const config = render_command.render_data.text;
const text = config.string_contents.chars[0..@intCast(config.string_contents.length)];
// Raylib uses standard C strings so isn't compatible with cheap slices, we need to clone the string to append null terminator
const cloned = allocator.dupeZ(u8, text) catch unreachable;
const cloned = try allocator.dupeZ(u8, text);
defer allocator.free(cloned);
const fontToUse: rl.Font = raylib_fonts[config.font_id].?;
rl.setTextLineSpacing(config.line_height);
rl.drawTextEx(
fontToUse,
@ptrCast(@alignCast(cloned.ptr)),
cloned,
rl.Vector2{ .x = bounding_box.x, .y = bounding_box.y },
@floatFromInt(config.font_size),
@floatFromInt(config.letter_spacing),

View file

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 193 KiB

After

Width:  |  Height:  |  Size: 193 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Before After
Before After

View file

@ -5,34 +5,28 @@ pub fn build(b: *B) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(root_module, b, target, optimize);
{
const exe = b.addExecutable(.{
.name = "zclay-example",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(exe, b, target, optimize);
const exe = b.addExecutable(.{ .name = "zclay-example", .root_module = root_module });
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
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,
});
addDependencies(exe_unit_tests, b, target, optimize);
const exe_unit_tests = b.addTest(.{ .root_module = root_module });
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
@ -40,21 +34,8 @@ pub fn build(b: *B) void {
}
{
const exe_check = b.addExecutable(.{
.name = "check",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(exe_check, b, target, optimize);
const tests_check = b.addTest(.{
.name = "check",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(tests_check, b, target, optimize);
const exe_check = b.addExecutable(.{ .name = "check", .root_module = root_module });
const tests_check = b.addTest(.{ .name = "check", .root_module = root_module });
const check = b.step("check", "Check if exe and tests compile");
check.dependOn(&exe_check.step);
@ -63,7 +44,7 @@ pub fn build(b: *B) void {
}
fn addDependencies(
compile_step: *B.Step.Compile,
module: *B.Module,
b: *B,
target: B.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
@ -72,15 +53,12 @@ fn addDependencies(
.target = target,
.optimize = optimize,
});
const zclay = zclay_dep.module("zclay");
compile_step.root_module.addImport("zclay", zclay);
module.addImport("zclay", zclay_dep.module("zclay"));
const raylib_dep = b.dependency("raylib-zig", .{
const raylib_dep = b.dependency("raylib_zig", .{
.target = target,
.optimize = optimize,
});
const raylib = raylib_dep.module("raylib");
compile_step.root_module.addImport("raylib", raylib);
const raylib_artifact = raylib_dep.artifact("raylib");
compile_step.linkLibrary(raylib_artifact);
module.addImport("raylib", raylib_dep.module("raylib"));
module.linkLibrary(raylib_dep.artifact("raylib"));
}

View file

@ -1,13 +1,13 @@
.{
.name = "zig-exe-template",
.name = .clay_sidebar_example,
.version = "0.0.0",
.dependencies = .{
.zclay = .{
.path = "../../",
},
.@"raylib-zig" = .{
.url = "https://github.com/Not-Nik/raylib-zig/archive/ab6f1566bcb21f98b06fbccf17c57a9c4711482b.tar.gz",
.hash = "12207f719c1fa181d79ff174f476281e6b13610e4430d8e5b8453b43d6e62712b45f",
.raylib_zig = .{
.url = "https://github.com/Not-Nik/raylib-zig/archive/57a8a21b486af47d62cb8ce39a0c7902019a86d2.tar.gz",
.hash = "raylib_zig-5.6.0-dev-KE8RELUtBQD9ynf9BONdwukHlR4Ib8k_hZZUkqUPO7uJ",
},
},
.paths = .{
@ -17,4 +17,5 @@
"LICENSE",
"README.md",
},
.fingerprint = 0x934a5243274e06bb,
}

View file

@ -65,18 +65,18 @@ fn createLayout(profile_picture: *const rl.Texture2D) cl.ClayArray(cl.RenderComm
return cl.endLayout();
}
fn loadFont(file_data: ?[]const u8, font_id: u16, font_size: i32) void {
renderer.raylib_fonts[font_id] = rl.loadFontFromMemory(".ttf", file_data, font_size * 2, null);
fn loadFont(file_data: ?[]const u8, font_id: u16, font_size: i32) !void {
renderer.raylib_fonts[font_id] = try rl.loadFontFromMemory(".ttf", file_data, font_size * 2, null);
rl.setTextureFilter(renderer.raylib_fonts[font_id].?.texture, .bilinear);
}
fn loadImage(comptime path: [:0]const u8) rl.Texture2D {
const texture = rl.loadTextureFromImage(rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path)));
fn loadImage(comptime path: [:0]const u8) !rl.Texture2D {
const texture = try rl.loadTextureFromImage(try rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path)));
rl.setTextureFilter(texture, .bilinear);
return texture;
}
pub fn main() anyerror!void {
pub fn main() !void {
const allocator = std.heap.page_allocator;
// init clay
@ -97,8 +97,8 @@ pub fn main() anyerror!void {
rl.setTargetFPS(120);
// load assets
loadFont(@embedFile("./resources/Roboto-Regular.ttf"), 0, 24);
const profile_picture = loadImage("./resources/profile-picture.png");
try loadFont(@embedFile("./resources/Roboto-Regular.ttf"), 0, 24);
const profile_picture = try loadImage("./resources/profile-picture.png");
var debug_mode_enabled = false;
while (!rl.windowShouldClose()) {
@ -127,7 +127,7 @@ pub fn main() anyerror!void {
var render_commands = createLayout(&profile_picture);
rl.beginDrawing();
renderer.clayRaylibRender(&render_commands, allocator);
try renderer.clayRaylibRender(&render_commands, allocator);
rl.endDrawing();
}
}

View file

@ -14,7 +14,7 @@ pub fn clayColorToRaylibColor(color: cl.Color) rl.Color {
pub var raylib_fonts: [10]?rl.Font = .{null} ** 10;
pub fn clayRaylibRender(render_commands: *cl.ClayArray(cl.RenderCommand), allocator: std.mem.Allocator) void {
pub fn clayRaylibRender(render_commands: *cl.ClayArray(cl.RenderCommand), allocator: std.mem.Allocator) !void {
var i: usize = 0;
while (i < render_commands.length) : (i += 1) {
const render_command = cl.renderCommandArrayGet(render_commands, @intCast(i));
@ -25,13 +25,13 @@ pub fn clayRaylibRender(render_commands: *cl.ClayArray(cl.RenderCommand), alloca
const config = render_command.render_data.text;
const text = config.string_contents.chars[0..@intCast(config.string_contents.length)];
// Raylib uses standard C strings so isn't compatible with cheap slices, we need to clone the string to append null terminator
const cloned = allocator.dupeZ(u8, text) catch unreachable;
const cloned = try allocator.dupeZ(u8, text);
defer allocator.free(cloned);
const fontToUse: rl.Font = raylib_fonts[config.font_id].?;
rl.setTextLineSpacing(config.line_height);
rl.drawTextEx(
fontToUse,
@ptrCast(@alignCast(cloned.ptr)),
cloned,
rl.Vector2{ .x = bounding_box.x, .y = bounding_box.y },
@floatFromInt(config.font_size),
@floatFromInt(config.letter_spacing),