feat: persist win count to disk
parent
5e7f978e74
commit
9de25c4129
23
src/main.zig
23
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue