Bindings for saving/loading (read/write)!

This commit is contained in:
KikooDX 2021-02-24 15:48:32 +01:00
parent f24b6340a2
commit 5abd8cbff3
5 changed files with 42 additions and 6 deletions

View File

@ -42,6 +42,11 @@ Verbs:
* `-`: decrease scale (dezoom)
* `=`: reset scale
File (read/write):
* `e`: load level contained in `sample.kble`
* `w`: write level to `sample.kble`
*See `kbleformat.md` for technical informations.*
Modes:
* `<return>`: normal mode, default
* `i`: free selection mode

Binary file not shown.

View File

@ -19,6 +19,7 @@ pub const ActionCat = enum {
movement, // move and change selection
verb, // do stuff with selection
scale, // change draw scaling
file, // load/save, file related stuff
mode, // change mode
};
@ -28,6 +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,
next_mode: Mode = Mode.normal,
};
@ -110,6 +112,16 @@ pub const ActionsDef = .{
.function_scale = scaling.scale_down,
},
// File
.file_read = Action{
.category = ActionCat.file,
.function_file = Level.action_read,
},
.file_write = Action{
.category = ActionCat.file,
.function_file = Level.action_write,
},
// Mode change.
.mode_normal = Action{
.category = ActionCat.mode,

View File

@ -58,7 +58,7 @@ 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_load(allocator: *std.mem.Allocator, kble_file_path: []const u8) !Self {
pub fn init_read(allocator: *std.mem.Allocator, kble_file_path: []const u8) !Self {
var self: Self = undefined;
// Open directory.
@ -140,7 +140,7 @@ pub fn write(self: Self, kble_file_path: []const u8) !void {
const height_byte1: u8 = @intCast(u8, self.height / 256);
const height_byte2: u8 = @intCast(u8, self.height % 256);
std.log.info("{}·{} ; {}·{}", .{width_byte1, width_byte2, height_byte1, height_byte2});
std.log.info("{}·{} ; {}·{}", .{ width_byte1, width_byte2, height_byte1, height_byte2 });
try writer.writeByte(width_byte1);
try writer.writeByte(width_byte2);
@ -161,6 +161,17 @@ 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 {
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 {
self.write(kble_file_path) catch unreachable;
}
/// Draw level tiles from `offset` to fill the window.
pub fn draw(self: Self, scale: u16, offset: Vec2) void {
// Pixel position (were we draw).
@ -307,7 +318,7 @@ test "load level from sample file and save it" {
defer arena.deinit();
const allocator = &arena.allocator;
var level: Self = try Self.init_load(allocator, "sample.kble");
var level: Self = try Self.init_read(allocator, "sample.kble");
defer level.deinit(allocator);
try level.write("sample_out.kble");
@ -323,7 +334,7 @@ test "write large level to file and load it back" {
try level.write("sample_large.kble");
level.deinit(allocator);
level = try Self.init_load(allocator, "sample_large.kble");
level = try Self.init_read(allocator, "sample_large.kble");
defer level.deinit(allocator);
expect(level.width == 300);

View File

@ -26,6 +26,9 @@ 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 {
// Create allocator
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
@ -41,8 +44,7 @@ pub fn main() void {
ray.SetTargetFPS(60);
// Create level.
//var level: Level = Level.init(allocator, 16, 16) catch unreachable;
var level: Level = Level.init_load(allocator, "sample.kble") catch unreachable;
var level: Level = Level.init(allocator, 16, 16) catch unreachable;
defer level.deinit(allocator);
// Create camera.
@ -84,6 +86,9 @@ pub fn main() void {
bindings['='] = &ActionsDef.scale_reset;
bindings['+'] = &ActionsDef.scale_up;
bindings['-'] = &ActionsDef.scale_down;
// File.
bindings['e'] = &ActionsDef.file_read;
bindings['w'] = &ActionsDef.file_write;
// Mode.
bindings['\n'] = &ActionsDef.mode_normal;
bindings['i'] = &ActionsDef.mode_select;
@ -155,6 +160,9 @@ pub fn main() void {
.scale => {
scale = action.function_scale(scale);
},
.file => {
action.function_file(&level, allocator, level_path);
},
.mode => {
// Rectangle selection!
if (mode == Mode.rectangle) {