Blocks and chunks
This commit is contained in:
262
src/assets/Blocks.zig
Normal file
262
src/assets/Blocks.zig
Normal file
@@ -0,0 +1,262 @@
|
||||
const Blocks = @This();
|
||||
const std = @import("std");
|
||||
|
||||
const atoms = @import("../engine/atoms.zig");
|
||||
const Engine = @import("../engine/Engine.zig");
|
||||
const Materials = @import("Materials.zig");
|
||||
const Textures = @import("Textures.zig");
|
||||
|
||||
pub const Block = struct {
|
||||
positive_x: Materials.Id,
|
||||
negative_x: Materials.Id,
|
||||
positive_y: Materials.Id,
|
||||
negative_y: Materials.Id,
|
||||
positive_z: Materials.Id,
|
||||
negative_z: Materials.Id,
|
||||
|
||||
pub fn initUniform(material: Materials.Id) Block {
|
||||
return .{
|
||||
.positive_x = material,
|
||||
.negative_x = material,
|
||||
.positive_y = material,
|
||||
.negative_y = material,
|
||||
.positive_z = material,
|
||||
.negative_z = material,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn initSeparate(
|
||||
positive_x: Materials.Id,
|
||||
negative_x: Materials.Id,
|
||||
positive_y: Materials.Id,
|
||||
negative_y: Materials.Id,
|
||||
positive_z: Materials.Id,
|
||||
negative_z: Materials.Id,
|
||||
) Block {
|
||||
return .{
|
||||
.positive_x = positive_x,
|
||||
.negative_x = negative_x,
|
||||
.positive_y = positive_y,
|
||||
.negative_y = negative_y,
|
||||
.positive_z = positive_z,
|
||||
.negative_z = negative_z,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn initArray(materials: [6]Materials.Id) Block {
|
||||
return .{
|
||||
.positive_x = materials[0],
|
||||
.negative_x = materials[1],
|
||||
.positive_y = materials[2],
|
||||
.negative_y = materials[3],
|
||||
.positive_z = materials[4],
|
||||
.negative_z = materials[5],
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
map: Map,
|
||||
blocks: Array,
|
||||
|
||||
pub const capacity = std.math.maxInt(std.meta.Tag(Id));
|
||||
|
||||
pub const Key = struct { atom: atoms.Atom };
|
||||
pub const Id = enum(u12) {
|
||||
air = 0,
|
||||
_,
|
||||
|
||||
pub fn next(self: Id) Id {
|
||||
return @enumFromInt(@intFromEnum(self) + 1);
|
||||
}
|
||||
};
|
||||
|
||||
pub const Map = std.AutoHashMapUnmanaged(Key, Id);
|
||||
pub const Array = std.ArrayList(Block);
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) !Blocks {
|
||||
var map: Map = .empty;
|
||||
errdefer map.deinit(allocator);
|
||||
try map.ensureTotalCapacity(allocator, capacity);
|
||||
|
||||
var blocks: Array = try .initCapacity(allocator, capacity);
|
||||
errdefer blocks.deinit(allocator);
|
||||
|
||||
const air = Block.initUniform(.empty);
|
||||
blocks.appendAssumeCapacity(air);
|
||||
|
||||
return .{
|
||||
.map = map,
|
||||
.blocks = blocks,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Blocks, allocator: std.mem.Allocator) void {
|
||||
std.log.debug("Deinitializing {*} with Allocator{{{*},{*}}}", .{ self, allocator.ptr, allocator.vtable });
|
||||
|
||||
self.blocks.deinit(allocator);
|
||||
self.map.deinit(allocator);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn getAtom(self: *const Blocks, atom: atoms.Atom) ?Id {
|
||||
const key: Key = .{ .atom = atom };
|
||||
return self.map.get(key);
|
||||
}
|
||||
|
||||
pub fn getFilename(self: *const Blocks, filename: []const u8) ?Id {
|
||||
const atom = atoms.getAtom(filename) orelse return null;
|
||||
const key: Key = .{ .atom = atom };
|
||||
return self.map.get(key);
|
||||
}
|
||||
|
||||
pub fn getBlock(self: *const Blocks, id: Id) ?*Block {
|
||||
const index: usize = @intFromEnum(id);
|
||||
return if (index < self.blocks.items.len) &self.blocks.items[index] else null;
|
||||
}
|
||||
|
||||
pub fn getOrLoadAtom(
|
||||
self: *Blocks,
|
||||
engine: *Engine,
|
||||
materials: *Materials,
|
||||
textures: *Textures,
|
||||
atom: atoms.Atom,
|
||||
temp_allocator: std.mem.Allocator,
|
||||
) !Id {
|
||||
const key: Key = .{ .atom = atom };
|
||||
const entry = self.map.getOrPutAssumeCapacity(key);
|
||||
|
||||
if (entry.found_existing) {
|
||||
return entry.value_ptr.*;
|
||||
} else {
|
||||
errdefer _ = self.map.remove(key);
|
||||
const block = try loadBlock(engine, materials, textures, atoms.getString(atom), temp_allocator);
|
||||
const id = self.nextId();
|
||||
entry.value_ptr.* = id;
|
||||
self.blocks.appendAssumeCapacity(block);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getOrLoadFilename(
|
||||
self: *Blocks,
|
||||
engine: *Engine,
|
||||
materials: *Materials,
|
||||
textures: *Textures,
|
||||
filename: []const u8,
|
||||
temp_allocator: std.mem.Allocator,
|
||||
) !Id {
|
||||
const atom = try atoms.getOrPutAtom(filename);
|
||||
const key: Key = .{ .atom = atom };
|
||||
const entry = self.map.getOrPutAssumeCapacity(key);
|
||||
|
||||
if (entry.found_existing) {
|
||||
return entry.value_ptr.*;
|
||||
} else {
|
||||
errdefer _ = self.map.remove(key);
|
||||
const texture = try loadBlock(engine, materials, textures, filename, temp_allocator);
|
||||
const id = self.nextId();
|
||||
entry.value_ptr.* = id;
|
||||
self.blocks.appendAssumeCapacity(texture);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn loadAll(
|
||||
self: *Blocks,
|
||||
engine: *Engine,
|
||||
materials: *Materials,
|
||||
textures: *Textures,
|
||||
temp_allocator: std.mem.Allocator,
|
||||
) void {
|
||||
const cwd = std.fs.cwd();
|
||||
|
||||
var dir = cwd.openDir("assets/blocks", .{ .iterate = true }) catch |err| {
|
||||
std.log.err("Error while opening blocks directory: {s}", .{@errorName(err)});
|
||||
return;
|
||||
};
|
||||
defer dir.close();
|
||||
|
||||
var it = dir.iterate();
|
||||
while (it.next() catch |err| {
|
||||
std.log.err("Error while iterating over blocks directory: {s}", .{@errorName(err)});
|
||||
return;
|
||||
}) |entry| {
|
||||
if (entry.kind != .file) {
|
||||
std.log.warn("Skipping block entry {s}, which is not a file", .{entry.name});
|
||||
continue;
|
||||
}
|
||||
|
||||
_ = self.getOrLoadFilename(engine, materials, textures, entry.name, temp_allocator) catch |err| {
|
||||
std.log.err("Error while loading block entry {s}: {s}", .{ entry.name, @errorName(err) });
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn loadBlock(
|
||||
engine: *Engine,
|
||||
materials: *Materials,
|
||||
textures: *Textures,
|
||||
filename: []const u8,
|
||||
temp_allocator: std.mem.Allocator,
|
||||
) !Block {
|
||||
const BlockJson = struct {
|
||||
material: ?[]const u8 = null,
|
||||
materials: ?[6][]const u8 = null,
|
||||
};
|
||||
|
||||
std.log.debug("Loading block \"{s}\"...", .{filename});
|
||||
|
||||
const cwd = std.fs.cwd();
|
||||
|
||||
var dir = try cwd.openDir("assets/blocks", .{});
|
||||
defer dir.close();
|
||||
|
||||
// NOTE Buffer size approximated based on expected JSON structure.
|
||||
var buffer: [256]u8 = undefined;
|
||||
|
||||
const file = try dir.openFile(filename, .{});
|
||||
defer file.close();
|
||||
|
||||
var file_reader = file.reader(&buffer);
|
||||
var json_reader: std.json.Reader = .init(temp_allocator, &file_reader.interface);
|
||||
defer json_reader.deinit();
|
||||
|
||||
const parsed: std.json.Parsed(BlockJson) = try std.json.parseFromTokenSource(BlockJson, temp_allocator, &json_reader, .{
|
||||
.duplicate_field_behavior = .@"error",
|
||||
.ignore_unknown_fields = false,
|
||||
.allocate = .alloc_if_needed,
|
||||
});
|
||||
defer parsed.deinit();
|
||||
|
||||
const block_json = parsed.value;
|
||||
|
||||
const block: Block = blk: {
|
||||
if (block_json.material) |name| {
|
||||
if (block_json.materials != null) {
|
||||
std.log.err("Block entry {s} has both properties \"material\" and \"materials\" defined, but exactly one of them must be defined", .{filename});
|
||||
return error.ParseError;
|
||||
}
|
||||
|
||||
const material = try materials.getOrLoadFilename(engine, textures, name, temp_allocator);
|
||||
break :blk .initUniform(material);
|
||||
}
|
||||
|
||||
if (block_json.materials) |names| {
|
||||
var ids: [6]Materials.Id = undefined;
|
||||
for (names, &ids) |name, *id| {
|
||||
id.* = try materials.getOrLoadFilename(engine, textures, name, temp_allocator);
|
||||
}
|
||||
break :blk .initArray(ids);
|
||||
}
|
||||
|
||||
std.log.err("Block entry {s} has neither \"material\" or \"materials\" properties defined, but exactly one of them must be defined", .{filename});
|
||||
return error.ParseError;
|
||||
};
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
fn nextId(self: *const Blocks) Id {
|
||||
const index = self.blocks.items.len;
|
||||
return @enumFromInt(index);
|
||||
}
|
||||
Reference in New Issue
Block a user