diff options
| author | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2021-09-09 00:36:04 +0700 |
|---|---|---|
| committer | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2021-09-09 00:36:04 +0700 |
| commit | 5fec1072d49a159b0b3893950dcb692f6ecd5d0e (patch) | |
| tree | 6d0da4bcb39b20ab8e42021a07739ae9b1b4af23 /src | |
| download | gfz-5fec1072d49a159b0b3893950dcb692f6ecd5d0e.tar.gz | |
Make example in GLFW docs work
Diffstat (limited to 'src')
| -rw-r--r-- | src/Monitor.zig | 4 | ||||
| -rw-r--r-- | src/Window.zig | 54 | ||||
| -rw-r--r-- | src/cimport.zig | 1 | ||||
| -rw-r--r-- | src/gfz.zig | 118 |
4 files changed, 177 insertions, 0 deletions
diff --git a/src/Monitor.zig b/src/Monitor.zig new file mode 100644 index 0000000..39e9815 --- /dev/null +++ b/src/Monitor.zig @@ -0,0 +1,4 @@ +usingnamespace @import("cimport.zig"); +const Monitor = @This(); + +pimpl: *GLFWmonitor, 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(); +} diff --git a/src/cimport.zig b/src/cimport.zig new file mode 100644 index 0000000..6089200 --- /dev/null +++ b/src/cimport.zig @@ -0,0 +1 @@ +pub usingnamespace @cImport(@cInclude("GLFW/glfw3.h")); diff --git a/src/gfz.zig b/src/gfz.zig new file mode 100644 index 0000000..f68461a --- /dev/null +++ b/src/gfz.zig @@ -0,0 +1,118 @@ +// Graphics Framework for Zig +// 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"); + +pub const Error = error { + /// GLFW has not been initialized. + NotInitialized, + /// No context is current for this thread. + NoCurrentContext, + /// One of the arguments to the function was an invalid enum value. + InvalidEnum, + /// One of the arguments to the function was an invalid value. + InvalidValue, + /// A memory allocation failed. + OutOfMemory, + /// GLFW could not find support for the requested API on the system. + ApiUnavailable, + /// The requested OpenGL or OpenGL ES version is not available. + VersionUnavailable, + /// A platform-specific error occurred + /// that does not match any of the more specific categories. + PlatformError, + /// The requested format is not supported or available. + FormatUnavailable, + /// The specified window does not have an OpenGL or OpenGL ES context. + NoWindowContext, +}; + +/// Return and clear the error code of the last error +/// that occurred on the calling thread if any. +pub fn checkError() Error!void { + return switch (glfwGetError(null)) { + GLFW_NO_ERROR => {}, + GLFW_NOT_INITIALIZED => Error.NotInitialized, + GLFW_NO_CURRENT_CONTEXT => Error.NoCurrentContext, + GLFW_INVALID_ENUM => Error.InvalidEnum, + GLFW_INVALID_VALUE => Error.InvalidValue, + GLFW_OUT_OF_MEMORY => Error.OutOfMemory, + GLFW_API_UNAVAILABLE => Error.ApiUnavailable, + GLFW_VERSION_UNAVAILABLE => Error.VersionUnavailable, + GLFW_PLATFORM_ERROR => Error.PlatformError, + GLFW_FORMAT_UNAVAILABLE => Error.FormatUnavailable, + GLFW_NO_WINDOW_CONTEXT => Error.NoWindowContext, + else => unreachable, + }; +} + +/// Return and clear the last error for the calling thread. +pub fn getError() Error { + return if (checkError()) |_| unreachable else |err| err; +} + +/// Initialize the GLFW library. +/// +/// Before most GLFW functions can be used, GLFW must be initialized, +/// and before an application terminates GLFW should be terminated +/// in order to free any resources allocated during or after initialization. +/// +/// If this function fails, it calls `glfwTerminate` before returning. +/// If it succeeds, you should call `deinit` before the application exits. +/// +/// Additional calls to this function after successful initialization +/// but before termination will return immediately. +pub fn init() Error!void { + if (glfwInit() != GLFW_TRUE) + return getError(); +} + +/// Destroy all remaining windows and cursors, restore any modified gamma ramps +/// and frees any other allocated resources. +/// +/// Once this function is called, you must again call `init` successfully +/// before you will be able to use most GLFW functions. +/// +/// If GLFW has been successfully initialized, this function should be called +/// before the application exits. If initialization fails, there is no need +/// to call this function, as it is called by `init` before it returns failure. +/// +/// This function has no effect if GLFW is not initialized. +pub fn deinit() Error!void { + glfwTerminate(); + try checkError(); +} + +pub fn pollEvents() Error!void { + glfwPollEvents(); + try checkError(); +} + +pub const Window = @import("Window.zig"); + +test { + try init(); + defer deinit() catch unreachable; + const window = try Window.create(800, 600, "Hello, World!", null, null); + try window.makeCurrent(); + + while (!try window.shouldClose()) { + try window.swapBuffers(); + try pollEvents(); + } +} |
