From 509e7f3b4ba557c2c9c7c7a7a74651a5e767851f Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 22 Oct 2021 00:00:46 -0700 Subject: [PATCH] glfw: add Cursor.create Signed-off-by: Stephen Gutekanst --- glfw/src/Cursor.zig | 100 +++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/glfw/src/Cursor.zig b/glfw/src/Cursor.zig index f890753f..861b56e1 100644 --- a/glfw/src/Cursor.zig +++ b/glfw/src/Cursor.zig @@ -1,47 +1,48 @@ -// TODO(cursor): -// /// Opaque cursor object. -// /// -// /// Opaque cursor object. -// /// -// /// see also: cursor_object -// /// -// /// -// /// @ingroup input -// typedef struct GLFWcursor GLFWcursor; +//! Represents a cursor and provides facilities for setting cursor images. -// TODO(cursor icon) -// /// Creates a custom cursor. -// /// -// /// Creates a new custom cursor image that can be set for a window with @ref -// /// glfwSetCursor. The cursor can be destroyed with @ref glfwDestroyCursor. -// /// Any remaining cursors are destroyed by @ref glfwTerminate. -// /// -// /// The pixels are 32-bit, little-endian, non-premultiplied RGBA, i.e. eight -// /// bits per channel with the red channel first. They are arranged canonically -// /// as packed sequential rows, starting from the top-left corner. -// /// -// /// The cursor hotspot is specified in pixels, relative to the upper-left corner -// /// of the cursor image. Like all other coordinate systems in GLFW, the X-axis -// /// points to the right and the Y-axis points down. -// /// -// /// @param[in] image The desired cursor image. -// /// @param[in] xhot The desired x-coordinate, in pixels, of the cursor hotspot. -// /// @param[in] yhot The desired y-coordinate, in pixels, of the cursor hotspot. -// /// @return The handle of the created cursor, or null if an -// /// error occurred. -// /// -// /// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError. -// /// -// /// @pointer_lifetime The specified image data is copied before this function -// /// returns. -// /// -// /// @thread_safety This function must only be called from the main thread. -// /// -// /// see also: cursor_object, glfwDestroyCursor, glfwCreateStandardCursor -// /// -// /// -// /// @ingroup input -// GLFWAPI GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot); +const std = @import("std"); +const testing = std.testing; + +const c = @import("c.zig").c; +const Error = @import("errors.zig").Error; +const getError = @import("errors.zig").getError; +const Image = @import("Image.zig"); + +const Cursor = @This(); + +ptr: *c.GLFWcursor, + +/// Creates a custom cursor. +/// +/// Creates a new custom cursor image that can be set for a window with glfw.Cursor.set. The cursor +/// can be destroyed with glfwCursor.destroy. Any remaining cursors are destroyed by glfw.terminate. +/// +/// The pixels are 32-bit, little-endian, non-premultiplied RGBA, i.e. eight bits per channel with +/// the red channel first. They are arranged canonically as packed sequential rows, starting from +/// the top-left corner. +/// +/// The cursor hotspot is specified in pixels, relative to the upper-left corner of the cursor +/// image. Like all other coordinate systems in GLFW, the X-axis points to the right and the Y-axis +/// points down. +/// +/// @param[in] image The desired cursor image. +/// @param[in] xhot The desired x-coordinate, in pixels, of the cursor hotspot. +/// @param[in] yhot The desired y-coordinate, in pixels, of the cursor hotspot. +/// @return The handle of the created cursor. +/// +/// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError. +/// +/// @pointer_lifetime The specified image data is copied before this function returns. +/// +/// @thread_safety This function must only be called from the main thread. +/// +/// see also: cursor_object, glfw.Cursor.destroy, glfw.Cursor.createStandard +pub inline fn create(image: Image, xhot: isize, yhot: isize) Error!Cursor { + const img = image.toC(); + const cursor = c.glfwCreateCursor(&img, @intCast(c_int, xhot), @intCast(c_int, yhot)); + try getError(); + return Cursor{ .ptr = cursor.? }; +} // TODO(cursor icon) // /// Creates a cursor with a standard shape. @@ -86,3 +87,16 @@ // /// // /// @ingroup input // GLFWAPI void glfwDestroyCursor(GLFWcursor* cursor); + +test "create" { + const allocator = testing.allocator; + + const glfw = @import("main.zig"); + try glfw.init(); + defer glfw.terminate(); + + const image = try Image.init(allocator, 32, 32, 32 * 32 * 4); + defer image.deinit(allocator); + + _ = glfw.Cursor.create(image, 0, 0) catch |err| std.debug.print("failed to create cursor, custom cursors not supported? error={}\n", .{err}); +}