about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-09-09 17:53:24 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-09-09 17:53:24 +0700
commitda9e10dc992d3a389d549e344b17eae4c32d98b0 (patch)
tree6b1a54d42ad220ad49e1cb135c6a4612b508e6fa /src
parent517356e2d6ebce33004d91cb55173f8cc419fc66 (diff)
downloadgfz-da9e10dc992d3a389d549e344b17eae4c32d98b0.tar.gz
Support window hinting
Diffstat (limited to 'src')
-rw-r--r--src/Window.zig31
-rw-r--r--src/gfz.zig2
2 files changed, 28 insertions, 5 deletions
diff --git a/src/Window.zig b/src/Window.zig
index bc3b8bd..748e042 100644
--- a/src/Window.zig
+++ b/src/Window.zig
@@ -26,12 +26,35 @@ const gfz = @import("gfz.zig");
 const Window = @This();
 pimpl: *GLFWwindow,
 
+const Options = struct {
+    fullscreen: ?Monitor = null,
+    share: ?Window = null,
+};
+
+/// Set the specified window hint to the desired value.
+fn setHint(hint: c_int, value: anytype) Error!void {
+    switch (@TypeOf(value)) {
+        c_int, comptime_int => glfwWindowHint(hint, value),
+        bool => glfwWindowHint(hint, if (value) GLFW_TRUE else GLFW_FALSE),
+        else => unreachable,
+    }
+    try checkError();
+}
+
 /// Create a window and its associated context.
 pub fn create(width: c_int, height: c_int, title: []const u8,
-              monitor: ?Monitor, share: ?Window) Error!Window {
-    const pmonitor = if (monitor) |box| box.pimpl else null;
-    const pshare = if (share) |box| box.pimpl else null;
-    const ptr = glfwCreateWindow(width, height, title.ptr, pmonitor, pshare);
+              options: Options, hints: anytype) Error!Window {
+    const Hints = @TypeOf(hints);
+    // TODO: Add all supported GLFW window hints:
+    // https://www.glfw.org/docs/latest/window_guide.html#window_hints_values
+    if (@hasField(Hints, "depth_bits"))
+        try setHint(GLFW_DEPTH_BITS, hints.depth_bits);
+    if (@hasField(Hints, "double_buffer"))
+        try setHint(GLFW_DOUBLEBUFFER, hints.double_buffer);
+
+    const monitor = if (options.fullscreen) |box| box.pimpl else null;
+    const share = if (options.share) |box| box.pimpl else null;
+    const ptr = glfwCreateWindow(width, height, title.ptr, monitor, share);
     return if (ptr) |pimpl|
         Window{ .pimpl = pimpl }
     else
diff --git a/src/gfz.zig b/src/gfz.zig
index f68461a..53e4aaa 100644
--- a/src/gfz.zig
+++ b/src/gfz.zig
@@ -108,7 +108,7 @@ pub const Window = @import("Window.zig");
 test {
     try init();
     defer deinit() catch unreachable;
-    const window = try Window.create(800, 600, "Hello, World!", null, null);
+    const window = try Window.create(800, 600, "Hello, World!", .{}, .{});
     try window.makeCurrent();
 
     while (!try window.shouldClose()) {