From f533747fa83454467674c9b24fd67c7e0ca01fff Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 16 Jul 2021 15:07:06 -0700 Subject: [PATCH] glfw: add init function Signed-off-by: Stephen Gutekanst --- glfw/src/main.zig | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/glfw/src/main.zig b/glfw/src/main.zig index 68f0700a..b9f2d974 100644 --- a/glfw/src/main.zig +++ b/glfw/src/main.zig @@ -4,7 +4,8 @@ const testing = std.testing; const c = @cImport(@cInclude("GLFW/glfw3.h")); pub usingnamespace @import("consts.zig"); -pub usingnamespace @import("errors.zig"); +pub const Error = @import("errors.zig").Error; +const getError = @import("errors.zig").getError; pub const action = @import("action.zig"); pub const gamepad_axis = @import("gamepad_axis.zig"); @@ -16,10 +17,36 @@ pub const mod = @import("mod.zig"); pub const mouse_button = @import("mouse_button.zig"); pub const version = @import("version.zig"); -pub fn basicTest() void { +/// Initializes the GLFW library. +/// +/// This function initializes 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 glfw.Terminate before returning. If it succeeds, you should +/// call glfw.Terminate before the application exits. +/// +/// Additional calls to this function after successful initialization but before termination will +/// return immediately with no error. +/// +/// macos: This function will change the current directory of the application to the +/// `Contents/Resources` subdirectory of the application's bundle, if present. This can be disabled +/// with the glfw.COCOA_CHDIR_RESOURCES init hint. +/// +/// x11: This function will set the `LC_CTYPE` category of the application locale according to the +/// current environment if that category is still "C". This is because the "C" locale breaks +/// Unicode text input. +/// +/// @thread_safety This function must only be called from the main thread. +pub fn init() Error!void { if (c.glfwInit() != c.GLFW_TRUE) { - @panic("failed to init"); + return try getError(); } + return; +} + +pub fn basicTest() void { + init() catch @panic("failed to init!"); c.glfwWindowHint(c.GLFW_VISIBLE, c.GLFW_FALSE); const window = c.glfwCreateWindow(640, 480, "GLFW example", null, null); if (window == null) {