From 4d61b14052049552472d205ee42fd261054ccc6a Mon Sep 17 00:00:00 2001 From: Szymon Nowakowski Date: Sun, 30 Nov 2025 14:33:01 +0100 Subject: [PATCH] Interator2 --- src/Game.zig | 33 +++++++++++++++++++-------------- src/math.zig | 1 + src/math/Interator2.zig | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 src/math/Interator2.zig diff --git a/src/Game.zig b/src/Game.zig index 34c55ca..edfb88d 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -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]), diff --git a/src/math.zig b/src/math.zig index cbf20f0..fb77496 100644 --- a/src/math.zig +++ b/src/math.zig @@ -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; diff --git a/src/math/Interator2.zig b/src/math/Interator2.zig new file mode 100644 index 0000000..0031bf4 --- /dev/null +++ b/src/math/Interator2.zig @@ -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; + } + }; +}