Add tilemap to texture array conversion to pipeline

This commit is contained in:
2025-02-16 16:54:15 +01:00
parent 88e2e58228
commit d7794972d4

View File

@@ -103,7 +103,7 @@ fn visit(assets_dir: std.fs.Dir, library_dir: std.fs.Dir, allocator: std.mem.All
}; };
defer allocator.free(outbuf); defer allocator.free(outbuf);
@memcpy(outbuf, img.data); rearrange(grid_w, grid_h, tile_w, tile_h, img.data, outbuf);
const out_name = std.fs.path.stem(entry.name); const out_name = std.fs.path.stem(entry.name);
@@ -116,6 +116,42 @@ fn visit(assets_dir: std.fs.Dir, library_dir: std.fs.Dir, allocator: std.mem.All
} }
} }
fn rearrange(grid_w: u32, grid_h: u32, tile_w: u32, tile_h: u32, inbuf: []const u8, outbuf: []u8) void {
std.log.debug("rearrange: {}×{} grid of {}×{} tiles", .{ grid_w, grid_h, tile_w, tile_h });
const row_size = 4 * tile_w;
const row_stride = row_size * grid_w;
const tile_stride = row_stride * tile_h;
std.debug.assert(inbuf.len == tile_stride * grid_h);
std.debug.assert(outbuf.len == tile_stride * grid_h);
var outptr: usize = 0;
var tile_y: u32 = 0;
while (tile_y < grid_h) : (tile_y += 1) {
const tile_byte_offset = tile_y * tile_stride;
var tile_x: u32 = 0;
while (tile_x < grid_w) : (tile_x += 1) {
const column_byte_offset = tile_x * row_size;
var row: u32 = 0;
while (row < tile_h) : (row += 1) {
const row_byte_offset = row * row_stride + tile_byte_offset;
const byte_offset = row_byte_offset + column_byte_offset;
@memcpy(
outbuf[outptr .. outptr + row_size],
inbuf[byte_offset .. byte_offset + row_size],
);
outptr += row_size;
}
}
}
}
fn openDirOrExit(dir: std.fs.Dir, sub_path: []const u8, args: std.fs.Dir.OpenOptions) std.fs.Dir { fn openDirOrExit(dir: std.fs.Dir, sub_path: []const u8, args: std.fs.Dir.OpenOptions) std.fs.Dir {
return dir.openDir(sub_path, args) catch |err| { return dir.openDir(sub_path, args) catch |err| {
std.log.err("Could not open \"{s}\" directory: {s}", .{ sub_path, @errorName(err) }); std.log.err("Could not open \"{s}\" directory: {s}", .{ sub_path, @errorName(err) });