about summary refs log tree commit diff
path: root/src/Window.zig
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-09-09 00:36:04 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-09-09 00:36:04 +0700
commit5fec1072d49a159b0b3893950dcb692f6ecd5d0e (patch)
tree6d0da4bcb39b20ab8e42021a07739ae9b1b4af23 /src/Window.zig
downloadgfz-5fec1072d49a159b0b3893950dcb692f6ecd5d0e.tar.gz
Make example in GLFW docs work
Diffstat (limited to 'src/Window.zig')
-rw-r--r--src/Window.zig54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/Window.zig b/src/Window.zig
new file mode 100644
index 0000000..0c8bacb
--- /dev/null
+++ b/src/Window.zig
@@ -0,0 +1,54 @@
+// Window manipulation
+// Copyright (C) 2021  Nguyễn Gia Phong
+//
+// This file is part of gfz.
+//
+// gfz is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// gfz is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with gfz.  If not, see <https://www.gnu.org/licenses/>.
+
+usingnamespace @import("cimport.zig");
+const gfz = @import("gfz.zig");
+const Monitor = @import("Monitor.zig");
+const Window = @This();
+
+pimpl: *GLFWwindow,
+
+/// Create a window and its associated context.
+pub fn create(width: c_int, height: c_int, title: []const u8,
+              monitor: ?Monitor, share: ?Window) gfz.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);
+    return if (ptr) |pimpl|
+        Window{ .pimpl = pimpl }
+    else
+        gfz.getError();
+}
+
+/// Make the context current for the calling thread.
+pub fn makeCurrent(self: Window) gfz.Error!void {
+    glfwMakeContextCurrent(self.pimpl);
+    try gfz.checkError();
+}
+
+/// Check the close flag.
+pub fn shouldClose(self: Window) gfz.Error!bool {
+    const flag = glfwWindowShouldClose(self.pimpl);
+    try gfz.checkError();
+    return flag == GLFW_TRUE;
+}
+
+pub fn swapBuffers(self: Window) gfz.Error!void {
+    glfwSwapBuffers(self.pimpl);
+    try gfz.checkError();
+}