glfw: add Window.getUserPointer

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-17 23:10:51 -07:00 committed by Stephen Gutekanst
parent 726bc52c73
commit a93de211cf

View file

@ -1077,23 +1077,21 @@ pub inline fn setUserPointer(self: Window, Type: anytype, pointer: Type) void {
getError() catch {};
}
// TODO(window):
/// Returns the user pointer of the specified window.
///
/// This function returns the current value of the user-defined pointer of the specified window.
/// The initial value is null.
///
/// @thread_safety This function may be called from any thread. Access is not synchronized.
///
/// see also: window_userptr, glfw.Window.setUserPointer
pub inline fn getUserPointer(self: Window, Type: anytype) ?Type {
const ptr = c.glfwGetWindowUserPointer(self.handle);
if (ptr) |p| return @ptrCast(Type, @alignCast(@alignOf(Type), p));
return null;
}
// /// Returns the user pointer of the specified window.
// ///
// /// This function returns the current value of the user-defined pointer of the
// /// specified window. The initial value is null.
// ///
// /// @param[in] window The window whose pointer to return.
// ///
// /// Possible errors include glfw.Error.NotInitialized.
// ///
// /// @thread_safety This function may be called from any thread. Access is not
// /// synchronized.
// ///
// /// see also: window_userptr, glfwSetWindowUserPointer
// ///
// GLFWAPI void* glfwGetWindowUserPointer(GLFWwindow* window);
// TODO(window):
// /// Sets the position callback for the specified window.
// ///
@ -1880,3 +1878,24 @@ test "setUserPointer" {
window.setUserPointer(*T, &my_value);
}
test "getUserPointer" {
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();
const T = struct { name: []const u8 };
var my_value = T{ .name = "my window!" };
window.setUserPointer(*T, &my_value);
const got = window.getUserPointer(*T);
std.debug.assert(&my_value == got);
}