Interator2

This commit is contained in:
2025-11-30 14:33:01 +01:00
parent c33f700177
commit 4d61b14052
3 changed files with 58 additions and 14 deletions

View File

@@ -15,7 +15,7 @@ const Engine = @import("engine/Engine.zig");
const GenericBuffer = @import("engine/GenericBuffer.zig").GenericBuffer;
const StagingBuffer = @import("engine/StagingBuffer.zig");
const Swapchain = @import("engine/Swapchain.zig");
const Interator3 = math.Interator3;
const Interator2 = math.Interator2;
const Matrix4x4 = math.Matrix4x4;
const Quaternion = math.Quaternion;
const Vector2 = math.Vector2;
@@ -612,31 +612,36 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
chunks.deinit(allocator);
}
var it = Interator3(i16).init(-10, -10, 0, 9, 9, 0);
var it = Interator2(i16).init(-10, -10, 9, 9);
const block_grass = blocks.getFilename("Grass.json").?;
const block_dirt = blocks.getFilename("Dirt.json").?;
const block_stone = blocks.getFilename("Stone.json").?;
const block_bedrock = blocks.getFilename("Bedrock.json").?;
while (it.next()) |chunk_coords| {
while (it.next()) |chunk_coords2| {
const chunk_coords3 = chunk_coords2 ++ [_]i16{0};
const origin = Vector3.init(
@floatFromInt(chunk_coords[0]),
@floatFromInt(chunk_coords[1]),
@floatFromInt(chunk_coords[2]),
@floatFromInt(chunk_coords3[0]),
@floatFromInt(chunk_coords3[1]),
@floatFromInt(chunk_coords3[2]),
).mulScalar(16);
try chunks.ensureUnusedCapacity(allocator, 1);
chunks.putAssumeCapacityNoClobber(chunk_coords, try Chunk.init(engine, .{
.origin = origin,
.descriptor_pool = descriptor_pool,
.per_batch_descriptor_set_layout = per_batch_descriptor_set_layout,
}));
const chunk = chunks.getPtr(chunk_coords).?;
chunks.putAssumeCapacityNoClobber(
chunk_coords3,
try Chunk.init(engine, .{
.origin = origin,
.descriptor_pool = descriptor_pool,
.per_batch_descriptor_set_layout = per_batch_descriptor_set_layout,
}),
);
const chunk = chunks.getPtr(chunk_coords3).?;
var it2 = Interator3(usize).init(0, 0, 0, 15, 15, 0);
var it2 = Interator2(usize).init(0, 0, 15, 15);
while (it2.next()) |pos| {
const x, const y, _ = pos;
const x, const y = pos;
const fpos = Vector2.init(
@floatFromInt(pos[0]),
@floatFromInt(pos[1]),

View File

@@ -1,3 +1,4 @@
pub const Interator2 = @import("math/Interator2.zig").Interator2;
pub const Interator3 = @import("math/Interator3.zig").Interator3;
pub const Iterator3 = @import("math/Iterator3.zig");
pub const Matrix4x4 = @import("math/Matrix4x4.zig").Matrix4x4;

38
src/math/Interator2.zig Normal file
View File

@@ -0,0 +1,38 @@
pub fn Interator2(comptime T: type) type {
return struct {
const Self = @This();
const Vector = @Vector(2, T);
min: Vector,
max: Vector,
current: ?Vector,
pub fn init(min_x: T, min_y: T, max_x: T, max_y: T) Self {
const min: Vector = .{ min_x, min_y };
const max: Vector = .{ max_x, max_y };
return .{
.min = min,
.max = max,
.current = min,
};
}
pub fn next(self: *Self) ?[2]T {
const current = self.current orelse return null;
var next_current = current;
next_current[0] = next_current[0] + 1;
if (next_current[0] > self.max[0]) {
next_current[0] = self.min[0];
next_current[1] = next_current[1] + 1;
if (next_current[1] > self.max[1]) {
self.current = null;
return current;
}
}
self.current = next_current;
return current;
}
};
}