feat: show throbber while win count is loading

ci-dev
LeRoyce Pearson 2024-05-25 14:55:31 -06:00
parent e157502750
commit e4d34632aa
1 changed files with 31 additions and 11 deletions

View File

@ -8,7 +8,6 @@ var context_global: *seizer.Context = undefined;
var window_global: seizer.Window = undefined;
var canvas: seizer.Canvas = undefined;
var card_tilemap: ?DeckSprites = null;
var invert_y: bool = false;
var draw_pile: std.ArrayListUnmanaged(Card) = .{};
var draw_pile_exhausted = false;
@ -34,8 +33,9 @@ const MENU_ITEMS = [_][]const u8{
"Quit",
};
var win_count: u32 = 0;
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;
@ -52,8 +52,6 @@ pub fn init(context: *seizer.Context) !void {
canvas = try seizer.Canvas.init(context.gpa, .{});
errdefer canvas.deinit();
invert_y = std.mem.eql(u8, context.backend.name, "linux");
card_tilemap = try assets.loadSolitaireCards(context.gpa);
try resetGame();
@ -138,7 +136,7 @@ fn onWinCountRead(userdata: ?*anyopaque, result: seizer.Context.FileError![]cons
defer gpa.free(buffer);
if (result) |bytes| {
if (bytes.len >= @sizeOf(@TypeOf(win_count))) {
if (bytes.len >= @sizeOf(u32)) {
win_count = std.mem.readInt(u32, bytes[0..4], .little);
}
} else |_| {}
@ -214,11 +212,11 @@ fn destroy(window: seizer.Window) void {
}
fn render(window: seizer.Window) !void {
if (haveWon() and !win_triggered) {
win_count += 1;
if (haveWon() and !win_triggered and win_count != null) {
win_count.? += 1;
const win_count_write_buffer = try gpa.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(.{
.appname = APPNAME,
.path = "win_count.bin",
@ -237,7 +235,6 @@ fn render(window: seizer.Window) !void {
canvas.begin(.{
.window_size = window.getSize(),
.framebuffer_size = window.getFramebufferSize(),
.invert_y = invert_y,
});
const scale: u32 = if (canvas.window_size[1] <= 400) 1 else 2;
@ -267,14 +264,35 @@ fn render(window: seizer.Window) !void {
(canvas.window_size[1] - space_taken_by_board[1]) / 2,
};
{
if (win_count) |w| {
_ = canvas.printText(
.{
canvas.window_size[0] - board_offset[0] - marginf - hand_offsetf[0],
board_offset[1] + marginf + hand_offsetf[1],
},
"Win Count: {}",
.{win_count},
.{w},
.{ .@"align" = .right, .scale = scalef },
);
} else {
const throbber_character: u8 = switch ((frame_count / 5) % 8) {
0 => '-',
1 => '\\',
2 => '|',
3 => '/',
4 => '-',
5 => '\\',
6 => '|',
7 => '/',
else => unreachable,
};
_ = canvas.printText(
.{
canvas.window_size[0] - board_offset[0] - marginf - hand_offsetf[0],
board_offset[1] + marginf + hand_offsetf[1],
},
"Win Count: [{c}]",
.{throbber_character},
.{ .@"align" = .right, .scale = scalef },
);
}
@ -432,6 +450,8 @@ fn render(window: seizer.Window) !void {
}
canvas.end();
frame_count +%= 1;
}
pub fn haveWon() bool {