From 9de25c4129050a74e1e427babeb88ac56f586f0f Mon Sep 17 00:00:00 2001 From: geemili Date: Sun, 21 Apr 2024 02:19:52 -0600 Subject: [PATCH] feat: persist win count to disk --- src/main.zig | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main.zig b/src/main.zig index da7a7db..03c0dd0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,6 +28,8 @@ const MENU_ITEMS = [_][]const u8{ "Quit", }; +var app_data_dir: std.fs.Dir = undefined; +var win_count_file: std.fs.File = undefined; var win_count: u32 = 0; var win_triggered: bool = false; @@ -83,6 +85,22 @@ pub fn init(context: *seizer.Context) !void { .on_event = toggleMenu, .default_bindings = &.{.start}, }); + + const app_data_dir_path = try std.fs.getAppDataDir(gpa, "seizer-solitaire"); + defer gpa.free(app_data_dir_path); + + app_data_dir = try std.fs.cwd().makeOpenPath(app_data_dir_path, .{}); + win_count_file = try app_data_dir.createFile("win_count.bin", .{ .read = true, .truncate = false }); + + var win_count_buffer: [4]u8 = undefined; + const bytes_read = try win_count_file.preadAll(&win_count_buffer, 0); + if (bytes_read < win_count_buffer.len) { + win_count = 0; + std.mem.writeInt(u32, &win_count_buffer, win_count, .little); + try win_count_file.pwriteAll(&win_count_buffer, 0); + } else { + win_count = std.mem.readInt(u32, &win_count_buffer, .little); + } } fn resetGame() !void { @@ -138,6 +156,11 @@ fn destroy(window: *seizer.Window) void { fn render(window: *seizer.Window) !void { if (haveWon() and !win_triggered) { win_count += 1; + + var win_count_buffer: [4]u8 = undefined; + std.mem.writeInt(u32, &win_count_buffer, win_count, .little); + try win_count_file.pwriteAll(&win_count_buffer, 0); + win_triggered = true; }