From 5141f17f2340d024e5668986a283840f16cc4241 Mon Sep 17 00:00:00 2001 From: Robert Lodico Date: Sat, 21 May 2016 17:22:01 -0400 Subject: [PATCH] Fixed glfwSetGammaRamp() and glfwCreateCursor() --- glfw-net-test/Program.cs | 6 ++ glfw-net/Binding/GLFW3.Imports.cs | 19 ++++-- glfw-net/Binding/GLFWgammaramp.cs | 4 +- glfw-net/Binding/GLFWimage.cs | 7 +++ glfw-net/Binding/InternalGLFW3.Imports.cs | 70 +++++++++++++++++++++++ glfw-net/Binding/InternalUtils.cs | 24 ++++++++ glfw-net/glfw-net.csproj | 1 + 7 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 glfw-net/Binding/InternalUtils.cs diff --git a/glfw-net-test/Program.cs b/glfw-net-test/Program.cs index 284e500..71c19ea 100644 --- a/glfw-net-test/Program.cs +++ b/glfw-net-test/Program.cs @@ -34,6 +34,12 @@ static void Main(string[] args) { GLFW3.glfwSetWindowSizeCallback(window, callbackWindowSize); var ramp = GLFW3.glfwGetGammaRamp(GLFW3.glfwGetPrimaryMonitor()); + for (int i = 0; i < ramp.size; i++) + { + ramp.red[i] /= 2; + ramp.blue[i] *= 2; + ramp.green[i] /= 2; + } GLFW3.glfwSetGammaRamp(GLFW3.glfwGetPrimaryMonitor(), ref ramp); while (!GLFW3.glfwWindowShouldClose(window)) { diff --git a/glfw-net/Binding/GLFW3.Imports.cs b/glfw-net/Binding/GLFW3.Imports.cs index 4d74eff..c38862c 100644 --- a/glfw-net/Binding/GLFW3.Imports.cs +++ b/glfw-net/Binding/GLFW3.Imports.cs @@ -478,7 +478,6 @@ public static GLFWgammaramp glfwGetGammaRamp(GLFWmonitor monitor) { var ramp = new GLFWgammaramp { - size = internalRamp->size, red = new ushort[internalRamp->size], green = new ushort[internalRamp->size], blue = new ushort[internalRamp->size] @@ -519,8 +518,14 @@ public static GLFWgammaramp glfwGetGammaRamp(GLFWmonitor monitor) { * * @ingroup monitor */ - [DllImport(NATIVE), SuppressUnmanagedCodeSecurity] - public static extern void glfwSetGammaRamp(GLFWmonitor monitor, ref GLFWgammaramp ramp); + public static void glfwSetGammaRamp(GLFWmonitor monitor, ref GLFWgammaramp ramp) { + fixed (ushort* rampRed = ramp.red, rampBlue = ramp.blue, rampGreen = ramp.green) { + var internalRamp = new InternalGLFWgammaramp { + red = rampRed, blue = rampBlue, green = rampGreen, size = ramp.size + }; + InternalGLFW3.glfwSetGammaRamp(monitor, internalRamp); + } + } /*! @brief Resets all window hints to their default values. * @@ -1678,8 +1683,12 @@ public static GLFWgammaramp glfwGetGammaRamp(GLFWmonitor monitor) { * * @ingroup input */ - [DllImport(NATIVE, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] - public static extern GLFWcursor glfwCreateCursor(GLFWimage image, int xhot, int yhot); + public static GLFWcursor glfwCreateCursor(GLFWimage image, int xhot, int yhot) { + fixed (byte* imagePixels = image.pixels) { + var internalImage = new InternalGLFWimage { width = image.width, height = image.height, pixels = imagePixels }; + return InternalGLFW3.glfwCreateCursor(internalImage, xhot, yhot); + } + } /*! @brief Creates a cursor with a standard shape. * diff --git a/glfw-net/Binding/GLFWgammaramp.cs b/glfw-net/Binding/GLFWgammaramp.cs index 05b8c52..6784497 100644 --- a/glfw-net/Binding/GLFWgammaramp.cs +++ b/glfw-net/Binding/GLFWgammaramp.cs @@ -27,7 +27,7 @@ public struct GLFWgammaramp { /// /// The number of elements in each array. /// - public uint size; + public uint size { get { return (uint)InternalUtils.min(red.Length, green.Length, blue.Length); } } } [StructLayout(LayoutKind.Sequential)] @@ -35,6 +35,6 @@ internal unsafe struct InternalGLFWgammaramp { public ushort* red; public ushort* green; public ushort* blue; - public uint size; + internal uint size; } } diff --git a/glfw-net/Binding/GLFWimage.cs b/glfw-net/Binding/GLFWimage.cs index 3716881..cb5642b 100644 --- a/glfw-net/Binding/GLFWimage.cs +++ b/glfw-net/Binding/GLFWimage.cs @@ -22,4 +22,11 @@ public struct GLFWimage { [MarshalAs(UnmanagedType.SafeArray)] public byte[] pixels; } + + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct InternalGLFWimage { + public int width; + public int height; + public byte* pixels; + } } diff --git a/glfw-net/Binding/InternalGLFW3.Imports.cs b/glfw-net/Binding/InternalGLFW3.Imports.cs index c732f68..2ef3f77 100644 --- a/glfw-net/Binding/InternalGLFW3.Imports.cs +++ b/glfw-net/Binding/InternalGLFW3.Imports.cs @@ -173,6 +173,76 @@ public unsafe partial class InternalGLFW3 { */ [DllImport(GLFW3.NATIVE), SuppressUnmanagedCodeSecurity] internal static extern InternalGLFWgammaramp* glfwGetGammaRamp(GLFWmonitor monitor); + + /*! @brief Sets the current gamma ramp for the specified monitor. + * + * This function sets the current gamma ramp for the specified monitor. The + * original gamma ramp for that monitor is saved by GLFW the first time this + * function is called and is restored by @ref glfwTerminate. + * + * @param[in] monitor The monitor whose gamma ramp to set. + * @param[in] ramp The gamma ramp to use. + * + * @remarks Gamma ramp sizes other than 256 are not supported by all platforms + * or graphics hardware. + * + * @remarks __Windows:__ The gamma ramp size must be 256. + * + * @par Pointer Lifetime + * The specified gamma ramp is copied before this function returns. + * + * @par Thread Safety + * This function may only be called from the main thread. + * + * @sa @ref monitor_gamma + * + * @since Added in GLFW 3.0. + * + * @ingroup monitor + */ + [DllImport(GLFW3.NATIVE), SuppressUnmanagedCodeSecurity] + internal static extern void glfwSetGammaRamp(GLFWmonitor monitor, InternalGLFWgammaramp ramp); + + /*! @brief 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 RGBA, i.e. eight bits per channel. 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](@ref error_handling) occurred. + * + * @par Pointer Lifetime + * The specified image data is copied before this function returns. + * + * @par Reentrancy + * This function may not be called from a callback. + * + * @par Thread Safety + * This function may only be called from the main thread. + * + * @sa @ref cursor_object + * @sa glfwDestroyCursor + * @sa glfwCreateStandardCursor + * + * @since Added in GLFW 3.1. + * + * @ingroup input + */ + [DllImport(GLFW3.NATIVE, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + internal static extern GLFWcursor glfwCreateCursor(InternalGLFWimage image, int xhot, int yhot); #endregion GLFW API functions } } diff --git a/glfw-net/Binding/InternalUtils.cs b/glfw-net/Binding/InternalUtils.cs new file mode 100644 index 0000000..6980248 --- /dev/null +++ b/glfw-net/Binding/InternalUtils.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace GLFWnet.Binding { + internal static class InternalUtils { + internal static int max(int val, params int[] compare) { + int result = val; + foreach (int i in compare) { + result = (result < i ? result : i); + } + return result; + } + + internal static int min(int val, params int[] compare) { + int result = val; + foreach (int i in compare) { + result = (result > i ? result : i); + } + return result; + } + } +} diff --git a/glfw-net/glfw-net.csproj b/glfw-net/glfw-net.csproj index 03af2c9..4bcc756 100644 --- a/glfw-net/glfw-net.csproj +++ b/glfw-net/glfw-net.csproj @@ -71,6 +71,7 @@ +