commit 90fe75cd89b391655ad09203d6bf3e9e5752a2ad
parent f3beb23e472b8e1e352a2d50c88ba31df3fd91ba
Author: Christian Ermann <christianermann@gmail.com>
Date: Wed, 7 May 2025 18:58:51 -0700
Keep GLFW window open until X is clicked
Diffstat:
2 files changed, 38 insertions(+), 25 deletions(-)
diff --git a/src/glfw.zig b/src/glfw.zig
@@ -23,7 +23,38 @@ const Error = error{
};
const Monitor = opaque {};
-const Window = opaque {};
+
+pub const Window = opaque {
+ pub fn create(
+ width: c_int,
+ height: c_int,
+ title: [*:0]const u8,
+ monitor: ?*Monitor,
+ share: ?*Window,
+ ) !*Window {
+ const window = c.glfwCreateWindow(
+ width,
+ height,
+ title,
+ @ptrCast(monitor),
+ @ptrCast(share),
+ );
+ try getError();
+ return @ptrCast(window.?);
+ }
+
+ pub fn destroy(window: *Window) void {
+ c.glfwDestroyWindow(@ptrCast(window));
+ }
+
+ pub fn shouldClose(window: *Window) bool {
+ return c.glfwWindowShouldClose(@ptrCast(window)) == c.GLFW_TRUE;
+ }
+};
+
+pub fn pollEvents() void {
+ c.glfwPollEvents();
+}
fn getError() !void {
const error_code = c.glfwGetError(null);
@@ -56,28 +87,6 @@ pub fn terminate() void {
c.glfwTerminate();
}
-pub fn createWindow(
- width: c_int,
- height: c_int,
- title: [*:0]const u8,
- monitor: ?*Monitor,
- share: ?*Window,
-) !*Window {
- const window = c.glfwCreateWindow(
- width,
- height,
- title,
- @ptrCast(monitor),
- @ptrCast(share),
- );
- try getError();
- return @ptrCast(window.?);
-}
-
-pub fn destroyWindow(window: *Window) void {
- c.glfwDestroyWindow(@ptrCast(window));
-}
-
const Hint = enum(u32) {
client_api = 0x00022001,
};
diff --git a/src/main.zig b/src/main.zig
@@ -43,8 +43,8 @@ pub fn main() !void {
defer glfw.terminate();
glfw.windowHint(.client_api, glfw.ClientApi.no_api);
- const window = try glfw.createWindow(640, 480, "test", null, null);
- defer glfw.destroyWindow(window);
+ const window = try glfw.Window.create(640, 480, "test", null, null);
+ defer window.destroy();
const display = try glfw.native.getX11Display();
const window_id = try glfw.native.getX11Window(window);
@@ -117,4 +117,8 @@ pub fn main() !void {
.height = 480,
.present_mode = .fifo,
});
+
+ while (!window.shouldClose()) {
+ glfw.pollEvents();
+ }
}