From 572c94f78fc4c81d0213af0ac1fbf2076c3090f2 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 24 Feb 2021 12:20:38 +0100 Subject: [PATCH] Level loading functionnal. --- gen_sample.lua | 11 +++++++++++ sample.kble | Bin 10 -> 13 bytes src/level.zig | 49 +++++++++++++++++++++++++++++++++++++++++-------- src/main.zig | 3 ++- 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100755 gen_sample.lua diff --git a/gen_sample.lua b/gen_sample.lua new file mode 100755 index 0000000..180f125 --- /dev/null +++ b/gen_sample.lua @@ -0,0 +1,11 @@ +#!/usr/bin/lua +local out = io.open("sample.kble", "wb") +out:write(string.char( + 2, + 0, 2, + 0, 2, + 0, 1, + 0, 0, + 0, 0, + 1, 1)) +out:close() diff --git a/sample.kble b/sample.kble index 90b7ea85a856bae50e7758a60fafd2b51a4ce2e6..6bcbf0d42fd3b09e5dd81b2870aeb0da8576caa0 100644 GIT binary patch literal 13 RcmZQ#U;;u$1|VQ$1ONb8015yA literal 10 PcmZQ#U}Ruo00Kq;05bpw diff --git a/src/level.zig b/src/level.zig index 466e0ae..bdda7c5 100644 --- a/src/level.zig +++ b/src/level.zig @@ -52,12 +52,7 @@ pub fn init(allocator: *std.mem.Allocator, width: u16, height: u16) !Self { /// Load level content from given absolute path. Expect the KBLE file format (see /// `kbleformat.md` for more details. pub fn init_load(allocator: *std.mem.Allocator, kble_file_path: []const u8) !Self { - var self = Self{ - .width = undefined, - .height = undefined, - .content = undefined, - .selection = undefined, - }; + var self: Self = undefined; const expected_bytes_per_cell = @sizeOf(cell_type); @@ -74,10 +69,48 @@ pub fn init_load(allocator: *std.mem.Allocator, kble_file_path: []const u8) !Sel // Read first byte and check than the value matches the size of cell_type. { - const cell_spec_byte = try(reader.readByte()); + const cell_spec_byte = try reader.readByte(); assert(cell_spec_byte == expected_bytes_per_cell); } + // Read four bytes and use them for width and height. Two bytes each. + { + const width_byte1: u8 = try reader.readByte(); + const width_byte2: u8 = try reader.readByte(); + const height_byte1: u8 = try reader.readByte(); + const height_byte2: u8 = try reader.readByte(); + + const width: u16 = @intCast(u16, width_byte1) * 256 + @intCast(u16, width_byte2); + const height: u16 = @intCast(u16, height_byte1) * 256 + @intCast(u16, height_byte2); + + std.log.info("Cell size: {}\nWidth/height: {}·{} ; {}·{} => {} ; {}", .{ + expected_bytes_per_cell, + width_byte1, + width_byte2, + height_byte1, + height_byte2, + width, + height, + }); + + // Non-null width and height. + assert(width > 0); + assert(height > 0); + + self = try Self.init(allocator, width, height); + } + + // Read the rest of the file and assign to .content accordingly. + { + var i: u32 = 0; + while (i < self.width * self.height) : (i += 1) { + const byte1 = try reader.readByte(); + const byte2 = try reader.readByte(); + const cell = @intCast(u16, byte1) * 256 + @intCast(u16, byte2); + self.content[i] = cell; + } + } + return self; } @@ -234,5 +267,5 @@ test "load level from existing file" { const allocator = &arena.allocator; var level: Self = try Self.init_load(allocator, "sample.kble"); - //defer level.deinit(allocator); + defer level.deinit(allocator); } diff --git a/src/main.zig b/src/main.zig index 43429d0..ff51198 100644 --- a/src/main.zig +++ b/src/main.zig @@ -41,7 +41,8 @@ pub fn main() void { ray.SetTargetFPS(60); // Create level. - var level: Level = Level.init(allocator, 16, 16) catch unreachable; + //var level: Level = Level.init(allocator, 16, 16) catch unreachable; + var level: Level = Level.init_load(allocator, "sample.kble") catch unreachable; defer level.deinit(allocator); // Create camera.