From b780db3e8aecd52bcfafa42c8a3c4bdcc13bf3ac Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sat, 14 Dec 2024 16:20:48 -0800 Subject: [PATCH] Emscripten uses its own glfw library (#8) --- README.md | 5 ++++- build.zig | 2 ++ src/zglfw.zig | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d48844e..d7ce27b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,10 @@ pub fn build(b: *std.Build) void { const zglfw = b.dependency("zglfw", .{}); exe.root_module.addImport("zglfw", zglfw.module("root")); - exe.linkLibrary(zglfw.artifact("glfw")); + + if (target.result.os.tag != .emscrpten) { + exe.linkLibrary(zglfw.artifact("glfw")); + } } ``` diff --git a/build.zig b/build.zig index 0d9d3d1..48feb34 100644 --- a/build.zig +++ b/build.zig @@ -36,6 +36,8 @@ pub fn build(b: *std.Build) void { }, }); + if (target.result.os.tag == .emscripten) return; + const glfw = if (options.shared) blk: { const lib = b.addSharedLibrary(.{ .name = "glfw", diff --git a/src/zglfw.zig b/src/zglfw.zig index 5bf589a..9f0a698 100644 --- a/src/zglfw.zig +++ b/src/zglfw.zig @@ -1107,3 +1107,26 @@ fn _isLinuxDesktopLike() bool { else => false, }; } + +//-------------------------------------------------------------------------------------------------- +// +// Emscripten +// +//-------------------------------------------------------------------------------------------------- +const os = builtin.target.os.tag; +usingnamespace if (os == .emscripten or os == .freestanding) struct { + // GLFW - emscripten uses older version that doesn't have these functions - implement dummies + var glfwGetGamepadStateWarnPrinted: bool = false; + pub export fn glfwGetGamepadState(_: i32, _: ?*anyopaque) i32 { + if (!glfwGetGamepadStateWarnPrinted) { + std.log.err("glfwGetGamepadState(): not implemented! Use emscripten specific functions: https://emscripten.org/docs/api_reference/html5.h.html?highlight=gamepadstate#c.emscripten_get_gamepad_status", .{}); + glfwGetGamepadStateWarnPrinted = true; + } + return 0; // false - failure + } + + /// use glfwSetCallback instead + pub export fn glfwGetError() i32 { + return 0; // no error + } +} else struct {};