glfw: add Window.getUserPointer
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
9f7e2e4a55
commit
c7161afec3
1 changed files with 35 additions and 16 deletions
|
@ -1077,23 +1077,21 @@ pub inline fn setUserPointer(self: Window, Type: anytype, pointer: Type) void {
|
||||||
getError() catch {};
|
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.
|
// TODO(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);
|
|
||||||
|
|
||||||
// /// Sets the position callback for the specified window.
|
// /// Sets the position callback for the specified window.
|
||||||
// ///
|
// ///
|
||||||
|
@ -1880,3 +1878,24 @@ test "setUserPointer" {
|
||||||
|
|
||||||
window.setUserPointer(*T, &my_value);
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue