Skip to content

Commit b114535

Browse files
committed
Fix crash by SDL3 code in UWP
1 parent 0d1537e commit b114535

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/GameWindowEXT.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
#region Using Statements
1111
using System;
12+
#if WINDOWS10_0_17763_0_OR_GREATER
13+
using SDL2;
14+
#else
1215
using SDL3;
16+
#endif
1317
using Microsoft.Xna.Framework.Input;
1418
#endregion
1519

src/Input/MouseCursorEXT.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
#region Using Statements
1111
using System;
12+
#if WINDOWS10_0_17763_0_OR_GREATER
13+
using SDL2;
14+
#else
1215
using SDL3;
16+
#endif
1317
using Microsoft.Xna.Framework.Graphics;
1418
using System.Runtime.InteropServices;
1519
#endregion
@@ -36,6 +40,38 @@ public static void SetCursor(MouseCursor cursor)
3640
/// <param name="texture">Texture to use as the cursor image.</param>
3741
/// <param name="originx">X cordinate of the image that will be used for mouse position.</param>
3842
/// <param name="originy">Y cordinate of the image that will be used for mouse position.</param>
43+
#if WINDOWS10_0_17763_0_OR_GREATER
44+
public static MouseCursor CreateFromTexture2D(Texture2D texture, int originx, int originy)
45+
{
46+
if (texture.Format != SurfaceFormat.Color)
47+
throw new ArgumentException("Only Color textures are accepted for mouse cursors", "texture");
48+
49+
IntPtr surface = IntPtr.Zero;
50+
IntPtr handle = IntPtr.Zero;
51+
try
52+
{
53+
var bytes = new byte[texture.Width * texture.Height * 4];
54+
texture.GetData(bytes);
55+
56+
GCHandle gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
57+
surface = SDL.SDL_CreateRGBSurfaceFrom(gcHandle.AddrOfPinnedObject(), texture.Width, texture.Height, 32, texture.Width * 4, 0x000000ff, 0x0000FF00, 0x00FF0000, 0xFF000000);
58+
gcHandle.Free();
59+
if (surface == IntPtr.Zero)
60+
throw new InvalidOperationException("Failed to create surface for mouse cursor: " + SDL.SDL_GetError());
61+
62+
handle = SDL.SDL_CreateColorCursor(surface, originx, originy);
63+
if (handle == IntPtr.Zero)
64+
throw new InvalidOperationException("Failed to set surface for mouse cursor: " + SDL.SDL_GetError());
65+
}
66+
finally
67+
{
68+
if (surface != IntPtr.Zero)
69+
SDL.SDL_FreeSurface(surface);
70+
}
71+
72+
return new MouseCursor(handle);
73+
}
74+
#else
3975
public static MouseCursor CreateFromTexture2D(Texture2D texture, int originx, int originy)
4076
{
4177
if (texture.Format != SurfaceFormat.Color)
@@ -66,6 +102,7 @@ public static MouseCursor CreateFromTexture2D(Texture2D texture, int originx, in
66102

67103
return new MouseCursor(handle);
68104
}
105+
#endif
69106
}
70107

71108
public class MouseCursor : IDisposable
@@ -136,6 +173,21 @@ public class MouseCursor : IDisposable
136173

137174
static MouseCursor()
138175
{
176+
#if WINDOWS10_0_17763_0_OR_GREATER
177+
Arrow = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_ARROW);
178+
IBeam = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_IBEAM);
179+
Wait = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_WAIT);
180+
Crosshair = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_CROSSHAIR);
181+
WaitArrow = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_WAITARROW);
182+
SizeNWSE = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENWSE);
183+
SizeNESW = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENESW);
184+
SizeWE = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEWE);
185+
SizeNS = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENS);
186+
SizeAll = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEALL);
187+
No = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_NO);
188+
Hand = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_HAND);
189+
#else
190+
139191
Arrow = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_DEFAULT);
140192
IBeam = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_TEXT);
141193
Wait = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_WAIT);
@@ -148,6 +200,7 @@ static MouseCursor()
148200
SizeAll = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_MOVE);
149201
No = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_NOT_ALLOWED);
150202
Hand = new MouseCursor(SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_POINTER);
203+
#endif
151204
}
152205

153206
internal MouseCursor(IntPtr handle)
@@ -163,7 +216,11 @@ public void Dispose()
163216
if (Handle == IntPtr.Zero)
164217
return;
165218

219+
#if WINDOWS10_0_17763_0_OR_GREATER
220+
SDL.SDL_FreeCursor(Handle);
221+
#else
166222
SDL.SDL_DestroyCursor(Handle);
223+
#endif
167224
Handle = IntPtr.Zero;
168225

169226
_disposed = true;

0 commit comments

Comments
 (0)