update to zig 0.14.0 and clay 0.13
|
@ -3,7 +3,7 @@
|
||||||

|

|
||||||
|
|
||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
> Zig 0.14.0 or higher is required. (tested with zig 0.14.0-dev.3062+ff551374a)
|
> Zig 0.14.0 or higher is required
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> This project is currently in beta.
|
> This project is currently in beta.
|
||||||
|
|
30
build.zig
|
@ -6,12 +6,13 @@ pub fn build(b: *B) void {
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const clay_lib = blk: {
|
const clay_lib = blk: {
|
||||||
var target_clay = target;
|
const clay_lib = b.addLibrary(.{
|
||||||
target_clay.result.os.tag = .freestanding;
|
|
||||||
const clay_lib = b.addStaticLibrary(.{
|
|
||||||
.name = "clay",
|
.name = "clay",
|
||||||
.target = target,
|
.linkage = .static,
|
||||||
.optimize = optimize,
|
.root_module = b.createModule(.{
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const clay_dep = b.dependency("clay", .{});
|
const clay_dep = b.dependency("clay", .{});
|
||||||
|
@ -32,7 +33,6 @@ pub fn build(b: *B) void {
|
||||||
.root_source_file = b.path("src/root.zig"),
|
.root_source_file = b.path("src/root.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.link_libc = true,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module.linkLibrary(clay_lib);
|
module.linkLibrary(clay_lib);
|
||||||
|
@ -40,10 +40,11 @@ pub fn build(b: *B) void {
|
||||||
|
|
||||||
{
|
{
|
||||||
const exe_unit_tests = b.addTest(.{
|
const exe_unit_tests = b.addTest(.{
|
||||||
.root_source_file = b.path("src/root.zig"),
|
.root_module = b.createModule(.{
|
||||||
.target = target,
|
.root_source_file = b.path("src/root.zig"),
|
||||||
.optimize = optimize,
|
.target = target,
|
||||||
.link_libc = true,
|
.optimize = optimize,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
exe_unit_tests.linkLibrary(clay_lib);
|
exe_unit_tests.linkLibrary(clay_lib);
|
||||||
|
@ -55,10 +56,11 @@ pub fn build(b: *B) void {
|
||||||
|
|
||||||
{
|
{
|
||||||
const tests_check = b.addTest(.{
|
const tests_check = b.addTest(.{
|
||||||
.root_source_file = b.path("src/root.zig"),
|
.root_module = b.createModule(.{
|
||||||
.target = target,
|
.root_source_file = b.path("src/root.zig"),
|
||||||
.optimize = optimize,
|
.target = target,
|
||||||
.link_libc = true,
|
.optimize = optimize,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
tests_check.linkLibrary(clay_lib);
|
tests_check.linkLibrary(clay_lib);
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
.{
|
.{
|
||||||
.name = "zclay",
|
.name = .zclay,
|
||||||
.version = "0.0.0",
|
.version = "0.0.0",
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.clay = .{
|
.clay = .{
|
||||||
.url = "https://github.com/nicbarker/clay/archive/7a84facec968df81b9fd7dda10cee60c9bd40beb.tar.gz",
|
.url = "https://github.com/nicbarker/clay/archive/refs/tags/v0.13.tar.gz",
|
||||||
.hash = "12205b854b62ef63f65c19b979782d2332d5cc88bc7069ec158d584ba8d92d389265",
|
.hash = "1220211ff0505143a8c4768a5b85c06e25661a51893b6b00a41493f82cab6d710043",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
|
@ -15,4 +15,5 @@
|
||||||
"LICENSE",
|
"LICENSE",
|
||||||
"README.md",
|
"README.md",
|
||||||
},
|
},
|
||||||
|
.fingerprint = 0x4821f6a4906b3d12,
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
64
examples/raylib-clay-official-website/build.zig
Normal 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"));
|
||||||
|
}
|
|
@ -1,13 +1,13 @@
|
||||||
.{
|
.{
|
||||||
.name = "zig-exe-template",
|
.name = .clay_website_example,
|
||||||
.version = "0.0.0",
|
.version = "0.0.0",
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.zclay = .{
|
.zclay = .{
|
||||||
.path = "../../",
|
.path = "../../",
|
||||||
},
|
},
|
||||||
.@"raylib-zig" = .{
|
.raylib_zig = .{
|
||||||
.url = "https://github.com/Not-Nik/raylib-zig/archive/ab6f1566bcb21f98b06fbccf17c57a9c4711482b.tar.gz",
|
.url = "https://github.com/Not-Nik/raylib-zig/archive/57a8a21b486af47d62cb8ce39a0c7902019a86d2.tar.gz",
|
||||||
.hash = "12207f719c1fa181d79ff174f476281e6b13610e4430d8e5b8453b43d6e62712b45f",
|
.hash = "raylib_zig-5.6.0-dev-KE8RELUtBQD9ynf9BONdwukHlR4Ib8k_hZZUkqUPO7uJ",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
|
@ -17,4 +17,5 @@
|
||||||
"LICENSE",
|
"LICENSE",
|
||||||
"README.md",
|
"README.md",
|
||||||
},
|
},
|
||||||
|
.fingerprint = 0x5a04d39cd5b4f3dd,
|
||||||
}
|
}
|
|
@ -93,7 +93,6 @@ fn landingPageDesktop() void {
|
||||||
.border = .{ .width = .{ .left = 2, .right = 2 }, .color = COLOR_RED },
|
.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);
|
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()(.{
|
cl.UI()(.{
|
||||||
.id = .ID("ClayPresentation"),
|
.id = .ID("ClayPresentation"),
|
||||||
.layout = .{
|
.layout = .{
|
||||||
|
@ -527,18 +526,18 @@ fn createLayout(lerp_value: f32) cl.ClayArray(cl.RenderCommand) {
|
||||||
return cl.endLayout();
|
return cl.endLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn loadFont(file_data: ?[]const u8, font_id: u16, font_size: i32) void {
|
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);
|
renderer.raylib_fonts[font_id] = try rl.loadFontFromMemory(".ttf", file_data, font_size * 2, null);
|
||||||
rl.setTextureFilter(renderer.raylib_fonts[font_id].?.texture, .bilinear);
|
rl.setTextureFilter(renderer.raylib_fonts[font_id].?.texture, .bilinear);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn loadImage(comptime path: [:0]const u8) rl.Texture2D {
|
fn loadImage(comptime path: [:0]const u8) !rl.Texture2D {
|
||||||
const texture = rl.loadTextureFromImage(rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path)));
|
const texture = try rl.loadTextureFromImage(try rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path)));
|
||||||
rl.setTextureFilter(texture, .bilinear);
|
rl.setTextureFilter(texture, .bilinear);
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() !void {
|
||||||
const allocator = std.heap.page_allocator;
|
const allocator = std.heap.page_allocator;
|
||||||
|
|
||||||
// init clay
|
// init clay
|
||||||
|
@ -559,24 +558,24 @@ pub fn main() anyerror!void {
|
||||||
rl.setTargetFPS(60);
|
rl.setTargetFPS(60);
|
||||||
|
|
||||||
// load assets
|
// load assets
|
||||||
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_56, 56);
|
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_56, 56);
|
||||||
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_52, 52);
|
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_52, 52);
|
||||||
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_48, 48);
|
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_48, 48);
|
||||||
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_36, 36);
|
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_36, 36);
|
||||||
loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_32, 32);
|
try loadFont(@embedFile("resources/Calistoga-Regular.ttf"), FONT_ID_TITLE_32, 32);
|
||||||
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_36, 36);
|
try loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_36, 36);
|
||||||
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_30, 30);
|
try loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_30, 30);
|
||||||
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_28, 28);
|
try loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_28, 28);
|
||||||
loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_24, 24);
|
try 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/Quicksand-Semibold.ttf"), FONT_ID_BODY_16, 16);
|
||||||
|
|
||||||
syntaxImage = loadImage("resources/declarative.png");
|
syntaxImage = try loadImage("resources/declarative.png");
|
||||||
checkImage1 = loadImage("resources/check_1.png");
|
checkImage1 = try loadImage("resources/check_1.png");
|
||||||
checkImage2 = loadImage("resources/check_2.png");
|
checkImage2 = try loadImage("resources/check_2.png");
|
||||||
checkImage3 = loadImage("resources/check_3.png");
|
checkImage3 = try loadImage("resources/check_3.png");
|
||||||
checkImage4 = loadImage("resources/check_4.png");
|
checkImage4 = try loadImage("resources/check_4.png");
|
||||||
checkImage5 = loadImage("resources/check_5.png");
|
checkImage5 = try loadImage("resources/check_5.png");
|
||||||
zig_logo_image6 = loadImage("resources/zig-mark.png");
|
zig_logo_image6 = try loadImage("resources/zig-mark.png");
|
||||||
|
|
||||||
var animation_lerp_value: f32 = -1.0;
|
var animation_lerp_value: f32 = -1.0;
|
||||||
var debug_mode_enabled = false;
|
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);
|
var render_commands = createLayout(if (animation_lerp_value < 0) animation_lerp_value + 1 else 1 - animation_lerp_value);
|
||||||
|
|
||||||
rl.beginDrawing();
|
rl.beginDrawing();
|
||||||
renderer.clayRaylibRender(&render_commands, allocator);
|
try renderer.clayRaylibRender(&render_commands, allocator);
|
||||||
rl.endDrawing();
|
rl.endDrawing();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -14,7 +14,7 @@ pub fn clayColorToRaylibColor(color: cl.Color) rl.Color {
|
||||||
|
|
||||||
pub var raylib_fonts: [10]?rl.Font = .{null} ** 10;
|
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;
|
var i: usize = 0;
|
||||||
while (i < render_commands.length) : (i += 1) {
|
while (i < render_commands.length) : (i += 1) {
|
||||||
const render_command = cl.renderCommandArrayGet(render_commands, @intCast(i));
|
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 config = render_command.render_data.text;
|
||||||
const text = config.string_contents.chars[0..@intCast(config.string_contents.length)];
|
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
|
// 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);
|
defer allocator.free(cloned);
|
||||||
const fontToUse: rl.Font = raylib_fonts[config.font_id].?;
|
const fontToUse: rl.Font = raylib_fonts[config.font_id].?;
|
||||||
rl.setTextLineSpacing(config.line_height);
|
rl.setTextLineSpacing(config.line_height);
|
||||||
rl.drawTextEx(
|
rl.drawTextEx(
|
||||||
fontToUse,
|
fontToUse,
|
||||||
@ptrCast(@alignCast(cloned.ptr)),
|
cloned,
|
||||||
rl.Vector2{ .x = bounding_box.x, .y = bounding_box.y },
|
rl.Vector2{ .x = bounding_box.x, .y = bounding_box.y },
|
||||||
@floatFromInt(config.font_size),
|
@floatFromInt(config.font_size),
|
||||||
@floatFromInt(config.letter_spacing),
|
@floatFromInt(config.letter_spacing),
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 193 KiB After Width: | Height: | Size: 193 KiB |
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
@ -5,34 +5,28 @@ pub fn build(b: *B) void {
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
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(.{
|
const exe = b.addExecutable(.{ .name = "zclay-example", .root_module = root_module });
|
||||||
.name = "zclay-example",
|
|
||||||
.root_source_file = b.path("src/main.zig"),
|
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
|
||||||
addDependencies(exe, b, target, optimize);
|
|
||||||
|
|
||||||
b.installArtifact(exe);
|
b.installArtifact(exe);
|
||||||
const run_cmd = b.addRunArtifact(exe);
|
const run_cmd = b.addRunArtifact(exe);
|
||||||
run_cmd.step.dependOn(b.getInstallStep());
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
|
||||||
if (b.args) |args| {
|
if (b.args) |args| run_cmd.addArgs(args);
|
||||||
run_cmd.addArgs(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
const run_step = b.step("run", "Run the app");
|
const run_step = b.step("run", "Run the app");
|
||||||
run_step.dependOn(&run_cmd.step);
|
run_step.dependOn(&run_cmd.step);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const exe_unit_tests = b.addTest(.{
|
const exe_unit_tests = b.addTest(.{ .root_module = root_module });
|
||||||
.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 run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||||
const test_step = b.step("test", "Run 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(.{
|
const exe_check = b.addExecutable(.{ .name = "check", .root_module = root_module });
|
||||||
.name = "check",
|
const tests_check = b.addTest(.{ .name = "check", .root_module = root_module });
|
||||||
.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");
|
const check = b.step("check", "Check if exe and tests compile");
|
||||||
check.dependOn(&exe_check.step);
|
check.dependOn(&exe_check.step);
|
||||||
|
@ -63,7 +44,7 @@ pub fn build(b: *B) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addDependencies(
|
fn addDependencies(
|
||||||
compile_step: *B.Step.Compile,
|
module: *B.Module,
|
||||||
b: *B,
|
b: *B,
|
||||||
target: B.ResolvedTarget,
|
target: B.ResolvedTarget,
|
||||||
optimize: std.builtin.OptimizeMode,
|
optimize: std.builtin.OptimizeMode,
|
||||||
|
@ -72,15 +53,12 @@ fn addDependencies(
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
const zclay = zclay_dep.module("zclay");
|
module.addImport("zclay", zclay_dep.module("zclay"));
|
||||||
compile_step.root_module.addImport("zclay", zclay);
|
|
||||||
|
|
||||||
const raylib_dep = b.dependency("raylib-zig", .{
|
const raylib_dep = b.dependency("raylib_zig", .{
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
const raylib = raylib_dep.module("raylib");
|
module.addImport("raylib", raylib_dep.module("raylib"));
|
||||||
compile_step.root_module.addImport("raylib", raylib);
|
module.linkLibrary(raylib_dep.artifact("raylib"));
|
||||||
const raylib_artifact = raylib_dep.artifact("raylib");
|
|
||||||
compile_step.linkLibrary(raylib_artifact);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
.{
|
.{
|
||||||
.name = "zig-exe-template",
|
.name = .clay_sidebar_example,
|
||||||
.version = "0.0.0",
|
.version = "0.0.0",
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.zclay = .{
|
.zclay = .{
|
||||||
.path = "../../",
|
.path = "../../",
|
||||||
},
|
},
|
||||||
.@"raylib-zig" = .{
|
.raylib_zig = .{
|
||||||
.url = "https://github.com/Not-Nik/raylib-zig/archive/ab6f1566bcb21f98b06fbccf17c57a9c4711482b.tar.gz",
|
.url = "https://github.com/Not-Nik/raylib-zig/archive/57a8a21b486af47d62cb8ce39a0c7902019a86d2.tar.gz",
|
||||||
.hash = "12207f719c1fa181d79ff174f476281e6b13610e4430d8e5b8453b43d6e62712b45f",
|
.hash = "raylib_zig-5.6.0-dev-KE8RELUtBQD9ynf9BONdwukHlR4Ib8k_hZZUkqUPO7uJ",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
|
@ -17,4 +17,5 @@
|
||||||
"LICENSE",
|
"LICENSE",
|
||||||
"README.md",
|
"README.md",
|
||||||
},
|
},
|
||||||
|
.fingerprint = 0x934a5243274e06bb,
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,18 +65,18 @@ fn createLayout(profile_picture: *const rl.Texture2D) cl.ClayArray(cl.RenderComm
|
||||||
return cl.endLayout();
|
return cl.endLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn loadFont(file_data: ?[]const u8, font_id: u16, font_size: i32) void {
|
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);
|
renderer.raylib_fonts[font_id] = try rl.loadFontFromMemory(".ttf", file_data, font_size * 2, null);
|
||||||
rl.setTextureFilter(renderer.raylib_fonts[font_id].?.texture, .bilinear);
|
rl.setTextureFilter(renderer.raylib_fonts[font_id].?.texture, .bilinear);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn loadImage(comptime path: [:0]const u8) rl.Texture2D {
|
fn loadImage(comptime path: [:0]const u8) !rl.Texture2D {
|
||||||
const texture = rl.loadTextureFromImage(rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path)));
|
const texture = try rl.loadTextureFromImage(try rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path)));
|
||||||
rl.setTextureFilter(texture, .bilinear);
|
rl.setTextureFilter(texture, .bilinear);
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() !void {
|
||||||
const allocator = std.heap.page_allocator;
|
const allocator = std.heap.page_allocator;
|
||||||
|
|
||||||
// init clay
|
// init clay
|
||||||
|
@ -97,8 +97,8 @@ pub fn main() anyerror!void {
|
||||||
rl.setTargetFPS(120);
|
rl.setTargetFPS(120);
|
||||||
|
|
||||||
// load assets
|
// load assets
|
||||||
loadFont(@embedFile("./resources/Roboto-Regular.ttf"), 0, 24);
|
try loadFont(@embedFile("./resources/Roboto-Regular.ttf"), 0, 24);
|
||||||
const profile_picture = loadImage("./resources/profile-picture.png");
|
const profile_picture = try loadImage("./resources/profile-picture.png");
|
||||||
|
|
||||||
var debug_mode_enabled = false;
|
var debug_mode_enabled = false;
|
||||||
while (!rl.windowShouldClose()) {
|
while (!rl.windowShouldClose()) {
|
||||||
|
@ -127,7 +127,7 @@ pub fn main() anyerror!void {
|
||||||
var render_commands = createLayout(&profile_picture);
|
var render_commands = createLayout(&profile_picture);
|
||||||
|
|
||||||
rl.beginDrawing();
|
rl.beginDrawing();
|
||||||
renderer.clayRaylibRender(&render_commands, allocator);
|
try renderer.clayRaylibRender(&render_commands, allocator);
|
||||||
rl.endDrawing();
|
rl.endDrawing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ pub fn clayColorToRaylibColor(color: cl.Color) rl.Color {
|
||||||
|
|
||||||
pub var raylib_fonts: [10]?rl.Font = .{null} ** 10;
|
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;
|
var i: usize = 0;
|
||||||
while (i < render_commands.length) : (i += 1) {
|
while (i < render_commands.length) : (i += 1) {
|
||||||
const render_command = cl.renderCommandArrayGet(render_commands, @intCast(i));
|
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 config = render_command.render_data.text;
|
||||||
const text = config.string_contents.chars[0..@intCast(config.string_contents.length)];
|
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
|
// 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);
|
defer allocator.free(cloned);
|
||||||
const fontToUse: rl.Font = raylib_fonts[config.font_id].?;
|
const fontToUse: rl.Font = raylib_fonts[config.font_id].?;
|
||||||
rl.setTextLineSpacing(config.line_height);
|
rl.setTextLineSpacing(config.line_height);
|
||||||
rl.drawTextEx(
|
rl.drawTextEx(
|
||||||
fontToUse,
|
fontToUse,
|
||||||
@ptrCast(@alignCast(cloned.ptr)),
|
cloned,
|
||||||
rl.Vector2{ .x = bounding_box.x, .y = bounding_box.y },
|
rl.Vector2{ .x = bounding_box.x, .y = bounding_box.y },
|
||||||
@floatFromInt(config.font_size),
|
@floatFromInt(config.font_size),
|
||||||
@floatFromInt(config.letter_spacing),
|
@floatFromInt(config.letter_spacing),
|
||||||
|
|
25
src/root.zig
|
@ -7,10 +7,9 @@ pub extern var Clay__debugViewWidth: u32;
|
||||||
|
|
||||||
/// for direct calls to the clay c library
|
/// for direct calls to the clay c library
|
||||||
pub const cdefs = struct {
|
pub const cdefs = struct {
|
||||||
// TODO: should use @extern instead but zls does not yet support it well and that is more important
|
|
||||||
pub extern fn Clay_GetElementData(id: ElementId) ElementData;
|
pub extern fn Clay_GetElementData(id: ElementId) ElementData;
|
||||||
pub extern fn Clay_MinMemorySize() u32;
|
pub extern fn Clay_MinMemorySize() u32;
|
||||||
pub extern fn Clay_CreateArenaWithCapacityAndMemory(capacity: u32, offset: ?*anyopaque) Arena;
|
pub extern fn Clay_CreateArenaWithCapacityAndMemory(capacity: u32, memory: ?*anyopaque) Arena;
|
||||||
pub extern fn Clay_SetPointerState(position: Vector2, pointerDown: bool) void;
|
pub extern fn Clay_SetPointerState(position: Vector2, pointerDown: bool) void;
|
||||||
pub extern fn Clay_Initialize(arena: Arena, layoutDimensions: Dimensions, errorHandler: ErrorHandler) *Context;
|
pub extern fn Clay_Initialize(arena: Arena, layoutDimensions: Dimensions, errorHandler: ErrorHandler) *Context;
|
||||||
pub extern fn Clay_GetCurrentContext() *Context;
|
pub extern fn Clay_GetCurrentContext() *Context;
|
||||||
|
@ -40,8 +39,6 @@ pub const cdefs = struct {
|
||||||
pub extern fn Clay__ConfigureOpenElement(config: ElementDeclaration) void;
|
pub extern fn Clay__ConfigureOpenElement(config: ElementDeclaration) void;
|
||||||
pub extern fn Clay__OpenElement() void;
|
pub extern fn Clay__OpenElement() void;
|
||||||
pub extern fn Clay__CloseElement() void;
|
pub extern fn Clay__CloseElement() void;
|
||||||
pub extern fn Clay__StoreLayoutConfig(config: LayoutConfig) *LayoutConfig;
|
|
||||||
pub extern fn Clay__AttachId(id: ElementId) ElementId;
|
|
||||||
pub extern fn Clay__StoreTextElementConfig(config: TextElementConfig) *TextElementConfig;
|
pub extern fn Clay__StoreTextElementConfig(config: TextElementConfig) *TextElementConfig;
|
||||||
pub extern fn Clay__HashString(key: String, offset: u32, seed: u32) ElementId;
|
pub extern fn Clay__HashString(key: String, offset: u32, seed: u32) ElementId;
|
||||||
pub extern fn Clay__OpenTextElement(text: String, textConfig: *TextElementConfig) void;
|
pub extern fn Clay__OpenTextElement(text: String, textConfig: *TextElementConfig) void;
|
||||||
|
@ -164,6 +161,12 @@ pub const TextElementConfigWrapMode = enum(EnumBackingType) {
|
||||||
none = 2,
|
none = 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const TextAlignment = enum(EnumBackingType) {
|
||||||
|
left = 0,
|
||||||
|
center = 1,
|
||||||
|
right = 2,
|
||||||
|
};
|
||||||
|
|
||||||
pub const TextElementConfig = extern struct {
|
pub const TextElementConfig = extern struct {
|
||||||
color: Color = .{ 0, 0, 0, 255 },
|
color: Color = .{ 0, 0, 0, 255 },
|
||||||
font_id: u16 = 0,
|
font_id: u16 = 0,
|
||||||
|
@ -171,6 +174,7 @@ pub const TextElementConfig = extern struct {
|
||||||
letter_spacing: u16 = 0,
|
letter_spacing: u16 = 0,
|
||||||
line_height: u16 = 0,
|
line_height: u16 = 0,
|
||||||
wrap_mode: TextElementConfigWrapMode = .words,
|
wrap_mode: TextElementConfigWrapMode = .words,
|
||||||
|
alignement: TextAlignment = .left,
|
||||||
hash_string_contents: bool = false,
|
hash_string_contents: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -243,7 +247,8 @@ pub const ErrorType = enum(EnumBackingType) {
|
||||||
text_measurement_capacity_exceeded = 3,
|
text_measurement_capacity_exceeded = 3,
|
||||||
duplicate_id = 4,
|
duplicate_id = 4,
|
||||||
floating_container_parent_not_found = 5,
|
floating_container_parent_not_found = 5,
|
||||||
internal_error = 6,
|
percentage_over_1 = 6,
|
||||||
|
internal_error = 7,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ErrorData = extern struct {
|
pub const ErrorData = extern struct {
|
||||||
|
@ -299,7 +304,7 @@ pub const ElementId = extern struct {
|
||||||
pub const RenderCommand = extern struct {
|
pub const RenderCommand = extern struct {
|
||||||
bounding_box: BoundingBox,
|
bounding_box: BoundingBox,
|
||||||
render_data: RenderData,
|
render_data: RenderData,
|
||||||
user_data: *anyopaque,
|
user_data: ?*anyopaque,
|
||||||
id: u32,
|
id: u32,
|
||||||
z_index: i16,
|
z_index: i16,
|
||||||
command_type: RenderCommandType,
|
command_type: RenderCommandType,
|
||||||
|
@ -422,6 +427,11 @@ pub const ImageElementConfig = extern struct {
|
||||||
source_dimensions: Dimensions,
|
source_dimensions: Dimensions,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const ScrollRenderData = extern struct {
|
||||||
|
horizontal: bool,
|
||||||
|
vertical: bool,
|
||||||
|
};
|
||||||
|
|
||||||
pub const BorderRenderData = extern struct {
|
pub const BorderRenderData = extern struct {
|
||||||
color: Color,
|
color: Color,
|
||||||
corner_radius: CornerRadius,
|
corner_radius: CornerRadius,
|
||||||
|
@ -434,6 +444,7 @@ pub const RenderData = extern union {
|
||||||
image: ImageRenderData,
|
image: ImageRenderData,
|
||||||
custom: CustomRenderData,
|
custom: CustomRenderData,
|
||||||
border: BorderRenderData,
|
border: BorderRenderData,
|
||||||
|
scroll: ScrollRenderData,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const CustomElementConfig = extern struct {
|
pub const CustomElementConfig = extern struct {
|
||||||
|
@ -636,7 +647,7 @@ fn anytypeToAnyopaquePtr(user_data: anytype) ?*anyopaque {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn AnyopaquePtrToAnytype(T: type, userData: ?*anyopaque) T {
|
fn anyopaquePtrToAnytype(T: type, userData: ?*anyopaque) T {
|
||||||
if (T == void) {
|
if (T == void) {
|
||||||
return {};
|
return {};
|
||||||
} else if (@typeInfo(T) == .pointer) {
|
} else if (@typeInfo(T) == .pointer) {
|
||||||
|
|