From 3c084730b2a7dd1ad59c70672e5bdec9221f0b1b Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 24 Feb 2021 23:11:06 +0100 Subject: [PATCH] Command line argument ; take path to open and/or save to. + README.md update --- README.md | 21 ++++++++++++++++----- src/actions.zig | 2 +- src/level.zig | 12 ++++++------ src/main.zig | 16 +++++++++++----- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3c68ed2..dc44033 100644 --- a/README.md +++ b/README.md @@ -13,15 +13,26 @@ Subject to change. # Build instructions Build requirements: [Zig](https://ziglang.org/download/) master branch, -[raylib](https://www.raylib.com). For Arch users, `zig-git` is available -in the AUR. +[raylib](https://www.raylib.com). +For Arch users, `zig-dev-bin` and `zig-git` are available in the AUR. ```sh $ # Clone this repository $ git clone https://git.sr.ht/~kikoodx/kble && cd kble -$ zig build run +$ zig build +$ zig-cache/bin/kble ``` Press escape to close the program. +# Command line argument +**Not available on Windows.** Accept a path to KBLE file to open. +If the file doesn't exist, the given path will be used for saving. +Defaults to `level.kble` if no argument given. +```sh +$ kble #level.kble +$ kble sample.kble +$ kble $HOME/projects/kble/sample.kble +``` + # Default keybindings Movement: * `h`: left @@ -42,8 +53,8 @@ Verbs: * `=`: reset scale File (read/write): -* `e`: load level contained in `sample.kble` -* `w`: write level to `sample.kble` +* `e`: read (load) level +* `w`: write (save) level *See `kbleformat.md` for technical informations.* Modes: diff --git a/src/actions.zig b/src/actions.zig index bca6ad7..2b868f8 100644 --- a/src/actions.zig +++ b/src/actions.zig @@ -29,7 +29,7 @@ pub const Action = struct { function_move: fn (*Vec2, conf.arg_type) movement.SelectionUpdate = movement.move_left, function_verb: fn (*Level, conf.arg_type) void = verbs.delete, function_scale: fn (scaling.scale_type) scaling.scale_type = scaling.scale_reset, - function_file: fn (*Level, *std.mem.Allocator, []const u8) void = Level.action_write, + function_file: fn (*Level, *std.mem.Allocator, [*:0]const u8) void = Level.action_write, next_mode: Mode = Mode.normal, }; diff --git a/src/level.zig b/src/level.zig index 013d0fd..70961a5 100644 --- a/src/level.zig +++ b/src/level.zig @@ -57,13 +57,13 @@ pub fn deinit(self: *Self, allocator: *std.mem.Allocator) void { /// Load level content from given absolute or relative path. /// Expect the KBLE file format (see `kbleformat.md` for more details). -pub fn init_read(allocator: *std.mem.Allocator, kble_file_path: []const u8) !Self { +pub fn init_read(allocator: *std.mem.Allocator, kble_file_path: [*:0]const u8) !Self { var self: Self = undefined; // Open directory. var dir: std.fs.Dir = std.fs.cwd(); // Open file in read mode. - const file: std.fs.File = try dir.openFile(kble_file_path, std.fs.File.OpenFlags{ + const file: std.fs.File = try dir.openFileZ(kble_file_path, std.fs.File.OpenFlags{ .read = true, .write = false, }); @@ -126,11 +126,11 @@ pub fn init_read(allocator: *std.mem.Allocator, kble_file_path: []const u8) !Sel /// Write header and level content to given absolute or relative path. /// Uses the KBLE file format (see `kbleformat.md` for more details). -pub fn write(self: Self, kble_file_path: []const u8) !void { +pub fn write(self: Self, kble_file_path: [*:0]const u8) !void { // Open directory. var dir: std.fs.Dir = std.fs.cwd(); // Open file in write mode. - const file: std.fs.File = try dir.createFile(kble_file_path, std.fs.File.CreateFlags{}); + const file: std.fs.File = try dir.createFileZ(kble_file_path, std.fs.File.CreateFlags{}); defer file.close(); // Get writer. var writer: std.fs.File.Writer = file.writer(); @@ -169,13 +169,13 @@ pub fn write(self: Self, kble_file_path: []const u8) !void { } /// Wrapper around `init_read`. -pub fn action_read(self: *Self, allocator: *std.mem.Allocator, kble_file_path: []const u8) void { +pub fn action_read(self: *Self, allocator: *std.mem.Allocator, kble_file_path: [*:0]const u8) void { self.deinit(allocator); self.* = Self.init_read(allocator, kble_file_path) catch unreachable; } /// Wrapper around `write`. -pub fn action_write(self: *Self, allocator: *std.mem.Allocator, kble_file_path: []const u8) void { +pub fn action_write(self: *Self, allocator: *std.mem.Allocator, kble_file_path: [*:0]const u8) void { self.write(kble_file_path) catch unreachable; } diff --git a/src/main.zig b/src/main.zig index cc44854..9e998cb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -26,10 +26,14 @@ const Mode = @import("modes.zig").Mode; const char_range = 255; -// TODO: make this a command line parameter. -const level_path = "sample.kble"; - pub fn main() void { + const level_path: [*:0]const u8 = if (std.os.argv.len > 1) + std.os.argv[1] + else nopath: { + std.log.notice("No path provided, defaults to \"level.kble\".", .{}); + break :nopath "level.kble"; + }; + // Create allocator var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); @@ -43,8 +47,10 @@ pub fn main() void { // Limit FPS for performance. ray.SetTargetFPS(60); - // Create level. - var level: Level = Level.init(allocator, 16, 16) catch unreachable; + // Try to load level, is doesn't exist create it. + var level: Level = Level.init_read(allocator, level_path) catch create: { + break :create Level.init(allocator, 16, 16) catch unreachable; + }; defer level.deinit(allocator); // Create camera.