From d013ef1868347535b1d4e2360acf3bbac71bc134 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Wed, 30 Apr 2014 08:37:34 +0200 Subject: [PATCH] [Platform] Make MouseCursor actor public --- Source/OpenTK/MouseCursor.cs | 30 ++++++++++------------- Source/OpenTK/WindowIcon.cs | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/Source/OpenTK/MouseCursor.cs b/Source/OpenTK/MouseCursor.cs index 47f8fcc6..2668efbe 100644 --- a/Source/OpenTK/MouseCursor.cs +++ b/Source/OpenTK/MouseCursor.cs @@ -40,9 +40,6 @@ namespace OpenTK static readonly MouseCursor empty_cursor = new MouseCursor( new byte[16 * 16 * 4], 16, 16, 0, 0); - byte[] rgba; - int width; - int height; int x; int y; @@ -51,27 +48,26 @@ namespace OpenTK } // Todo: make public when byte-order issues are resolved - internal MouseCursor(byte[] rgba, int width, int height, int x, int y) + public MouseCursor(byte[] argb, int width, int height, int x, int y) + : base(argb, width, height) { - if (rgba == null) - throw new ArgumentNullException(); - if (width < 0 || width > 256 || height < 0 || height > 256) - throw new ArgumentOutOfRangeException(); - if (rgba.Length < width * height * 4) - throw new ArgumentOutOfRangeException(); - if (x < 0 || x >= width || y < 0 || y >= height) + if (x < 0 || x >= Width || y < 0 || y >= Height) + throw new ArgumentOutOfRangeException(); + + this.x = x; + this.y = y; + } + + public MouseCursor(IntPtr argb, int width, int height, int x, int y) + : base(argb, width, height) + { + if (x < 0 || x >= Width || y < 0 || y >= Height) throw new ArgumentOutOfRangeException(); - this.rgba = rgba; - this.width = width; - this.height = height; this.x = x; this.y = y; } - internal byte[] Rgba { get { return rgba; } } - internal int Width { get { return width; } } - internal int Height { get { return height; } } internal int X { get { return x; } } internal int Y { get { return y; } } diff --git a/Source/OpenTK/WindowIcon.cs b/Source/OpenTK/WindowIcon.cs index 94cfde4f..87262d7f 100644 --- a/Source/OpenTK/WindowIcon.cs +++ b/Source/OpenTK/WindowIcon.cs @@ -28,6 +28,7 @@ #endregion using System; +using System.Runtime.InteropServices; namespace OpenTK { @@ -37,9 +38,55 @@ namespace OpenTK /// public class WindowIcon { + byte[] argb; + int width; + int height; + internal protected WindowIcon() { } + + WindowIcon(int width, int height) + { + if (width < 0 || width > 256 || height < 0 || height > 256) + throw new ArgumentOutOfRangeException(); + + this.width = width; + this.height = height; + } + + internal WindowIcon(byte[] argb, int width, int height) + : this(width, height) + { + if (argb == null) + throw new ArgumentNullException(); + if (argb.Length < Width * Height * 4) + throw new ArgumentOutOfRangeException(); + + this.argb = argb; + } + + internal WindowIcon(IntPtr argb, int width, int height) + : this(width, height) + { + if (argb == IntPtr.Zero) + throw new ArgumentNullException(); + + // We assume that width and height are correctly set. + // If they are not, we will read garbage and probably + // crash. + this.argb = new byte[width * height * 4]; + for (int y = 0; y < height; y++) + { + var stride = width * 4; + var offset = new IntPtr(argb.ToInt64() + y * stride); + Marshal.Copy(offset, Argb, y * stride, stride); + } + } + + internal byte[] Argb { get { return argb; } } + internal int Width { get { return width; } } + internal int Height { get { return height; } } } }