feat: update seizer to version with Platform refactor

ci-dev
LeRoyce Pearson 2024-05-25 22:14:16 -06:00
parent e4d34632aa
commit 9b8ec6de26
2 changed files with 53 additions and 58 deletions

View File

@ -16,8 +16,8 @@
// internet connectivity. // internet connectivity.
.dependencies = .{ .dependencies = .{
.seizer = .{ .seizer = .{
.url = "https://github.com/leroycep/seizer/archive/8fbc262657f505a11505d12eb277ff5736821769.tar.gz", .url = "https://github.com/leroycep/seizer/archive/d7213d4951e4da187123a29164ac487c6408b53b.tar.gz",
.hash = "1220b6df5a68c8c918d92befcfbe379010c7ddbe25fded0a2e103c4514012ad48b97", .hash = "1220f48f244eb92f096efd8ce2b7db41b35b039eb33ae5a7509a6003ad2fb7f9f271",
}, },
}, },
.paths = .{ .paths = .{

View File

@ -2,9 +2,7 @@ pub const main = seizer.main;
const APPNAME = "seizer-solitaire"; const APPNAME = "seizer-solitaire";
var gpa: std.mem.Allocator = undefined;
var prng: std.rand.DefaultPrng = undefined; var prng: std.rand.DefaultPrng = undefined;
var context_global: *seizer.Context = undefined;
var window_global: seizer.Window = undefined; var window_global: seizer.Window = undefined;
var canvas: seizer.Canvas = undefined; var canvas: seizer.Canvas = undefined;
var card_tilemap: ?DeckSprites = null; var card_tilemap: ?DeckSprites = null;
@ -37,26 +35,23 @@ var win_count: ?u32 = null;
var win_triggered: bool = false; var win_triggered: bool = false;
var frame_count: u64 = 0; var frame_count: u64 = 0;
pub fn init(context: *seizer.Context) !void { pub fn init() !void {
context_global = context;
gpa = context.gpa;
prng = std.rand.DefaultPrng.init(@bitCast(std.time.timestamp())); prng = std.rand.DefaultPrng.init(@bitCast(std.time.timestamp()));
window_global = try context.createWindow(.{ window_global = try seizer.platform.createWindow(.{
.title = "Seizer Solitaire", .title = "Seizer Solitaire",
.on_render = render, .on_render = render,
.on_destroy = destroy, .on_destroy = destroy,
}); });
canvas = try seizer.Canvas.init(context.gpa, .{}); canvas = try seizer.Canvas.init(seizer.platform.allocator(), .{});
errdefer canvas.deinit(); errdefer canvas.deinit();
card_tilemap = try assets.loadSolitaireCards(context.gpa); card_tilemap = try assets.loadSolitaireCards(seizer.platform.allocator());
try resetGame(); try resetGame();
try context.addButtonInput(.{ try seizer.platform.addButtonInput(.{
.title = "move_up", .title = "move_up",
.on_event = moveUp, .on_event = moveUp,
.default_bindings = &.{ .default_bindings = &.{
@ -64,7 +59,7 @@ pub fn init(context: *seizer.Context) !void {
.{ .keyboard = .up }, .{ .keyboard = .up },
}, },
}); });
try context.addButtonInput(.{ try seizer.platform.addButtonInput(.{
.title = "move_down", .title = "move_down",
.on_event = moveDown, .on_event = moveDown,
.default_bindings = &.{ .default_bindings = &.{
@ -72,7 +67,7 @@ pub fn init(context: *seizer.Context) !void {
.{ .keyboard = .down }, .{ .keyboard = .down },
}, },
}); });
try context.addButtonInput(.{ try seizer.platform.addButtonInput(.{
.title = "move_left", .title = "move_left",
.on_event = moveLeft, .on_event = moveLeft,
.default_bindings = &.{ .default_bindings = &.{
@ -80,7 +75,7 @@ pub fn init(context: *seizer.Context) !void {
.{ .keyboard = .left }, .{ .keyboard = .left },
}, },
}); });
try context.addButtonInput(.{ try seizer.platform.addButtonInput(.{
.title = "move_right", .title = "move_right",
.on_event = moveRight, .on_event = moveRight,
.default_bindings = &.{ .default_bindings = &.{
@ -88,7 +83,7 @@ pub fn init(context: *seizer.Context) !void {
.{ .keyboard = .right }, .{ .keyboard = .right },
}, },
}); });
try context.addButtonInput(.{ try seizer.platform.addButtonInput(.{
.title = "select_or_place", .title = "select_or_place",
.on_event = doSelectOrPlace, .on_event = doSelectOrPlace,
.default_bindings = &.{ .default_bindings = &.{
@ -96,7 +91,7 @@ pub fn init(context: *seizer.Context) !void {
.{ .keyboard = .z }, .{ .keyboard = .z },
}, },
}); });
try context.addButtonInput(.{ try seizer.platform.addButtonInput(.{
.title = "deselect", .title = "deselect",
.on_event = deselect, .on_event = deselect,
.default_bindings = &.{ .default_bindings = &.{
@ -104,7 +99,7 @@ pub fn init(context: *seizer.Context) !void {
.{ .keyboard = .x }, .{ .keyboard = .x },
}, },
}); });
try context.addButtonInput(.{ try seizer.platform.addButtonInput(.{
.title = "toggle_menu", .title = "toggle_menu",
.on_event = toggleMenu, .on_event = toggleMenu,
.default_bindings = &.{ .default_bindings = &.{
@ -112,7 +107,7 @@ pub fn init(context: *seizer.Context) !void {
.{ .keyboard = .esc }, .{ .keyboard = .esc },
}, },
}); });
try context.addButtonInput(.{ try seizer.platform.addButtonInput(.{
.title = "undo", .title = "undo",
.on_event = undo, .on_event = undo,
.default_bindings = &.{ .default_bindings = &.{
@ -121,8 +116,8 @@ pub fn init(context: *seizer.Context) !void {
}, },
}); });
const read_buffer = try gpa.alloc(u8, @sizeOf(u32)); const read_buffer = try seizer.platform.allocator().alloc(u8, @sizeOf(u32));
context.readFile(.{ seizer.platform.readFile(.{
.appname = APPNAME, .appname = APPNAME,
.path = "win_count.bin", .path = "win_count.bin",
.buffer = read_buffer, .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); const buffer: *[4]u8 = @ptrCast(userdata);
defer gpa.free(buffer); defer seizer.platform.allocator().free(buffer);
if (result) |bytes| { if (result) |bytes| {
if (bytes.len >= @sizeOf(u32)) { if (bytes.len >= @sizeOf(u32)) {
@ -142,15 +137,15 @@ fn onWinCountRead(userdata: ?*anyopaque, result: seizer.Context.FileError![]cons
} else |_| {} } 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)); const buffer: *u32 = @ptrCast(@alignCast(userdata));
defer gpa.destroy(buffer); defer seizer.platform.allocator().destroy(buffer);
_ = result catch {}; _ = result catch {};
} }
fn resetGame() !void { fn resetGame() !void {
resetHistory(); resetHistory();
draw_pile.deinit(gpa); draw_pile.deinit(seizer.platform.allocator());
draw_pile_exhausted = false; draw_pile_exhausted = false;
drawn_cards.shrinkRetainingCapacity(0); drawn_cards.shrinkRetainingCapacity(0);
for (&stacks) |*stack| { for (&stacks) |*stack| {
@ -168,13 +163,13 @@ fn resetGame() !void {
selected_deck = null; selected_deck = null;
selected_card = 0; 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); prng.random().shuffle(Card, draw_pile.items);
for (&stacks, 0..) |*stack, i| { for (&stacks, 0..) |*stack, i| {
for (0..i + 1) |_| { for (0..i + 1) |_| {
const drawn_card = draw_pile.pop(); 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| { for (history.items) |*snapshot| {
snapshot.deinit(); snapshot.deinit();
} }
history.deinit(gpa); history.deinit(seizer.platform.allocator());
draw_pile.deinit(gpa); draw_pile.deinit(seizer.platform.allocator());
drawn_cards.deinit(gpa); drawn_cards.deinit(seizer.platform.allocator());
for (&stacks) |*stack| { for (&stacks) |*stack| {
stack.deinit(gpa); stack.deinit(seizer.platform.allocator());
} }
for (&foundations) |*foundation| { for (&foundations) |*foundation| {
foundation.deinit(gpa); foundation.deinit(seizer.platform.allocator());
} }
if (card_tilemap) |*ct| { if (card_tilemap) |*ct| {
ct.deinit(gpa); ct.deinit(seizer.platform.allocator());
card_tilemap = null; card_tilemap = null;
} }
canvas.deinit(); canvas.deinit();
@ -215,9 +210,9 @@ fn render(window: seizer.Window) !void {
if (haveWon() and !win_triggered and win_count != null) { if (haveWon() and !win_triggered and win_count != null) {
win_count.? += 1; 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); std.mem.writeInt(u32, std.mem.asBytes(win_count_write_buffer), win_count.?, .little);
context_global.writeFile(.{ seizer.platform.writeFile(.{
.appname = APPNAME, .appname = APPNAME,
.path = "win_count.bin", .path = "win_count.bin",
.data = std.mem.asBytes(win_count_write_buffer), .data = std.mem.asBytes(win_count_write_buffer),
@ -505,7 +500,7 @@ pub fn doSelectOrPlace(pressed: bool) !void {
if (selected_deck == null) { if (selected_deck == null) {
if (hovered_deck == &draw_pile and !draw_pile_exhausted) { if (hovered_deck == &draw_pile and !draw_pile_exhausted) {
resetHistory(); resetHistory();
drawn_cards.ensureUnusedCapacity(gpa, 3) catch return; drawn_cards.ensureUnusedCapacity(seizer.platform.allocator(), 3) catch return;
for (0..3) |_| { for (0..3) |_| {
if (draw_pile.popOrNull()) |card| drawn_cards.appendAssumeCapacity(card); 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 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; 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)) { 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()); foundation.appendAssumeCapacity(selected_deck.?.pop());
hovered_card = if (hovered_deck.? == &drawn_cards) hovered_card = if (hovered_deck.? == &drawn_cards)
@ -560,7 +555,7 @@ pub fn doSelectOrPlace(pressed: bool) !void {
} }
if (cards_moved) { if (cards_moved) {
try history.append(gpa, snapshot); try history.append(seizer.platform.allocator(), snapshot);
} else { } else {
snapshot.deinit(); snapshot.deinit();
} }
@ -578,7 +573,7 @@ pub fn doSelectOrPlace(pressed: bool) !void {
if (hovered.items.len == 0) { if (hovered.items.len == 0) {
for (stacks[0..]) |*stack| { for (stacks[0..]) |*stack| {
if (hovered != stack) continue; 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); hovered.appendSliceAssumeCapacity(selected_substack);
selected.shrinkRetainingCapacity(selected.items.len - selected_substack.len); selected.shrinkRetainingCapacity(selected.items.len - selected_substack.len);
hovered_card = indexOfTopOfStack(hovered_deck.?.items); hovered_card = indexOfTopOfStack(hovered_deck.?.items);
@ -588,7 +583,7 @@ pub fn doSelectOrPlace(pressed: bool) !void {
if (selected_substack.len == 1) { if (selected_substack.len == 1) {
if (draw_pile_exhausted and hovered == &draw_pile and draw_pile.items.len == 0) { 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.appendAssumeCapacity(selected_deck.?.pop());
hovered_card = indexOfTopOfStack(hovered_deck.?.items); hovered_card = indexOfTopOfStack(hovered_deck.?.items);
cards_moved = true; 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 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; 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)) { 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()); foundation.appendAssumeCapacity(selected_deck.?.pop());
hovered_card = indexOfTopOfStack(hovered_deck.?.items); hovered_card = indexOfTopOfStack(hovered_deck.?.items);
cards_moved = true; cards_moved = true;
@ -617,14 +612,14 @@ pub fn doSelectOrPlace(pressed: bool) !void {
{ {
break :move_from_selected_to_hovered; 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); hovered.appendSliceAssumeCapacity(selected_substack);
selected.shrinkRetainingCapacity(selected.items.len - selected_substack.len); selected.shrinkRetainingCapacity(selected.items.len - selected_substack.len);
hovered_card = indexOfTopOfStack(hovered_deck.?.items); hovered_card = indexOfTopOfStack(hovered_deck.?.items);
cards_moved = true; cards_moved = true;
} }
if (cards_moved) { if (cards_moved) {
try history.append(gpa, snapshot); try history.append(seizer.platform.allocator(), snapshot);
} else { } else {
snapshot.deinit(); snapshot.deinit();
} }
@ -826,19 +821,19 @@ const Snapshot = struct {
pub fn initFromCurrentGlobalState() !Snapshot { pub fn initFromCurrentGlobalState() !Snapshot {
const snapshot_selected_deck = selected_deck orelse return error.InvalidSelectionForSnapshot; const snapshot_selected_deck = selected_deck orelse return error.InvalidSelectionForSnapshot;
const snapshot_draw_pile = try gpa.dupe(Card, draw_pile.items); const snapshot_draw_pile = try seizer.platform.allocator().dupe(Card, draw_pile.items);
errdefer gpa.free(snapshot_draw_pile); errdefer seizer.platform.allocator().free(snapshot_draw_pile);
const snapshot_drawn_cards = try gpa.dupe(Card, drawn_cards.items); const snapshot_drawn_cards = try seizer.platform.allocator().dupe(Card, drawn_cards.items);
errdefer gpa.free(snapshot_drawn_cards); errdefer seizer.platform.allocator().free(snapshot_drawn_cards);
var snapshot_stacks: [7][]Card = undefined; var snapshot_stacks: [7][]Card = undefined;
for (&snapshot_stacks, &stacks) |*snapshot_stack, stack| { 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; var snapshot_foundations: [4][]Card = undefined;
for (&snapshot_foundations, &foundations) |*snapshot_foundation, foundation| { 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{ return Snapshot{
@ -853,25 +848,25 @@ const Snapshot = struct {
} }
pub fn deinit(snapshot: *@This()) void { pub fn deinit(snapshot: *@This()) void {
gpa.free(snapshot.draw_pile); seizer.platform.allocator().free(snapshot.draw_pile);
gpa.free(snapshot.drawn_cards); seizer.platform.allocator().free(snapshot.drawn_cards);
for (snapshot.stacks) |stack| { for (snapshot.stacks) |stack| {
gpa.free(stack); seizer.platform.allocator().free(stack);
} }
for (snapshot.foundations) |foundation| { for (snapshot.foundations) |foundation| {
gpa.free(foundation); seizer.platform.allocator().free(foundation);
} }
} }
pub fn restore(snapshot: @This()) !void { pub fn restore(snapshot: @This()) !void {
// ensure there is enough space for all the cards // ensure there is enough space for all the cards
try draw_pile.ensureTotalCapacity(gpa, snapshot.draw_pile.len); try draw_pile.ensureTotalCapacity(seizer.platform.allocator(), snapshot.draw_pile.len);
try drawn_cards.ensureTotalCapacity(gpa, snapshot.drawn_cards.len); try drawn_cards.ensureTotalCapacity(seizer.platform.allocator(), snapshot.drawn_cards.len);
for (&snapshot.stacks, &stacks) |snapshot_stack, *stack| { 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| { 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 // reset the state