using System.Runtime.InteropServices.Marshalling; // Hand-written members of the Cursor library: the GlfwCursor handle type, its custom marshaller, // and the custom-image CreateCursor factory (which bundles a CreateInfo struct and fixed-pins the // image - not mechanically derivable). The standard-shape CreateCursor overload is generated into // Generated/Libraries/Glfw.Cursor.gen.cs and merges into this same partial class. namespace Brutal.GlfwApi; public static unsafe partial class Glfw { /// Creates a custom cursor. /// /// Creates a new custom cursor image that can be set for a window with GlfwWindow.SetCursor /// . The cursor can be destroyed with GlfwCursor.Dispose. Any remaining cursors are destroyed by /// . /// /// 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. /// /// The handle of the new cursor, or null if an error occurred. /// This function must only be called from the main thread. /// GlfwCursor.Dispose /// /// Cursor objects /// /// /// /// /// public static GlfwCursor CreateCursor(in GlfwCursor.CreateInfo info) { fixed (GlfwImage* imagePtr = &info.Image) { return PInvoke.CreateCursor(imagePtr, info.Hot.X, info.Hot.Y); } } } /// /// Wrapper around a * /// [NativeMarshalling(typeof(GlfwCursorMarshaller))] public unsafe struct GlfwCursor : IDisposable, INullable { /// /// Information required to create a custom cursor. /// public struct CreateInfo { /// /// The image to use for the cursor. /// public GlfwImage Image; /// /// The position of the cursor hotspot, relative to the upper-left corner of the image. /// public int2 Hot; } internal GlfwCursor(GlfwCursorHandle* inPointer) { Pointer = inPointer; } internal GlfwCursorHandle* Pointer { get; private set; } /// /// Gets the native Glfw handle for this cursor /// public nint NativeHandle => (nint)Pointer; /// Destroys a cursor. /// /// This function destroys a cursor previously created with or /// . Any remaining cursors are destroyed by . /// /// If the specified cursor is current for any window, that window will be reverted to the default cursor. This does /// not affect the cursor mode. /// /// This function must not be called from a callback. /// This function must only be called from the main thread. /// /// Cursor objects /// /// /// /// public void Dispose() { if (Pointer != null) { Glfw.PInvoke.DestroyCursor(this); Pointer = null; } } /// Returns whether this cursor handle is null (uninitialised or already destroyed). /// true if the underlying native handle is null; otherwise false. public bool IsNull() { return Pointer == null; } } [CustomMarshaller( managedType: typeof(GlfwCursor), marshalMode: MarshalMode.Default, marshallerType: typeof(GlfwCursorMarshaller) )] internal static unsafe class GlfwCursorMarshaller { internal static GlfwCursorHandle* ConvertToUnmanaged(GlfwCursor managed) => managed.Pointer; internal static GlfwCursor ConvertToManaged(GlfwCursorHandle* unmanaged) => new(unmanaged); }