103 lines
3.1 KiB
Zig
103 lines
3.1 KiB
Zig
const std = @import("std");
|
|
|
|
const zon = @import("build.zig.zon");
|
|
|
|
pub fn build(b: *std.Build) !void {
|
|
const target = b.standardTargetOptions(.{});
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
const llvm = b.option(bool, "llvm", "Use LLVM and LLD") orelse false;
|
|
|
|
const media_dep = b.dependency("media", .{
|
|
.target = target,
|
|
});
|
|
|
|
const vecmath_dep = b.dependency("vecmath", .{
|
|
.target = target,
|
|
});
|
|
|
|
const vulkan_dep = b.dependency("vulkan_zig", .{
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.registry = b.path("vendor/vk.xml"),
|
|
});
|
|
|
|
const zglfw_dep = b.dependency("zglfw", .{
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.import_vulkan = true,
|
|
});
|
|
|
|
const media_mod = media_dep.module("media");
|
|
const vecmath_mod = vecmath_dep.module("vecmath");
|
|
const vulkan_mod = vulkan_dep.module("vulkan-zig");
|
|
const zglfw_mod = zglfw_dep.module("root");
|
|
|
|
zglfw_mod.addImport("vulkan", vulkan_mod);
|
|
|
|
const zglfw_lib = zglfw_dep.artifact("glfw");
|
|
|
|
const exe_mod = b.createModule(.{
|
|
.root_source_file = b.path("src/main.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
});
|
|
|
|
exe_mod.addImport("media", media_mod);
|
|
exe_mod.addImport("vecmath", vecmath_mod);
|
|
exe_mod.addImport("vulkan", vulkan_mod);
|
|
exe_mod.addImport("zglfw", zglfw_mod);
|
|
exe_mod.linkLibrary(zglfw_lib);
|
|
|
|
try addShaders(b, exe_mod);
|
|
|
|
const options = b.addOptions();
|
|
options.addOption([]const u8, "version", zon.version);
|
|
exe_mod.addOptions("config", options);
|
|
|
|
const exe = b.addExecutable(.{
|
|
.name = "voxel-game",
|
|
.root_module = exe_mod,
|
|
.use_llvm = llvm,
|
|
.use_lld = llvm,
|
|
});
|
|
|
|
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 game");
|
|
run_step.dependOn(&run_cmd.step);
|
|
}
|
|
|
|
fn isShaderExtension(extension: []const u8) bool {
|
|
return std.mem.eql(u8, extension, ".vert") or
|
|
std.mem.eql(u8, extension, ".frag") or
|
|
std.mem.eql(u8, extension, ".comp");
|
|
}
|
|
|
|
fn addShaders(b: *std.Build, exe_mod: *std.Build.Module) !void {
|
|
var dir = try b.build_root.handle.openDir(b.graph.io, "assets/shaders", .{ .iterate = true });
|
|
defer dir.close(b.graph.io);
|
|
|
|
var walker = try dir.walk(b.allocator);
|
|
defer walker.deinit();
|
|
|
|
while (try walker.next(b.graph.io)) |entry| {
|
|
if (entry.kind != .file) continue;
|
|
if (!isShaderExtension(std.fs.path.extension(entry.path))) continue;
|
|
|
|
const glslc = b.addSystemCommand(&.{ "glslc", "-g", "--target-env=vulkan1.2" });
|
|
glslc.addFileArg(b.path(b.pathJoin(&.{ "assets/shaders", entry.path })));
|
|
glslc.addArg("-o");
|
|
const spv = glslc.addOutputFileArg(b.fmt("{s}.spv", .{entry.basename}));
|
|
glslc.addArgs(&.{ "-MD", "-MF" });
|
|
_ = glslc.addDepFileOutputArg(b.fmt("{s}.d", .{entry.basename}));
|
|
|
|
exe_mod.addAnonymousImport(b.fmt("shaders/{s}", .{entry.path}), .{ .root_source_file = spv });
|
|
}
|
|
}
|