feat: create chunkloader

This commit is contained in:
catangent 2025-08-29 15:28:19 +01:00
parent df4ae0a20c
commit 5e0bae610e
2 changed files with 69 additions and 3 deletions

View file

@ -8,6 +8,7 @@ const znoise = @import("znoise");
const chunks = @import("world/chunk.zig");
const WorldState = @import("world/world_state.zig").WorldState;
const CameraChunkloader = @import("world/world_state.zig").CameraChunkloader;
const chunk_generators = @import("world/chunk_generators.zig");
const TILE_TEXTURE_RESOLUTION = 16;
@ -141,9 +142,12 @@ pub fn main() !void {
var world_state: WorldState = WorldState.init(&thread_pool, allocator, chunk_graphics_resources);
defer world_state.deinit();
for (0..10) |x| for (0..10) |z| {
try world_state.queueLoadChunk(.{@intCast(x), 0, @intCast(z)});
};
// for (0..10) |x| for (0..10) |z| {
// try world_state.queueLoadChunk(.{@intCast(x), 0, @intCast(z)});
// };
var chunkloader = CameraChunkloader.init(allocator, 3);
try chunkloader.update(&world_state, .{0, 0, 0});
defer chunkloader.deinit();
while (!raylib.WindowShouldClose()) {
raylib.PollInputEvents();

View file

@ -15,6 +15,68 @@ pub const WorldStateError = error{
ChunkNotGeneratedError,
};
pub const CameraChunkloader = struct {
allocator: Allocator,
render_distance: i64,
loaded: AutoHashMap(@Vector(3, i64), void),
to_load: AutoHashMap(@Vector(3, i64), void),
current_position: @Vector(3, i64),
pub fn init(allocator: Allocator, render_distance: i64) CameraChunkloader {
var self: CameraChunkloader = undefined;
self.allocator = allocator;
self.render_distance = render_distance;
self.loaded = AutoHashMap(@Vector(3, i64), void).init(allocator);
self.to_load = AutoHashMap(@Vector(3, i64), void).init(allocator);
self.current_position = .{0, 0, 0};
return self;
}
pub fn deinit(self: *CameraChunkloader) void {
self.loaded.deinit();
self.to_load.deinit();
self.* = undefined;
}
fn generateRecursively(self: *CameraChunkloader, position: @Vector(3, i64), world_state: *WorldState) !void {
if(self.loaded.contains(position)){
return;
}
if(@reduce(.Add, (position-self.current_position)*(position-self.current_position)) > self.render_distance*self.render_distance){
try self.to_load.put(position, {});
return;
}
try world_state.queueLoadChunk(position);
try self.loaded.put(position, {});
const neighbors: [6]@Vector(3, i64) = .{
position + @Vector(3, i64){0, 0, 1},
position + @Vector(3, i64){0, 0, -1},
position + @Vector(3, i64){0, 1, 0},
position + @Vector(3, i64){0, -1, 0},
position + @Vector(3, i64){1, 0, 0},
position + @Vector(3, i64){-1, 0, 0},
};
for (neighbors) |neighbor| {
try self.generateRecursively(neighbor, world_state);
}
}
pub fn update(self: *CameraChunkloader, world_state: *WorldState, new_position: @Vector(3, i64)) !void {
self.current_position = new_position;
if(!self.loaded.contains(self.current_position)) {
try self.generateRecursively(self.current_position, world_state);
}
var to_load_clone = self.to_load.move(); // old to_load is emptied.
defer to_load_clone.deinit();
var iterator = to_load_clone.keyIterator();
while (iterator.next()) |pos| {
try self.generateRecursively(pos.*, world_state);
}
}
};
pub const ChunkEntry = struct {
chunk: ?Chunk = null,
model: ?raylib.ChunkModel = null,