Compare commits

..

No commits in common. "e04f5ba682e8ccd253aff9fb9a6ebc1b09a255a4" and "099c3c0607d670a71e18cd5c694877a0d090afe2" have entirely different histories.

7 changed files with 71 additions and 363 deletions

224
build.zig
View File

@ -2,35 +2,21 @@ const std = @import("std");
const Build = std.Build;
const sokol = @import("sokol");
const game_name = "sokoban";
const itchio_target = "SlimeBOOS/sokoban";
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const dep_stb = b.dependency("stb", .{});
const dep_miniz = b.dependency("miniz", .{
.target = b.graph.host,
});
const ctx = Context.init(b, dep_stb.module("stb_image"), dep_miniz.module("miniz"));
const ctx = Context.init(b, dep_stb.module("stb_image"));
const pkg_install_dir: Build.InstallDir = .{ .custom = "pkg" };
const run_step = b.step("run", "Run game");
const artifact = try buildProject(ctx, target, optimize, .{
.imgui = b.option(bool, "imgui", "ImGui integration"),
.assets_from_filesystem = b.option(bool, "assets-filesystem", "Allow reading assets from filesystem"),
.tracy = b.option(bool, "tracy", "Tracy integration")
});
switch (artifact) {
var run_cmd_step: *std.Build.Step = undefined;
switch (try buildProject(ctx, target, optimize)) {
.native => |exe| {
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.addPassthruArgs();
run_step.dependOn(&run_cmd.step);
run_cmd_step = &run_cmd.step;
{
const exe_tests = b.addTest(.{ .root_module = exe.root_module, });
@ -40,103 +26,36 @@ pub fn build(b: *std.Build) !void {
test_step.dependOn(&run_exe_tests.step);
}
},
.wasm => |install_dir| {
b.getInstallStep().dependOn(&install_dir.step);
.wasm => |build_step| {
b.getInstallStep().dependOn(build_step);
const dep_sokol = b.dependency("sokol", .{});
const dep_emsdk = dep_sokol.builder.dependency("emsdk", .{});
const emrun_step = sokol.emRunStep(b, .{ .name = "index", .emsdk = dep_emsdk });
emrun_step.step.dependOn(&install_dir.step);
run_step.dependOn(&emrun_step.step);
emrun_step.step.dependOn(build_step);
run_cmd_step = &emrun_step.step;
// add an emsdk install step
const emsdk_install_step = sokol.emSdkInstallStep(b, dep_sokol.builder.dependency("emsdk", .{}), .{});
b.step("install-emsdk", "Install Emscripten SDK in zig-pkg").dependOn(emsdk_install_step);
}
}
{
const package_suffix = getPackageSuffix(target);
const package_name = b.fmt("{s}_{s}.zip", .{game_name, package_suffix});
const package = artifact.createPackage(ctx, package_name);
const package_step = b.step("package", "Pack artifacts into package");
package_step.dependOn(&b.addInstallFileWithDir(package, pkg_install_dir, package_name).step);
const deploy_step = b.step("deploy", "Deploy package to itch.io");
const deploy_run = butlerPush(b, package, package_suffix);
deploy_step.dependOn(&deploy_run.step);
}
{
const deploy_all_step = b.step("deploy-all", "Deploy windows,linux and web to itch.io");
inline for (.{
b.graph.host, // TODO: Add proper target for linux. Encountered problems with zig complaining about dynamic libraries.
b.resolveTargetQuery(.{
.os_tag = .windows,
.cpu_arch = .x86_64
}),
b.resolveTargetQuery(.{
.os_tag = .emscripten,
.cpu_arch = .wasm32
}),
}) |deploy_target| {
const deploy_artifact = try buildProject(ctx, deploy_target, .ReleaseSmall, .{});
const package_suffix = getPackageSuffix(deploy_target);
const package_name = b.fmt("{s}_{s}.zip", .{game_name, package_suffix});
const package = deploy_artifact.createPackage(ctx, package_name);
const deploy_run = butlerPush(b, package, package_suffix);
deploy_all_step.dependOn(&deploy_run.step);
}
}
}
fn butlerPush(b: *Build, package: Build.LazyPath, channel: []const u8) *Build.Step.Run {
const deploy_run = b.addSystemCommand(&.{"butler", "push"});
deploy_run.addFileArg(package);
deploy_run.addArg(b.fmt("{s}:{s}", .{itchio_target, channel}));
deploy_run.has_side_effects = true;
return deploy_run;
const run_step = b.step("run", "Run game");
run_step.dependOn(run_cmd_step);
}
const ProjectResult = union(enum) {
native: *std.Build.Step.Compile,
wasm: *std.Build.Step.InstallDir,
pub fn createPackage(self: ProjectResult, ctx: Context, output_name: []const u8) Build.LazyPath {
switch (self) {
.native => |exe| {
return buildZip(ctx, output_name, &.{
.{ .file = exe.getEmittedBin() }
});
},
.wasm => |install_dir| {
return buildZip(ctx, output_name, &.{
.{ .directory = install_dir.options.source_dir }
});
}
}
}
wasm: *std.Build.Step,
};
fn getPackageSuffix(target: Build.ResolvedTarget) []const u8 {
return switch(target.result.os.tag) {
.emscripten => "web",
.linux => "linux",
.windows => "windows",
else => @panic("Unsupported target")
};
}
const ProjectOptions = struct {
imgui: ?bool = null,
assets_from_filesystem: ?bool = null,
tracy: ?bool = null,
};
fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, opts: ProjectOptions) !ProjectResult {
fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !ProjectResult {
const b = ctx.b;
const name = "sokol_template_v2";
const assets_dir = b.path("assets");
const asset_files = [_][]const u8{
"sokoban/sokoban_tilesheet.png",
@ -150,15 +69,15 @@ fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builti
const is_wasm = target.result.cpu.arch.isWasm();
const is_debug = optimize == .debug;
const has_imgui = opts.imgui orelse is_debug;
const has_imgui = b.option(bool, "imgui", "ImGui integration") orelse is_debug;
var assets_from_filesystem = false;
if (!is_wasm) {
assets_from_filesystem = opts.assets_from_filesystem orelse is_debug;
assets_from_filesystem = b.option(bool, "assets-filesystem", "Allow reading assets from filesystem") orelse is_debug;
}
var has_tracy = false;
if (!is_wasm) {
has_tracy = opts.tracy orelse is_debug;
has_tracy = b.option(bool, "tracy", "Tracy integration") orelse is_debug;
}
const win32_has_console = null;
@ -218,15 +137,12 @@ fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builti
slang.metal_macos = true;
slang.glsl300es = true;
}
const mod_shader = b.createModule(.{
.root_source_file = try sokol.shdc.compile(b, .{
const mod_shader = try sokol.shdc.createModule(b, "shader", mod_sokol, .{
.shdc_dep = dep_shdc,
.input = "src/platform/shader.glsl",
.output = "src/platform/shader.zig",
.slang = slang
})
});
mod_shader.addImport("sokol", mod_sokol);
mod_shader.addImport("math", math_mod);
platform_mod.addImport("shader", mod_shader);
@ -258,41 +174,16 @@ fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builti
}
if (is_wasm) {
exe_mod.sanitize_c = .off;
// build the main file into a library, this is because the WASM 'exe'
// needs to be linked in a separate build step with the Emscripten linker
const main_lib = b.addLibrary(.{
.name = "index",
.root_module = exe_mod
});
const dep_emsdk = dep_sokol.builder.dependency("emsdk", .{});
patchWasmIncludeDirs(
exe_mod,
dep_emsdk.path("upstream/emscripten/cache/sysroot/include"),
&(dep_sokol.artifact("sokol_clib").step)
);
const link_step = try sokol.emLinkStep(b, .{
.lib_main = main_lib,
.target = target,
.optimize = optimize,
.emsdk = dep_emsdk,
.use_webgl2 = true,
.use_emmalloc = true,
.use_filesystem = true,
.shell_file_path = b.path("src/platform/shell.html"),
.extra_args = &.{ "-sALLOW_MEMORY_GROWTH", "-sENVIRONMENT=web" }
});
return ProjectResult{
.wasm = link_step
.wasm = buildWasm(b, .{
.name = "index",
.root_module = exe_mod,
.dep_sokol = dep_sokol,
})
};
} else {
const exe = b.addExecutable(.{
.name = game_name,
.name = name,
.root_module = exe_mod,
});
@ -354,9 +245,8 @@ const Context = struct {
b: *std.Build,
png_to_icon: *Build.Step.Compile,
create_win32_resource: *Build.Step.Compile,
create_zip: *Build.Step.Compile,
pub fn init(b: *std.Build, stb_image: *Build.Module, miniz: *Build.Module) Context {
pub fn init(b: *std.Build, stb_image: *Build.Module) Context {
return Context{
.b = b,
.png_to_icon = b.addExecutable(.{
@ -375,16 +265,6 @@ const Context = struct {
.target = b.graph.host,
.root_source_file = b.path("scripts/create-win32-resource.zig"),
})
}),
.create_zip = b.addExecutable(.{
.name = "create-zip",
.root_module = b.createModule(.{
.target = b.graph.host,
.root_source_file = b.path("scripts/create-zip.zig"),
.imports = &.{
.{ .name = "miniz", .module = miniz }
}
})
})
};
}
@ -408,22 +288,48 @@ fn buildWin32ResourceFile(ctx: Context, mod: *Build.Module, opts: Win32ResourceF
mod.addWin32ResourceFile(.{ .file = resource_file });
}
const ZipEntry = union(enum) {
directory: Build.LazyPath,
file: Build.LazyPath
const BuildWasmOptions = struct {
name: []const u8,
root_module: *std.Build.Module,
dep_sokol: *std.Build.Dependency,
};
fn buildZip(ctx: Context, output_name: []const u8, entries: []const ZipEntry) Build.LazyPath {
const b = ctx.b;
fn buildWasm(b: *std.Build, opts: BuildWasmOptions) *std.Build.Step {
opts.root_module.sanitize_c = .off;
var create_zip_run = b.addRunArtifact(ctx.create_zip);
for (entries) |entry| {
switch (entry) {
.directory => |lp| create_zip_run.addDirectoryArg(lp),
.file => |lp| create_zip_run.addFileArg(lp),
}
}
return create_zip_run.addOutputFileArg(output_name);
// build the main file into a library, this is because the WASM 'exe'
// needs to be linked in a separate build step with the Emscripten linker
const main_lib = b.addLibrary(.{
.name = opts.name,
.root_module = opts.root_module,
});
const dep_sokol = opts.dep_sokol;
const dep_emsdk = dep_sokol.builder.dependency("emsdk", .{});
// TODO:
// const emsdk_install_step = sokol.emSdkInstallStep(b, dep_emsdk, .{});
// main_lib.step.dependOn(emsdk_install_step);
patchWasmIncludeDirs(
opts.root_module,
dep_emsdk.path("upstream/emscripten/cache/sysroot/include"),
&(dep_sokol.artifact("sokol_clib").step)
);
const link_step = sokol.emLinkStep(b, .{
.lib_main = main_lib,
.target = opts.root_module.resolved_target.?,
.optimize = opts.root_module.optimize.?,
.emsdk = dep_emsdk,
.use_webgl2 = true,
.use_emmalloc = true,
.use_filesystem = true,
.shell_file_path = b.path("src/platform/shell.html"),
.extra_args = &.{ "-sALLOW_MEMORY_GROWTH", "-sENVIRONMENT=web" }
}) catch unreachable;
return &link_step.step;
}
fn patchWasmIncludeDirs(

View File

@ -1,7 +1,7 @@
.{
.name = .sokoban,
.name = .sokol_template_v2,
.version = "0.0.0",
.fingerprint = 0xa95600b91ee22a2e, // Changing this has security and trust implications.
.fingerprint = 0xf440b4e6e51f5206, // Changing this has security and trust implications.
.dependencies = .{
.sokol = .{
.url = "git+https://github.com/floooh/sokol-zig.git#e371234960812882406a296c4f3154321a16ee4d",
@ -21,9 +21,6 @@
.tracy = .{
.path = "./libs/tracy",
},
.miniz = .{
.path = "./libs/miniz",
},
},
.paths = .{
"build.zig",

View File

@ -1,41 +0,0 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const src_dep = b.dependency("src", .{});
// TODO: This is a workaround to avoid a translate-c bug.
// Remove this when fixed, likely when 0.17 is released.
const remove_pragma_step = b.addRunArtifact(b.addExecutable(.{
.name = "remove pragma",
.root_module = b.createModule(.{
.root_source_file = b.path("scripts/remove-pragma.zig"),
.target = b.graph.host
})
}));
remove_pragma_step.addFileArg(src_dep.path("miniz.h"));
var clean_miniz_h = remove_pragma_step.addOutputFileArg("miniz.h");
const mod = b.addModule("miniz", .{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/root.zig"),
.link_libc = true
});
mod.addIncludePath(clean_miniz_h.dirname());
mod.addCSourceFiles(.{
.root = src_dep.path("."),
.files = &.{ "miniz.c" },
});
const write_file_step = b.addWriteFiles();
const translate_c = b.addTranslateC(.{
.root_source_file = write_file_step.add("c.h", "#include \"miniz.h\""),
.target = target,
.optimize = optimize,
});
translate_c.addIncludePath(clean_miniz_h.dirname());
mod.addImport("c", translate_c.createModule());
}

View File

@ -1,13 +0,0 @@
.{
.name = .miniz,
.version = "0.0.1",
.dependencies = .{
.src = .{
.url = "https://github.com/richgel999/miniz/releases/download/3.1.2/miniz-3.1.2.zip",
.hash = "N-V-__8AACZ9BwBtpix1JS569yNZcDzDf9abe3Zimx8glq9u",
},
},
.minimum_zig_version = "0.17.0-dev.2264+230c63650",
.paths = .{""},
.fingerprint = 0xa708528eb541dff2,
}

View File

@ -1,48 +0,0 @@
const std = @import("std");
const Io = std.Io;
pub fn main(init: std.process.Init) !void {
const io = init.io;
const arena = init.arena.allocator();
const args = try init.minimal.args.toSlice(arena);
if (args.len != 3) {
std.debug.print("Usage: ./remove-pragma <input> <output>", .{});
std.process.exit(1);
}
const input_path = args[1];
const output_path = args[2];
const cwd = Io.Dir.cwd();
var input_file = try cwd.openFile(io, input_path, .{ .mode = .read_only });
defer input_file.close(io);
var input_buffer: [4*4096]u8 = undefined;
var input_file_reader = input_file.reader(io, &input_buffer);
const reader = &input_file_reader.interface;
var output_file = try cwd.createFile(io, output_path, .{ });
defer output_file.close(io);
var output_buffer: [4*4096]u8 = undefined;
var output_file_writer = output_file.writer(io, &output_buffer);
const writer = &output_file_writer.interface;
while (true) {
const line = reader.takeDelimiterInclusive('\n') catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
};
const trimmed_line = std.mem.trimStart(u8, line, &std.ascii.whitespace);
if (std.mem.startsWith(u8, trimmed_line, "#pragma ")) {
continue;
}
try writer.writeAll(line);
}
try writer.flush();
}

View File

@ -1,37 +0,0 @@
const c = @import("c");
pub const Archive = struct {
archive: c.mz_zip_archive,
pub fn initFile(self: *Archive, output: [*c]const u8) !void {
self.* = Archive{
.archive = .{}
};
if (c.mz_zip_writer_init_file(&self.archive, output, 0) == 0) {
return error.InitFileFailed;
}
}
pub fn end(self: *Archive) void {
_ = c.mz_zip_writer_end(&self.archive);
}
pub fn addFile(self: *Archive, name_in_archive: [*c]const u8, path: [*c]const u8) !void {
if (c.mz_zip_writer_add_file(
&self.archive,
name_in_archive,
path,
null,
0,
@bitCast(c.MZ_DEFAULT_COMPRESSION),
) == 0) {
return error.AddFileFailed;
}
}
pub fn finalize(self: *Archive) !void {
if (c.mz_zip_writer_finalize_archive(&self.archive) == 0) {
return error.FinalizeFailed;
}
}
};

View File

@ -1,56 +0,0 @@
const std = @import("std");
const Io = std.Io;
const miniz = @import("miniz");
pub fn main(init: std.process.Init) !void {
const io = init.io;
const arena = init.arena.allocator();
const args = try init.minimal.args.toSlice(arena);
if (args.len != 3) {
std.debug.print("Usage: ./create-zip <input> <output>", .{});
std.process.exit(1);
}
const input_path = args[1];
const output_zip_path = args[2];
const cwd = Io.Dir.cwd();
var archive: miniz.Archive = undefined;
try archive.initFile(output_zip_path);
defer archive.end();
const input_stat = try cwd.statFile(io, input_path, .{});
if (input_stat.kind == .file) {
try addFileToArchive(&archive, io, cwd, input_path);
} else if (input_stat.kind == .directory) {
var input_dir = try cwd.openDir(io, input_path, .{ .iterate = true });
defer input_dir.close(io);
var input_dir_iter = input_dir.iterate();
while (try input_dir_iter.next(io)) |entry| {
if (entry.kind == .file) {
try addFileToArchive(&archive, io, input_dir, entry.name);
}
}
} else {
std.debug.print("Invalid input\n", .{});
std.process.exit(1);
}
try archive.finalize();
}
fn addFileToArchive(archive: *miniz.Archive, io: Io, dir: Io.Dir, sub_path: []const u8) !void {
var path_buff: [std.fs.max_path_bytes+1]u8 = undefined;
const path_len = try dir.realPathFile(io, sub_path, path_buff[0..(path_buff.len-1)]);
path_buff[path_len] = 0;
const name = std.fs.path.basename(sub_path);
var namez_buff: [std.fs.max_name_bytes+1]u8 = undefined;
@memcpy(namez_buff[0..name.len], name);
namez_buff[name.len] = 0;
try archive.addFile(namez_buff[0..name.len :0], path_buff[0..path_len :0]);
}