From 9b8ec6de2630cf887a6557765eedee2127f459dc Mon Sep 17 00:00:00 2001 From: geemili Date: Sat, 25 May 2024 22:14:16 -0600 Subject: [PATCH] feat: update seizer to version with Platform refactor --- build.zig.zon | 4 +- src/main.zig | 107 ++++++++++++++++++++++++-------------------------- 2 files changed, 53 insertions(+), 58 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index c146c9d..bb26f1d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -16,8 +16,8 @@ // internet connectivity. .dependencies = .{ .seizer = .{ - .url = "https://github.com/leroycep/seizer/archive/8fbc262657f505a11505d12eb277ff5736821769.tar.gz", - .hash = "1220b6df5a68c8c918d92befcfbe379010c7ddbe25fded0a2e103c4514012ad48b97", + .url = "https://github.com/leroycep/seizer/archive/d7213d4951e4da187123a29164ac487c6408b53b.tar.gz", + .hash = "1220f48f244eb92f096efd8ce2b7db41b35b039eb33ae5a7509a6003ad2fb7f9f271", }, }, .paths = .{ diff --git a/src/main.zig b/src/main.zig index 101a069..59c516f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,9 +2,7 @@ pub const main = seizer.main; const APPNAME = "seizer-solitaire"; -var gpa: std.mem.Allocator = undefined; var prng: std.rand.DefaultPrng = undefined; -var context_global: *seizer.Context = undefined; var window_global: seizer.Window = undefined; var canvas: seizer.Canvas = undefined; var card_tilemap: ?DeckSprites = null; @@ -37,26 +35,23 @@ var win_count: ?u32 = null; var win_triggered: bool = false; var frame_count: u64 = 0; -pub fn init(context: *seizer.Context) !void { - context_global = context; - - gpa = context.gpa; +pub fn init() !void { prng = std.rand.DefaultPrng.init(@bitCast(std.time.timestamp())); - window_global = try context.createWindow(.{ + window_global = try seizer.platform.createWindow(.{ .title = "Seizer Solitaire", .on_render = render, .on_destroy = destroy, }); - canvas = try seizer.Canvas.init(context.gpa, .{}); + canvas = try seizer.Canvas.init(seizer.platform.allocator(), .{}); errdefer canvas.deinit(); - card_tilemap = try assets.loadSolitaireCards(context.gpa); + card_tilemap = try assets.loadSolitaireCards(seizer.platform.allocator()); try resetGame(); - try context.addButtonInput(.{ + try seizer.platform.addButtonInput(.{ .title = "move_up", .on_event = moveUp, .default_bindings = &.{ @@ -64,7 +59,7 @@ pub fn init(context: *seizer.Context) !void { .{ .keyboard = .up }, }, }); - try context.addButtonInput(.{ + try seizer.platform.addButtonInput(.{ .title = "move_down", .on_event = moveDown, .default_bindings = &.{ @@ -72,7 +67,7 @@ pub fn init(context: *seizer.Context) !void { .{ .keyboard = .down }, }, }); - try context.addButtonInput(.{ + try seizer.platform.addButtonInput(.{ .title = "move_left", .on_event = moveLeft, .default_bindings = &.{ @@ -80,7 +75,7 @@ pub fn init(context: *seizer.Context) !void { .{ .keyboard = .left }, }, }); - try context.addButtonInput(.{ + try seizer.platform.addButtonInput(.{ .title = "move_right", .on_event = moveRight, .default_bindings = &.{ @@ -88,7 +83,7 @@ pub fn init(context: *seizer.Context) !void { .{ .keyboard = .right }, }, }); - try context.addButtonInput(.{ + try seizer.platform.addButtonInput(.{ .title = "select_or_place", .on_event = doSelectOrPlace, .default_bindings = &.{ @@ -96,7 +91,7 @@ pub fn init(context: *seizer.Context) !void { .{ .keyboard = .z }, }, }); - try context.addButtonInput(.{ + try seizer.platform.addButtonInput(.{ .title = "deselect", .on_event = deselect, .default_bindings = &.{ @@ -104,7 +99,7 @@ pub fn init(context: *seizer.Context) !void { .{ .keyboard = .x }, }, }); - try context.addButtonInput(.{ + try seizer.platform.addButtonInput(.{ .title = "toggle_menu", .on_event = toggleMenu, .default_bindings = &.{ @@ -112,7 +107,7 @@ pub fn init(context: *seizer.Context) !void { .{ .keyboard = .esc }, }, }); - try context.addButtonInput(.{ + try seizer.platform.addButtonInput(.{ .title = "undo", .on_event = undo, .default_bindings = &.{ @@ -121,8 +116,8 @@ pub fn init(context: *seizer.Context) !void { }, }); - const read_buffer = try gpa.alloc(u8, @sizeOf(u32)); - context.readFile(.{ + const read_buffer = try seizer.platform.allocator().alloc(u8, @sizeOf(u32)); + seizer.platform.readFile(.{ .appname = APPNAME, .path = "win_count.bin", .buffer = read_buffer, @@ -131,9 +126,9 @@ pub fn init(context: *seizer.Context) !void { }); } -fn onWinCountRead(userdata: ?*anyopaque, result: seizer.Context.FileError![]const u8) void { +fn onWinCountRead(userdata: ?*anyopaque, result: seizer.Platform.FileError![]const u8) void { const buffer: *[4]u8 = @ptrCast(userdata); - defer gpa.free(buffer); + defer seizer.platform.allocator().free(buffer); if (result) |bytes| { if (bytes.len >= @sizeOf(u32)) { @@ -142,15 +137,15 @@ fn onWinCountRead(userdata: ?*anyopaque, result: seizer.Context.FileError![]cons } else |_| {} } -fn onWinCountWritten(userdata: ?*anyopaque, result: seizer.Context.FileError!void) void { +fn onWinCountWritten(userdata: ?*anyopaque, result: seizer.Platform.FileError!void) void { const buffer: *u32 = @ptrCast(@alignCast(userdata)); - defer gpa.destroy(buffer); + defer seizer.platform.allocator().destroy(buffer); _ = result catch {}; } fn resetGame() !void { resetHistory(); - draw_pile.deinit(gpa); + draw_pile.deinit(seizer.platform.allocator()); draw_pile_exhausted = false; drawn_cards.shrinkRetainingCapacity(0); for (&stacks) |*stack| { @@ -168,13 +163,13 @@ fn resetGame() !void { selected_deck = null; selected_card = 0; - draw_pile = std.ArrayListUnmanaged(Card).fromOwnedSlice(try makeStandardDeck(gpa)); + draw_pile = std.ArrayListUnmanaged(Card).fromOwnedSlice(try makeStandardDeck(seizer.platform.allocator())); prng.random().shuffle(Card, draw_pile.items); for (&stacks, 0..) |*stack, i| { for (0..i + 1) |_| { const drawn_card = draw_pile.pop(); - try stack.append(gpa, drawn_card); + try stack.append(seizer.platform.allocator(), drawn_card); } } @@ -193,19 +188,19 @@ fn destroy(window: seizer.Window) void { for (history.items) |*snapshot| { snapshot.deinit(); } - history.deinit(gpa); + history.deinit(seizer.platform.allocator()); - draw_pile.deinit(gpa); - drawn_cards.deinit(gpa); + draw_pile.deinit(seizer.platform.allocator()); + drawn_cards.deinit(seizer.platform.allocator()); for (&stacks) |*stack| { - stack.deinit(gpa); + stack.deinit(seizer.platform.allocator()); } for (&foundations) |*foundation| { - foundation.deinit(gpa); + foundation.deinit(seizer.platform.allocator()); } if (card_tilemap) |*ct| { - ct.deinit(gpa); + ct.deinit(seizer.platform.allocator()); card_tilemap = null; } canvas.deinit(); @@ -215,9 +210,9 @@ fn render(window: seizer.Window) !void { if (haveWon() and !win_triggered and win_count != null) { win_count.? += 1; - const win_count_write_buffer = try gpa.create(u32); + const win_count_write_buffer = try seizer.platform.allocator().create(u32); std.mem.writeInt(u32, std.mem.asBytes(win_count_write_buffer), win_count.?, .little); - context_global.writeFile(.{ + seizer.platform.writeFile(.{ .appname = APPNAME, .path = "win_count.bin", .data = std.mem.asBytes(win_count_write_buffer), @@ -505,7 +500,7 @@ pub fn doSelectOrPlace(pressed: bool) !void { if (selected_deck == null) { if (hovered_deck == &draw_pile and !draw_pile_exhausted) { resetHistory(); - drawn_cards.ensureUnusedCapacity(gpa, 3) catch return; + drawn_cards.ensureUnusedCapacity(seizer.platform.allocator(), 3) catch return; for (0..3) |_| { if (draw_pile.popOrNull()) |card| drawn_cards.appendAssumeCapacity(card); } @@ -543,7 +538,7 @@ pub fn doSelectOrPlace(pressed: bool) !void { const suit_matches = !empty and foundation.items[foundation.items.len - 1].suit == selected_deck.?.items[selected_deck.?.items.len - 1].suit; const is_next_rank = !empty and foundation.items[foundation.items.len - 1].rank + 1 == selected_deck.?.items[selected_deck.?.items.len - 1].rank; if ((empty and ace) or (suit_matches and is_next_rank)) { - foundation.ensureUnusedCapacity(gpa, 1) catch continue; + foundation.ensureUnusedCapacity(seizer.platform.allocator(), 1) catch continue; foundation.appendAssumeCapacity(selected_deck.?.pop()); hovered_card = if (hovered_deck.? == &drawn_cards) @@ -560,7 +555,7 @@ pub fn doSelectOrPlace(pressed: bool) !void { } if (cards_moved) { - try history.append(gpa, snapshot); + try history.append(seizer.platform.allocator(), snapshot); } else { snapshot.deinit(); } @@ -578,7 +573,7 @@ pub fn doSelectOrPlace(pressed: bool) !void { if (hovered.items.len == 0) { for (stacks[0..]) |*stack| { if (hovered != stack) continue; - hovered.ensureUnusedCapacity(gpa, selected_substack.len) catch break :move_from_selected_to_hovered; + hovered.ensureUnusedCapacity(seizer.platform.allocator(), selected_substack.len) catch break :move_from_selected_to_hovered; hovered.appendSliceAssumeCapacity(selected_substack); selected.shrinkRetainingCapacity(selected.items.len - selected_substack.len); hovered_card = indexOfTopOfStack(hovered_deck.?.items); @@ -588,7 +583,7 @@ pub fn doSelectOrPlace(pressed: bool) !void { if (selected_substack.len == 1) { if (draw_pile_exhausted and hovered == &draw_pile and draw_pile.items.len == 0) { - hovered.ensureUnusedCapacity(gpa, 1) catch break :move_from_selected_to_hovered; + hovered.ensureUnusedCapacity(seizer.platform.allocator(), 1) catch break :move_from_selected_to_hovered; hovered.appendAssumeCapacity(selected_deck.?.pop()); hovered_card = indexOfTopOfStack(hovered_deck.?.items); cards_moved = true; @@ -600,7 +595,7 @@ pub fn doSelectOrPlace(pressed: bool) !void { const suit_matches = !empty and foundation.items[foundation.items.len - 1].suit == selected_deck.?.items[selected_deck.?.items.len - 1].suit; const is_next_rank = !empty and foundation.items[foundation.items.len - 1].rank + 1 == selected_deck.?.items[selected_deck.?.items.len - 1].rank; if ((empty and ace) or (suit_matches and is_next_rank)) { - foundation.ensureUnusedCapacity(gpa, 1) catch continue; + foundation.ensureUnusedCapacity(seizer.platform.allocator(), 1) catch continue; foundation.appendAssumeCapacity(selected_deck.?.pop()); hovered_card = indexOfTopOfStack(hovered_deck.?.items); cards_moved = true; @@ -617,14 +612,14 @@ pub fn doSelectOrPlace(pressed: bool) !void { { break :move_from_selected_to_hovered; } - hovered.ensureUnusedCapacity(gpa, selected_substack.len) catch break :move_from_selected_to_hovered; + hovered.ensureUnusedCapacity(seizer.platform.allocator(), selected_substack.len) catch break :move_from_selected_to_hovered; hovered.appendSliceAssumeCapacity(selected_substack); selected.shrinkRetainingCapacity(selected.items.len - selected_substack.len); hovered_card = indexOfTopOfStack(hovered_deck.?.items); cards_moved = true; } if (cards_moved) { - try history.append(gpa, snapshot); + try history.append(seizer.platform.allocator(), snapshot); } else { snapshot.deinit(); } @@ -826,19 +821,19 @@ const Snapshot = struct { pub fn initFromCurrentGlobalState() !Snapshot { const snapshot_selected_deck = selected_deck orelse return error.InvalidSelectionForSnapshot; - const snapshot_draw_pile = try gpa.dupe(Card, draw_pile.items); - errdefer gpa.free(snapshot_draw_pile); - const snapshot_drawn_cards = try gpa.dupe(Card, drawn_cards.items); - errdefer gpa.free(snapshot_drawn_cards); + const snapshot_draw_pile = try seizer.platform.allocator().dupe(Card, draw_pile.items); + errdefer seizer.platform.allocator().free(snapshot_draw_pile); + const snapshot_drawn_cards = try seizer.platform.allocator().dupe(Card, drawn_cards.items); + errdefer seizer.platform.allocator().free(snapshot_drawn_cards); var snapshot_stacks: [7][]Card = undefined; for (&snapshot_stacks, &stacks) |*snapshot_stack, stack| { - snapshot_stack.* = try gpa.dupe(Card, stack.items); + snapshot_stack.* = try seizer.platform.allocator().dupe(Card, stack.items); } var snapshot_foundations: [4][]Card = undefined; for (&snapshot_foundations, &foundations) |*snapshot_foundation, foundation| { - snapshot_foundation.* = try gpa.dupe(Card, foundation.items); + snapshot_foundation.* = try seizer.platform.allocator().dupe(Card, foundation.items); } return Snapshot{ @@ -853,25 +848,25 @@ const Snapshot = struct { } pub fn deinit(snapshot: *@This()) void { - gpa.free(snapshot.draw_pile); - gpa.free(snapshot.drawn_cards); + seizer.platform.allocator().free(snapshot.draw_pile); + seizer.platform.allocator().free(snapshot.drawn_cards); for (snapshot.stacks) |stack| { - gpa.free(stack); + seizer.platform.allocator().free(stack); } for (snapshot.foundations) |foundation| { - gpa.free(foundation); + seizer.platform.allocator().free(foundation); } } pub fn restore(snapshot: @This()) !void { // ensure there is enough space for all the cards - try draw_pile.ensureTotalCapacity(gpa, snapshot.draw_pile.len); - try drawn_cards.ensureTotalCapacity(gpa, snapshot.drawn_cards.len); + try draw_pile.ensureTotalCapacity(seizer.platform.allocator(), snapshot.draw_pile.len); + try drawn_cards.ensureTotalCapacity(seizer.platform.allocator(), snapshot.drawn_cards.len); for (&snapshot.stacks, &stacks) |snapshot_stack, *stack| { - try stack.ensureTotalCapacity(gpa, snapshot_stack.len); + try stack.ensureTotalCapacity(seizer.platform.allocator(), snapshot_stack.len); } for (&snapshot.foundations, &foundations) |snapshot_foundation, *foundation| { - try foundation.ensureTotalCapacity(gpa, snapshot_foundation.len); + try foundation.ensureTotalCapacity(seizer.platform.allocator(), snapshot_foundation.len); } // reset the state