Skip to content

Commit

Permalink
Merge pull request #5 from lodico/fix
Browse files Browse the repository at this point in the history
Fixed glfwSetGammaRamp() and glfwCreateCursor()
  • Loading branch information
lodicolo committed May 21, 2016
2 parents d4c55ab + 5141f17 commit 8104007
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 7 deletions.
6 changes: 6 additions & 0 deletions glfw-net-test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
19 changes: 14 additions & 5 deletions glfw-net/Binding/GLFW3.Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions glfw-net/Binding/GLFWgammaramp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public struct GLFWgammaramp {
/// <summary>
/// The number of elements in each array.
/// </summary>
public uint size;
public uint size { get { return (uint)InternalUtils.min(red.Length, green.Length, blue.Length); } }
}

[StructLayout(LayoutKind.Sequential)]
internal unsafe struct InternalGLFWgammaramp {
public ushort* red;
public ushort* green;
public ushort* blue;
public uint size;
internal uint size;
}
}
7 changes: 7 additions & 0 deletions glfw-net/Binding/GLFWimage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
70 changes: 70 additions & 0 deletions glfw-net/Binding/InternalGLFW3.Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
24 changes: 24 additions & 0 deletions glfw-net/Binding/InternalUtils.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 1 addition & 0 deletions glfw-net/glfw-net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="Binding\GLFWcursor.cs" />
<Compile Include="Binding\GLFWmonitor.cs" />
<Compile Include="Binding\GLFWwindow.cs" />
<Compile Include="Binding\InternalUtils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down

0 comments on commit 8104007

Please sign in to comment.