feat: add web support; add keyboard support

ci-dev
LeRoyce Pearson 2024-05-23 16:25:01 -06:00
parent 813b7aad2b
commit 812caef88a
3 changed files with 84 additions and 26 deletions

View File

@ -29,6 +29,19 @@ pub fn build(b: *std.Build) void {
exe.root_module.addImport("seizer", seizer.module("seizer")); exe.root_module.addImport("seizer", seizer.module("seizer"));
b.installArtifact(exe); b.installArtifact(exe);
// additionally generate an HTML file with the wasm module embedded when we use the wasi target
if (target.result.os.tag == .wasi) {
exe.wasi_exec_model = .reactor;
const bundle_webpage_exe = seizer.artifact("bundle-webpage");
const bundle_webpage = b.addRunArtifact(bundle_webpage_exe);
bundle_webpage.addArtifactArg(exe);
const install_html = b.addInstallFile(bundle_webpage.captureStdOut(), "www/seizer-solitaire.html");
b.getInstallStep().dependOn(&install_html.step);
}
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());

View File

@ -16,8 +16,8 @@
// internet connectivity. // internet connectivity.
.dependencies = .{ .dependencies = .{
.seizer = .{ .seizer = .{
.url = "https://github.com/leroycep/seizer/archive/d6bd2d96267e5eb39c40644e3f96b649be9075cc.tar.gz", .url = "https://github.com/leroycep/seizer/archive/c66b5e5f1148a1f6ce37560ed1b8bc883520541e.tar.gz",
.hash = "1220d74e813694d19206bf5dd71134778da933912867cf2bc29698670d4692f8db9c", .hash = "122051972ed81522b6d1a8e1306f0cd3848a81180781794dce80fefcf1e42dc00d30",
}, },
}, },
.paths = .{ .paths = .{

View File

@ -31,8 +31,6 @@ const MENU_ITEMS = [_][]const u8{
"Quit", "Quit",
}; };
var app_data_dir: std.fs.Dir = undefined;
var win_count_file: std.fs.File = undefined;
var win_count: u32 = 0; var win_count: u32 = 0;
var win_triggered: bool = false; var win_triggered: bool = false;
@ -58,59 +56,107 @@ pub fn init(context: *seizer.Context) !void {
try context.addButtonInput(.{ try context.addButtonInput(.{
.title = "move_up", .title = "move_up",
.on_event = moveUp, .on_event = moveUp,
.default_bindings = &.{.dpup}, .default_bindings = &.{
.{ .gamepad = .dpup },
.{ .keyboard = .up },
},
}); });
try context.addButtonInput(.{ try context.addButtonInput(.{
.title = "move_down", .title = "move_down",
.on_event = moveDown, .on_event = moveDown,
.default_bindings = &.{.dpdown}, .default_bindings = &.{
.{ .gamepad = .dpdown },
.{ .keyboard = .down },
},
}); });
try context.addButtonInput(.{ try context.addButtonInput(.{
.title = "move_left", .title = "move_left",
.on_event = moveLeft, .on_event = moveLeft,
.default_bindings = &.{.dpleft}, .default_bindings = &.{
.{ .gamepad = .dpleft },
.{ .keyboard = .left },
},
}); });
try context.addButtonInput(.{ try context.addButtonInput(.{
.title = "move_right", .title = "move_right",
.on_event = moveRight, .on_event = moveRight,
.default_bindings = &.{.dpright}, .default_bindings = &.{
.{ .gamepad = .dpright },
.{ .keyboard = .right },
},
}); });
try context.addButtonInput(.{ try context.addButtonInput(.{
.title = "select_or_place", .title = "select_or_place",
.on_event = doSelectOrPlace, .on_event = doSelectOrPlace,
.default_bindings = &.{.a}, .default_bindings = &.{
.{ .gamepad = .a },
.{ .keyboard = .z },
},
}); });
try context.addButtonInput(.{ try context.addButtonInput(.{
.title = "deselect", .title = "deselect",
.on_event = deselect, .on_event = deselect,
.default_bindings = &.{.b}, .default_bindings = &.{
.{ .gamepad = .b },
.{ .keyboard = .x },
},
}); });
try context.addButtonInput(.{ try context.addButtonInput(.{
.title = "toggle_menu", .title = "toggle_menu",
.on_event = toggleMenu, .on_event = toggleMenu,
.default_bindings = &.{.start}, .default_bindings = &.{
.{ .gamepad = .start },
},
}); });
try context.addButtonInput(.{ try context.addButtonInput(.{
.title = "undo", .title = "undo",
.on_event = undo, .on_event = undo,
.default_bindings = &.{.x}, .default_bindings = &.{
.{ .gamepad = .x },
},
}); });
const app_data_dir_path = try std.fs.getAppDataDir(gpa, "seizer-solitaire"); win_count = try loadWinCount(gpa);
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 loadWinCount(allocator: std.mem.Allocator) !u32 {
if (builtin.target.os.tag == .wasi) return 0;
const app_data_dir_path = try std.fs.getAppDataDir(allocator, "seizer-solitaire");
defer allocator.free(app_data_dir_path);
var app_data_dir = try std.fs.cwd().makeOpenPath(app_data_dir_path, .{});
defer app_data_dir.close();
var win_count_file = try app_data_dir.createFile("win_count.bin", .{ .read = true, .truncate = false });
defer win_count_file.close();
var buffer: [4]u8 = undefined;
const bytes_read = try win_count_file.preadAll(&buffer, 0);
if (bytes_read < buffer.len) {
std.mem.writeInt(u32, &buffer, 0, .little);
try win_count_file.pwriteAll(&buffer, 0);
return 0;
} else {
return std.mem.readInt(u32, &buffer, .little);
}
}
fn saveWinCount(allocator: std.mem.Allocator, count: u32) !void {
if (builtin.target.os.tag == .wasi) return;
const app_data_dir_path = try std.fs.getAppDataDir(allocator, "seizer-solitaire");
defer allocator.free(app_data_dir_path);
var app_data_dir = try std.fs.cwd().makeOpenPath(app_data_dir_path, .{});
defer app_data_dir.close();
var win_count_file = try app_data_dir.createFile("win_count.bin", .{ .read = true, .truncate = false });
defer win_count_file.close();
var buffer: [4]u8 = undefined;
std.mem.writeInt(u32, &buffer, count, .little);
try win_count_file.pwriteAll(&buffer, 0);
} }
fn resetGame() !void { fn resetGame() !void {
@ -180,9 +226,7 @@ fn render(window: seizer.Window) !void {
if (haveWon() and !win_triggered) { if (haveWon() and !win_triggered) {
win_count += 1; win_count += 1;
var win_count_buffer: [4]u8 = undefined; try saveWinCount(gpa, win_count);
std.mem.writeInt(u32, &win_count_buffer, win_count, .little);
try win_count_file.pwriteAll(&win_count_buffer, 0);
resetHistory(); resetHistory();
win_triggered = true; win_triggered = true;
@ -841,6 +885,7 @@ const DeckSprites = assets.DeckSprites;
const Card = assets.Card; const Card = assets.Card;
const assets = @import("./assets.zig"); const assets = @import("./assets.zig");
const builtin = @import("builtin");
const seizer = @import("seizer"); const seizer = @import("seizer");
const gl = seizer.gl; const gl = seizer.gl;
const ecs = seizer.flecs; const ecs = seizer.flecs;