From a9446f4ed68131401faa4b629f55aedb1cf2436e Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 18 Jul 2021 21:57:46 -0700 Subject: [PATCH] glfw: add Window.shouldClose Signed-off-by: Stephen Gutekanst --- glfw/src/Window.zig | 18 ++++++++++++++++++ glfw/src/main.zig | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/glfw/src/Window.zig b/glfw/src/Window.zig index a00925a1..2e652e54 100644 --- a/glfw/src/Window.zig +++ b/glfw/src/Window.zig @@ -232,6 +232,24 @@ pub fn destroy(self: Window) void { getError() catch {}; } +/// Checks the close flag of the specified window. +/// +/// This function returns the value of the close flag of the specified window. +/// +/// @thread_safety This function may be called from any thread. Access is not synchronized. +/// +/// see also: window_close +pub fn shouldClose(self: Window) bool { + const flag = c.glfwWindowShouldClose(self.handle); + + // The only error shouldClose could return would be glfw.Error.NotInitialized, which would + // definitely have occurred before calls to shouldClose. Returning an error here makes the API + // awkward to use, so we discard it instead. + getError() catch {}; + + return flag == c.GLFW_TRUE; +} + test "defaultHints" { const glfw = @import("main.zig"); try glfw.init(); diff --git a/glfw/src/main.zig b/glfw/src/main.zig index af5a1b79..785ed0e9 100644 --- a/glfw/src/main.zig +++ b/glfw/src/main.zig @@ -133,7 +133,7 @@ pub fn basicTest() !void { defer window.destroy(); var start = std.time.milliTimestamp(); - while (std.time.milliTimestamp() < start + 1000 and c.glfwWindowShouldClose(window.handle) != c.GLFW_TRUE) { + while (std.time.milliTimestamp() < start + 1000 and !window.shouldClose()) { c.glfwPollEvents(); } }