glfw: add glfw.getProcAddress

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-16 15:27:57 -07:00 committed by Stephen Gutekanst
parent 9e4a784d70
commit 17fc430766

View file

@ -92,17 +92,6 @@ pub inline fn swapInterval(interval: isize) Error!void {
// TODO(opengl): // TODO(opengl):
// /// Client API function pointer type.
// ///
// /// Generic function pointer used for returning client API function pointers
// /// without forcing a cast from a regular pointer.
// ///
// /// see also: context_glext, glfwGetProcAddress
// ///
// ///
// /// @ingroup context
// typedef void (*GLFWglproc)(void);
// /// Returns whether the specified extension is available. // /// Returns whether the specified extension is available.
// /// // ///
// /// This function returns whether the specified // /// This function returns whether the specified
@ -136,43 +125,46 @@ pub inline fn swapInterval(interval: isize) Error!void {
// /// @ingroup context // /// @ingroup context
// GLFWAPI int glfwExtensionSupported(const char* extension); // GLFWAPI int glfwExtensionSupported(const char* extension);
// /// Returns the address of the specified function for the current /// Client API function pointer type.
// /// context. ///
// /// /// Generic function pointer used for returning client API function pointers.
// /// This function returns the address of the specified OpenGL or OpenGL ES ///
// /// [core or extension function](@ref context_glext), if it is supported /// see also: context_glext, glfwGetProcAddress
// /// by the current context. pub const GLProc = fn () callconv(.C) void;
// ///
// /// A context must be current on the calling thread. Calling this function /// Returns the address of the specified function for the current context.
// /// without a current context will cause a @ref GLFW_NO_CURRENT_CONTEXT error. ///
// /// /// This function returns the address of the specified OpenGL or OpenGL ES core or extension
// /// This function does not apply to Vulkan. If you are rendering with Vulkan, /// function (see context_glext), if it is supported by the current context.
// /// see @ref glfwGetInstanceProcAddress, `vkGetInstanceProcAddr` and ///
// /// `vkGetDeviceProcAddr` instead. /// A context must be current on the calling thread. Calling this function without a current
// /// // context will cause Error.NoCurrentContext.
// /// @param[in] procname The ASCII encoded name of the function. ///
// /// @return The address of the function, or null if an /// This function does not apply to Vulkan. If you are rendering with Vulkan, see glfw.getInstanceProcAddress,
// /// error occurred. // `vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` instead.
// /// ///
// /// Possible errors include glfw.Error.NotInitialized, glfw.Error.NoCurrentContext and glfw.Error.PlatformError. /// @param[in] procname The ASCII encoded name of the function.
// /// /// @return The address of the function, or null if an error occurred.
// /// The address of a given function is not guaranteed to be the same ///
// /// between contexts. /// Possible errors include glfw.Error.NotInitialized, glfw.Error.NoCurrentContext and glfw.Error.PlatformError.
// /// ///
// /// This function may return a non-null address despite the /// The address of a given function is not guaranteed to be the same between contexts.
// /// associated version or extension not being available. Always check the ///
// /// context version or extension string first. /// This function may return a non-null address despite the associated version or extension
// /// /// not being available. Always check the context version or extension string first.
// /// @pointer_lifetime The returned function pointer is valid until the context ///
// /// is destroyed or the library is terminated. /// @pointer_lifetime The returned function pointer is valid until the context is destroyed or the
// /// /// library is terminated.
// /// @thread_safety This function may be called from any thread. ///
// /// /// @thread_safety This function may be called from any thread.
// /// see also: context_glext, glfwExtensionSupported ///
// /// /// see also: context_glext, glfwExtensionSupported
// /// pub inline fn getProcAddress(proc_name: [*c]const u8) Error!?GLProc {
// /// @ingroup context const proc_address = c.glfwGetProcAddress(&proc_name[0]);
// GLFWAPI GLFWglproc glfwGetProcAddress(const char* procname); try getError();
if (proc_address) |addr| return addr;
return null;
}
test "makeContextCurrent" { test "makeContextCurrent" {
const glfw = @import("main.zig"); const glfw = @import("main.zig");
@ -218,3 +210,20 @@ test "swapInterval" {
try glfw.makeContextCurrent(window); try glfw.makeContextCurrent(window);
glfw.swapInterval(1) catch |err| std.debug.print("failed to set swap interval, error={}\n", .{err}); glfw.swapInterval(1) catch |err| std.debug.print("failed to set swap interval, error={}\n", .{err});
} }
test "getProcAddress" {
const glfw = @import("main.zig");
try glfw.init();
defer glfw.terminate();
const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null) catch |err| {
// return without fail, because most of our CI environments are headless / we cannot open
// windows on them.
std.debug.print("note: failed to create window: {}\n", .{err});
return;
};
defer window.destroy();
try glfw.makeContextCurrent(window);
_ = glfw.getProcAddress("foobar") catch |err| std.debug.print("failed to get proc address, error={}\n", .{err});
}