//! Level structure, grid containing tile data. const std = @import("std"); const expect = std.testing.expect; const Self = @This(); const cell_t = u16; width: u16, height: u16, buffer: []cell_t, /// Create structure and allocate required memory. The `buffer` array size will /// be equal to `width` times `height`. pub fn init(allocator: *std.mem.Allocator, width: u16, height: u16) !Self { var level = Self{ .width = width, .height = height, .buffer = undefined, }; level.buffer = try allocator.alloc(cell_t, width * height); return level; } /// Free the level buffer. pub fn deinit(self: *Self, allocator: *std.mem.Allocator) void { allocator.free(self.buffer); } test "create level buffer" { // Create allocator. var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer { const leaked = gpa.deinit(); if (leaked) expect(false); //fail test } const allocator = &gpa.allocator; // Initialize level struct (twice 'cause why not?). var level: Self = try Self.init(allocator, 64, 32); level.deinit(allocator); level = try Self.init(allocator, 64, 64); defer level.deinit(allocator); level.buffer[128] = 32; expect(level.buffer[128] == 32); expect(level.width == 64); expect(level.height == 64); }