Files
voxel-game/src/assets/Blocks.zig

311 lines
9.6 KiB
Zig

//! Module for loading block definitions.
const Blocks = @This();
const std = @import("std");
const media = @import("media");
const voxels = @import("../voxels.zig");
const Atom = @import("../engine/Atom.zig").Atom;
const Engine = @import("../engine/Engine.zig");
const Materials = @import("Materials.zig");
const Textures = @import("Textures.zig");
pub const Id = enum(u16) {
// VOLATILE Synchronize explicit values with `init` implementation.
/// An empty block.
air = 0,
_,
/// Cast an integer into an ID. This can produce an invalid ID.
pub fn fromInt(value: u16) Id {
return @enumFromInt(value);
}
/// Cast an index into an ID. This can produce an invalid ID. The caller
/// asserts that the index is not greater than the max ID value.
pub fn fromIndex(index: usize) Id {
std.debug.assert(index < max_blocks);
return @enumFromInt(@as(u16, @intCast(index)));
}
/// Cast an index into an ID. This can produce an invalid ID. Returns an
/// error if the index is greater than the max ID value.
pub fn fromIndexSafe(index: usize) error{Overflow}!Id {
if (index >= max_blocks) return error.Overflow;
return @enumFromInt(@as(u16, @intCast(index)));
}
/// Cast an ID into an integer.
pub fn toInt(self: Id) u16 {
return @intFromEnum(self);
}
};
pub const Key = struct {
/// Atom representing the filename of the block definition.
filename: Atom,
};
pub const Definition = struct {
pub const Wall = struct {
material: Materials.Id = .empty,
transform: voxels.Transform = .identity,
};
pub const Walls = std.enums.EnumFieldStruct(voxels.Orientation, Wall, .{});
walls: Walls = .{},
pub fn initUniform(material: Materials.Id) Definition {
const wall: Wall = .{
.material = material,
};
return .{
.walls = .{
.negative_x = wall,
.positive_x = wall,
.negative_y = wall,
.positive_y = wall,
.negative_z = wall,
.positive_z = wall,
},
};
}
};
/// Maps a key value to a block definition ID.
map: std.AutoHashMapUnmanaged(Key, Id),
/// Stores all `Definition` structs and maps a block definition ID to a
/// `Definition` struct.
array: std.ArrayList(Definition),
/// With `@sizeOf(Definition) == 24` and `max_blocks = 4096`, the block
/// definitions should take ~95.4 kiB in RAM.
pub const max_blocks = 4096;
pub fn init(allocator: std.mem.Allocator) !Blocks {
var map: std.AutoHashMapUnmanaged(Key, Id) = .empty;
errdefer map.deinit(allocator);
try map.ensureTotalCapacity(allocator, max_blocks);
var array: std.ArrayList(Definition) = try .initCapacity(allocator, max_blocks);
errdefer array.deinit(allocator);
// VOLATILE Synchronize with explicit values on top of `Id` type.
array.appendAssumeCapacity(.{});
return .{
.map = map,
.array = array,
};
}
pub fn deinit(self: *Blocks, allocator: std.mem.Allocator) void {
std.log.scoped(.deinit).debug("Deinitializing {*} with Allocator{{{*},{*}}}", .{ self, allocator.ptr, allocator.vtable });
self.array.deinit(allocator);
self.map.deinit(allocator);
self.* = undefined;
}
/// Get the ID of a block definition given its filename (as a string). Returns
/// `null` if such block hasn't been loaded.
pub fn get(self: *const Blocks, filename: []const u8) ?Id {
return self.map.get(.{
// If the atom doesn't exist, then the block definition cannot possibly
// exist.
.filename = .fromStringIfExists(filename) orelse return null,
});
}
/// Get the ID of a block definition given its filename (as an atom). Returns
/// `null` if such block hasn't been loaded.
pub fn getAtom(self: *const Blocks, filename: Atom) ?Id {
return self.map.get(.{
.filename = filename,
});
}
pub fn getOrLoad(
self: *Blocks,
engine: *Engine,
materials: *Materials,
textures: *Textures,
stbi: *media.stbi,
filename: []const u8,
temp_allocator: std.mem.Allocator,
io: std.Io,
) !Id {
const key: Key = .{
// If the material already exists, then the atom must exist and the
// following line will not return any error.
.filename = try .fromString(filename, io),
};
// We don't use `getOrPutAssumeCapacity` method, because we might already be
// at full capacity, in which case we should return `error.OutOfBlocks`.
if (self.map.get(key)) |id| {
return id;
} else {
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfBlocks,
};
const def = try loadBlock(engine, materials, textures, stbi, filename, temp_allocator, io);
self.map.putAssumeCapacityNoClobber(key, id);
self.array.appendAssumeCapacity(def);
return id;
}
}
pub fn getOrLoadAtom(
self: *Blocks,
engine: *Engine,
materials: *Materials,
textures: *Textures,
stbi: *media.stbi,
filename: Atom,
temp_allocator: std.mem.Allocator,
io: std.Io,
) !Id {
const key: Key = .{
.filename = filename,
};
// We don't use `getOrPutAssumeCapacity` method, because we might already be
// at full capacity, in which case we should return `error.OutOfBlocks`.
if (self.map.get(key)) |id| {
return id;
} else {
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfBlocks,
};
const def = try loadBlock(engine, materials, textures, stbi, filename.toString(), temp_allocator, io);
self.map.putAssumeCapacityNoClobber(key, id);
self.array.appendAssumeCapacity(def);
return id;
}
}
pub fn loadAll(
self: *Blocks,
engine: *Engine,
materials: *Materials,
textures: *Textures,
stbi: *media.stbi,
temp_allocator: std.mem.Allocator,
io: std.Io,
) void {
const cwd = std.Io.Dir.cwd();
var dir = cwd.openDir(io, "assets/blocks", .{ .iterate = true }) catch |err| {
std.log.err("Error while opening block definitions directory: {s}", .{@errorName(err)});
return;
};
defer dir.close(io);
var it = dir.iterate();
while (it.next(io) catch |err| {
std.log.err("Error while iterating over block definitions directory: {s}", .{@errorName(err)});
return;
}) |entry| {
if (entry.kind != .file) {
std.log.warn("Skipping block definition entry {s}, which is not a file", .{entry.name});
continue;
}
_ = self.getOrLoad(engine, materials, textures, stbi, entry.name, temp_allocator, io) catch |err| {
std.log.err("Error while loading block definition entry {s}: {s}", .{ entry.name, @errorName(err) });
};
}
}
fn loadBlock(
engine: *Engine,
materials: *Materials,
textures: *Textures,
stbi: *media.stbi,
filename: []const u8,
temp_allocator: std.mem.Allocator,
io: std.Io,
) !Definition {
const DefinitionJson = struct {
pub const Wall = struct {
material: ?[]const u8 = null,
transform: voxels.Transform = .identity,
};
pub const Walls = std.enums.EnumFieldStruct(voxels.Orientation, Wall, .{});
material: ?[]const u8 = null,
walls: ?Walls = null,
};
std.log.debug("Loading block \"{s}\"...", .{filename});
const cwd = std.Io.Dir.cwd();
var dir = try cwd.openDir(io, "assets/blocks", .{});
defer dir.close(io);
// NOTE Buffer size approximated based on expected JSON structure.
var buffer: [1024]u8 = undefined;
const file = try dir.openFile(io, filename, .{});
defer file.close(io);
var file_reader = file.reader(io, &buffer);
var json_reader = std.json.Reader.init(temp_allocator, &file_reader.interface);
defer json_reader.deinit();
const parsed: std.json.Parsed(DefinitionJson) = try std.json.parseFromTokenSource(DefinitionJson, temp_allocator, &json_reader, .{
.duplicate_field_behavior = .@"error",
.ignore_unknown_fields = false,
.allocate = .alloc_if_needed,
});
defer parsed.deinit();
const def_json = parsed.value;
const block: Definition = blk: {
if (def_json.material) |name| {
if (def_json.walls != null) {
std.log.err("Block definition entry {s} has both properties \"material\" and \"walls\" defined, but exactly one of them must be defined", .{filename});
return error.ParseError;
}
const material = try materials.getOrLoad(engine, textures, stbi, name, temp_allocator, io);
break :blk .initUniform(material);
}
if (def_json.walls) |walls| {
break :blk .{
.walls = walls: {
var ret: Definition.Walls = undefined;
inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| {
@field(ret, field.name) = .{
.material = try materials.getOrLoad(engine, textures, stbi, @field(walls, field.name).material, temp_allocator, io),
.transform = @field(walls, field.name).transform,
};
}
break :walls ret;
},
};
}
std.log.err("Block definition entry {s} has neither \"material\" or \"walls\" properties defined, but exactly one of them must be defined", .{filename});
return error.ParseError;
};
return block;
}