Add ldtk file

master
Louis Pearson 2022-08-03 16:23:59 -06:00
parent 9b235a036b
commit 61a6fc9905
4 changed files with 3691 additions and 0 deletions

3614
assets/maps/wired.ldtk Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 723 B

After

Width:  |  Height:  |  Size: 730 B

View File

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const LDtkImport = @import("tools/LDtkImport.zig");
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.build.Builder) !void {
const assets = std.build.Pkg{ const assets = std.build.Pkg{
@ -6,8 +7,14 @@ pub fn build(b: *std.build.Builder) !void {
.source = .{ .path = "assets/assets.zig" }, .source = .{ .path = "assets/assets.zig" },
}; };
const ldtk = LDtkImport.create(b, .{
.source_path = .{ .path = "assets/maps/wired.ldtk" },
.output_name = "mapldtk",
});
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const lib = b.addSharedLibrary("cart", "src/main.zig", .unversioned); const lib = b.addSharedLibrary("cart", "src/main.zig", .unversioned);
lib.step.dependOn(&ldtk.step);
lib.addPackage(assets); lib.addPackage(assets);
lib.setBuildMode(mode); lib.setBuildMode(mode);
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });

70
tools/LDtkImport.zig Normal file
View File

@ -0,0 +1,70 @@
//! Uses zig-ldtk to convert a ldtk file into a binary format for wired
const std = @import("std");
const LDtk = @import("../deps/zig-ldtk/src/LDtk.zig");
const KB = 1024;
const MB = 1024 * KB;
const LDtkImport = @This();
step: std.build.Step,
builder: *std.build.Builder,
source_path: std.build.FileSource,
output_name: []const u8,
pub fn create(b: *std.build.Builder, opt: struct {
source_path: std.build.FileSource,
output_name: []const u8,
}) *@This() {
var result = b.allocator.create(LDtkImport) catch @panic("memory");
result.* = LDtkImport{
.step = std.build.Step.init(.custom, "convert and embed a ldtk map file", b.allocator, make),
.builder = b,
.source_path = opt.source_path,
.output_name = opt.output_name,
};
return result;
}
fn make(step: *std.build.Step) !void {
const this = @fieldParentPtr(LDtkImport, "step", step);
const allocator = this.builder.allocator;
const cwd = std.fs.cwd();
// Get path to source and output
const source_src = this.source_path.getPath(this.builder);
const output = this.builder.getInstallPath(.lib, this.output_name);
// Open ldtk file and read all of it into `source`
const source_file = try cwd.openFile(source_src, .{});
defer source_file.close();
const source = try source_file.readToEndAlloc(allocator, 10 * MB);
defer allocator.free(source);
// Create output array to write to
var data = std.ArrayList(u8).init(allocator);
defer data.deinit();
const writer = data.writer();
_ = writer;
// TODO: Convert LDtk data into wired format
const ldtk = try LDtk.parse(allocator, source);
defer ldtk.deinit(allocator);
if (ldtk.levels.len > 0) {
const level0 = ldtk.levels[0];
if (level0.layerInstances) |layers| {
for (layers) |layer| {
std.log.warn("{s}: {}", .{ layer.__identifier, layer.__type });
}
}
}
// Open output file and write data
cwd.makePath(this.builder.getInstallPath(.lib, "")) catch |e| switch (e) {
error.PathAlreadyExists => {},
else => return e,
};
try cwd.writeFile(output, data.items);
}