feat: persist win count to disk

ci-dev
LeRoyce Pearson 2024-04-21 02:19:52 -06:00
parent 5e7f978e74
commit 9de25c4129
1 changed files with 23 additions and 0 deletions

View File

@ -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;
}