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

@ -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),