Implemented new, leaner SDL2 binding.

The new binding is based on the SDL2.0.1 RC1 headers.
This commit is contained in:
Stefanos A 2013-10-24 01:45:13 +02:00
parent 907a1de89f
commit 80f0569f9a
11 changed files with 1474 additions and 320 deletions

View file

@ -174,17 +174,17 @@ namespace OpenTK
// Detect whether SDL2 is supported // Detect whether SDL2 is supported
try try
{ {
if (OpenTK.Platform.SDL2.SDL.SDL_WasInit(0) == 0) if (!OpenTK.Platform.SDL2.SDL.WasInit(0))
{ {
var flags = OpenTK.Platform.SDL2.SDL.SDL_INIT_EVERYTHING; var flags = OpenTK.Platform.SDL2.SystemFlags.EVERYTHING;
flags &= ~OpenTK.Platform.SDL2.SDL.SDL_INIT_AUDIO; flags &= ~OpenTK.Platform.SDL2.SystemFlags.AUDIO;
if (OpenTK.Platform.SDL2.SDL.SDL_Init((uint)flags) == 0) if (OpenTK.Platform.SDL2.SDL.Init(flags) == 0)
{ {
supported = true; supported = true;
} }
else else
{ {
Debug.Print("SDL2 init failed with error: {0}", OpenTK.Platform.SDL2.SDL.SDL_GetError()); Debug.Print("SDL2 init failed with error: {0}", OpenTK.Platform.SDL2.SDL.GetError());
} }
} }
else else

File diff suppressed because it is too large Load diff

View file

@ -36,33 +36,33 @@ namespace OpenTK.Platform.SDL2
{ {
public Sdl2DisplayDeviceDriver() public Sdl2DisplayDeviceDriver()
{ {
int displays = SDL.SDL_GetNumVideoDisplays(); int displays = SDL.GetNumVideoDisplays();
for (int d = 0; d < displays; d++) for (int d = 0; d < displays; d++)
{ {
SDL.SDL_Rect bounds; Rect bounds;
SDL.SDL_GetDisplayBounds(d, out bounds); SDL.GetDisplayBounds(d, out bounds);
SDL.SDL_DisplayMode current_mode; DisplayMode current_mode;
SDL.SDL_GetCurrentDisplayMode(d, out current_mode); SDL.GetCurrentDisplayMode(d, out current_mode);
var mode_list = new List<DisplayResolution>(); var mode_list = new List<DisplayResolution>();
int num_modes = SDL.SDL_GetNumDisplayModes(d); int num_modes = SDL.GetNumDisplayModes(d);
for (int m = 0; m < num_modes; m++) for (int m = 0; m < num_modes; m++)
{ {
SDL.SDL_DisplayMode sdl_mode; DisplayMode sdl_mode;
SDL.SDL_GetDisplayMode(d, m, out sdl_mode); SDL.GetDisplayMode(d, m, out sdl_mode);
mode_list.Add(new DisplayResolution( mode_list.Add(new DisplayResolution(
bounds.x, bounds.y, bounds.X, bounds.Y,
sdl_mode.w, sdl_mode.h, sdl_mode.Width, sdl_mode.Height,
TranslateFormat(sdl_mode.format), TranslateFormat(sdl_mode.Format),
sdl_mode.refresh_rate)); sdl_mode.RefreshRate));
} }
var current_resolution = new DisplayResolution( var current_resolution = new DisplayResolution(
bounds.x, bounds.y, bounds.X, bounds.Y,
current_mode.w, current_mode.h, current_mode.Width, current_mode.Height,
TranslateFormat(current_mode.format), TranslateFormat(current_mode.Format),
current_mode.refresh_rate); current_mode.RefreshRate);
var device = new DisplayDevice( var device = new DisplayDevice(
current_resolution, d == 0, mode_list, TranslateBounds(bounds), d); current_resolution, d == 0, mode_list, TranslateBounds(bounds), d);
@ -79,13 +79,13 @@ namespace OpenTK.Platform.SDL2
{ {
int bpp; int bpp;
uint a, r, g, b; uint a, r, g, b;
SDL.SDL_PixelFormatEnumToMasks(format, out bpp, out r, out g, out b, out a); SDL.PixelFormatEnumToMasks(format, out bpp, out r, out g, out b, out a);
return bpp; return bpp;
} }
Rectangle TranslateBounds(SDL.SDL_Rect rect) Rectangle TranslateBounds(Rect rect)
{ {
return new Rectangle(rect.x, rect.y, rect.w, rect.h); return new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
} }
#endregion #endregion

View file

@ -50,7 +50,7 @@ namespace OpenTK.Platform.SDL2
else else
{ {
Window = new Sdl2WindowInfo( Window = new Sdl2WindowInfo(
SDL.SDL_CreateWindowFrom(window.Handle), SDL.CreateWindowFrom(window.Handle),
null); null);
} }
} }
@ -64,11 +64,11 @@ namespace OpenTK.Platform.SDL2
lock (SDL.Sync) lock (SDL.Sync)
{ {
SetGLAttributes(mode, shareContext, major, minor, flags); SetGLAttributes(mode, shareContext, major, minor, flags);
SdlContext = new ContextHandle(SDL.SDL_GL_CreateContext(Window.Handle)); SdlContext = new ContextHandle(SDL.GL.CreateContext(Window.Handle));
} }
if (SdlContext == ContextHandle.Zero) if (SdlContext == ContextHandle.Zero)
{ {
var error = SDL.SDL_GetError(); var error = SDL.GetError();
Debug.Print("SDL2 failed to create OpenGL context: {0}", error); Debug.Print("SDL2 failed to create OpenGL context: {0}", error);
throw new GraphicsContextException(error); throw new GraphicsContextException(error);
} }
@ -84,85 +84,93 @@ namespace OpenTK.Platform.SDL2
{ {
if (mode.AccumulatorFormat.BitsPerPixel > 0) if (mode.AccumulatorFormat.BitsPerPixel > 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ACCUM_ALPHA_SIZE, mode.AccumulatorFormat.Alpha); SDL.GL.SetAttribute(ContextAttribute.ACCUM_ALPHA_SIZE, mode.AccumulatorFormat.Alpha);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ACCUM_RED_SIZE, mode.AccumulatorFormat.Red); SDL.GL.SetAttribute(ContextAttribute.ACCUM_RED_SIZE, mode.AccumulatorFormat.Red);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ACCUM_GREEN_SIZE, mode.AccumulatorFormat.Green); SDL.GL.SetAttribute(ContextAttribute.ACCUM_GREEN_SIZE, mode.AccumulatorFormat.Green);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ACCUM_BLUE_SIZE, mode.AccumulatorFormat.Blue); SDL.GL.SetAttribute(ContextAttribute.ACCUM_BLUE_SIZE, mode.AccumulatorFormat.Blue);
} }
if (mode.Buffers > 0) if (mode.Buffers > 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DOUBLEBUFFER, mode.Buffers > 1 ? 1 : 0); SDL.GL.SetAttribute(ContextAttribute.DOUBLEBUFFER, mode.Buffers > 1 ? 1 : 0);
} }
if (mode.ColorFormat > 0) if (mode.ColorFormat > 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ALPHA_SIZE, mode.ColorFormat.Alpha); SDL.GL.SetAttribute(ContextAttribute.ALPHA_SIZE, mode.ColorFormat.Alpha);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_RED_SIZE, mode.ColorFormat.Red); SDL.GL.SetAttribute(ContextAttribute.RED_SIZE, mode.ColorFormat.Red);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_GREEN_SIZE, mode.ColorFormat.Green); SDL.GL.SetAttribute(ContextAttribute.GREEN_SIZE, mode.ColorFormat.Green);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, mode.ColorFormat.Blue); SDL.GL.SetAttribute(ContextAttribute.BLUE_SIZE, mode.ColorFormat.Blue);
} }
if (mode.Depth > 0) if (mode.Depth > 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DEPTH_SIZE, mode.Depth); SDL.GL.SetAttribute(ContextAttribute.DEPTH_SIZE, mode.Depth);
} }
if (mode.Samples > 0) if (mode.Samples > 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_MULTISAMPLEBUFFERS, 1); SDL.GL.SetAttribute(ContextAttribute.MULTISAMPLEBUFFERS, 1);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_MULTISAMPLESAMPLES, mode.Samples); SDL.GL.SetAttribute(ContextAttribute.MULTISAMPLESAMPLES, mode.Samples);
} }
if (mode.Stencil > 0) if (mode.Stencil > 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_STENCIL_SIZE, mode.Stereo ? 1 : 0); SDL.GL.SetAttribute(ContextAttribute.STENCIL_SIZE, mode.Stereo ? 1 : 0);
} }
if (mode.Stereo) if (mode.Stereo)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_STEREO, 1); SDL.GL.SetAttribute(ContextAttribute.STEREO, 1);
} }
if (major > 0) if (major > 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, major); SDL.GL.SetAttribute(ContextAttribute.CONTEXT_MAJOR_VERSION, major);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, minor); SDL.GL.SetAttribute(ContextAttribute.CONTEXT_MINOR_VERSION, minor);
} }
if ((flags & GraphicsContextFlags.Debug) != 0) if ((flags & GraphicsContextFlags.Debug) != 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_FLAGS, (int)SDL.SDL_GLcontext.SDL_GL_CONTEXT_DEBUG_FLAG); SDL.GL.SetAttribute(ContextAttribute.CONTEXT_FLAGS, ContextFlags.DEBUG);
}
if ((flags & GraphicsContextFlags.Embedded) != 0)
{
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_EGL, 1);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_ES);
}
if ((flags & GraphicsContextFlags.ForwardCompatible) != 0)
{
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE);
} }
/* /*
if ((flags & GraphicsContextFlags.Robust) != 0) if ((flags & GraphicsContextFlags.Robust) != 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_FLAGS, (int)SDL.SDL_GLcontext.SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG); SDL.GL.SetAttribute(ContextAttribute.CONTEXT_FLAGS, ContextFlags.ROBUST_ACCESS_FLAG);
} }
if ((flags & GraphicsContextFlags.ResetIsolation) != 0) if ((flags & GraphicsContextFlags.ResetIsolation) != 0)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_FLAGS, (int)SDL.SDL_GLcontext.SDL_GL_CONTEXT_RESET_ISOLATION_FLAG); SDL.GL.SetAttribute(ContextAttribute.CONTEXT_FLAGS, ContextFlags.RESET_ISOLATION_FLAG);
} }
*/ */
{
ContextProfileFlags cpflags = 0;
if ((flags & GraphicsContextFlags.Embedded) != 0)
{
cpflags |= ContextProfileFlags.ES;
SDL.GL.SetAttribute(ContextAttribute.CONTEXT_EGL, 1);
}
if ((flags & GraphicsContextFlags.ForwardCompatible) != 0)
{
cpflags |= ContextProfileFlags.CORE;
}
if (cpflags != 0)
{
SDL.GL.SetAttribute(ContextAttribute.CONTEXT_PROFILE_MASK, cpflags);
}
}
if (shareContext != null) if (shareContext != null)
{ {
if (shareContext.IsCurrent) if (shareContext.IsCurrent)
{ {
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); SDL.GL.SetAttribute(ContextAttribute.SHARE_WITH_CURRENT_CONTEXT, 1);
} }
else else
{ {
@ -177,7 +185,7 @@ namespace OpenTK.Platform.SDL2
public override void SwapBuffers() public override void SwapBuffers()
{ {
SDL.SDL_GL_SwapWindow(Window.Handle); SDL.GL.SwapWindow(Window.Handle);
} }
public override void MakeCurrent(IWindowInfo window) public override void MakeCurrent(IWindowInfo window)
@ -185,22 +193,22 @@ namespace OpenTK.Platform.SDL2
int result = 0; int result = 0;
if (window != null) if (window != null)
{ {
result = SDL.SDL_GL_MakeCurrent(window.Handle, SdlContext.Handle); result = SDL.GL.MakeCurrent(window.Handle, SdlContext.Handle);
} }
else else
{ {
result = SDL.SDL_GL_MakeCurrent(IntPtr.Zero, IntPtr.Zero); result = SDL.GL.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
} }
if (result < 0) if (result < 0)
{ {
Debug.Print("SDL2 MakeCurrent failed with: {0}", SDL.SDL_GetError()); Debug.Print("SDL2 MakeCurrent failed with: {0}", SDL.GetError());
} }
} }
public override IntPtr GetAddress(string function) public override IntPtr GetAddress(string function)
{ {
return SDL.SDL_GL_GetProcAddress(function); return SDL.GL.GetProcAddress(function);
} }
public override bool IsCurrent public override bool IsCurrent
@ -215,13 +223,13 @@ namespace OpenTK.Platform.SDL2
{ {
get get
{ {
return SDL.SDL_GL_GetSwapInterval(); return SDL.GL.GetSwapInterval();
} }
set set
{ {
if (SDL.SDL_GL_SetSwapInterval(value) < 0) if (SDL.GL.SetSwapInterval(value) < 0)
{ {
Debug.Print("SDL2 failed to set swap interval: {0}", SDL.SDL_GetError()); Debug.Print("SDL2 failed to set swap interval: {0}", SDL.GetError());
} }
} }
} }
@ -239,7 +247,7 @@ namespace OpenTK.Platform.SDL2
Debug.Print("Disposing {0}", GetType()); Debug.Print("Disposing {0}", GetType());
lock (SDL.Sync) lock (SDL.Sync)
{ {
SDL.SDL_GL_DeleteContext(SdlContext.Handle); SDL.GL.DeleteContext(SdlContext.Handle);
} }
} }
else else

View file

@ -45,22 +45,18 @@ namespace OpenTK.Platform.SDL2
readonly Sdl2Mouse mouse_driver = new Sdl2Mouse(); readonly Sdl2Mouse mouse_driver = new Sdl2Mouse();
readonly Sdl2JoystickDriver joystick_driver = new Sdl2JoystickDriver(); readonly Sdl2JoystickDriver joystick_driver = new Sdl2JoystickDriver();
readonly SDL.SDL_EventFilter EventFilterDelegate = FilterInputEvents; readonly EventFilter EventFilterDelegate = FilterInputEvents;
readonly IntPtr EventFilterPointer;
static int count; static int count;
bool disposed; bool disposed;
public Sdl2InputDriver() public Sdl2InputDriver()
{ {
EventFilterPointer = Marshal.GetFunctionPointerForDelegate(
EventFilterDelegate);
lock (SDL.Sync) lock (SDL.Sync)
{ {
driver_handle = new IntPtr(count++); driver_handle = new IntPtr(count++);
DriverHandles.Add(driver_handle, this); DriverHandles.Add(driver_handle, this);
SDL.SDL_AddEventWatch(EventFilterPointer, driver_handle); SDL.AddEventWatch(EventFilterDelegate, driver_handle);
} }
} }
@ -70,29 +66,29 @@ namespace OpenTK.Platform.SDL2
{ {
try try
{ {
SDL.SDL_Event ev = *(SDL.SDL_Event*)e; Event ev = *(Event*)e;
Sdl2InputDriver driver; Sdl2InputDriver driver;
if (DriverHandles.TryGetValue(driver_handle, out driver)) if (DriverHandles.TryGetValue(driver_handle, out driver))
{ {
switch (ev.type) switch (ev.Type)
{ {
case SDL.SDL_EventType.SDL_KEYDOWN: case EventType.KEYDOWN:
case SDL.SDL_EventType.SDL_KEYUP: case EventType.KEYUP:
driver.keyboard_driver.ProcessKeyboardEvent(ev.key); driver.keyboard_driver.ProcessKeyboardEvent(ev.Key);
break; break;
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: case EventType.MOUSEBUTTONDOWN:
case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: case EventType.MOUSEBUTTONUP:
driver.mouse_driver.ProcessMouseEvent(ev.button); driver.mouse_driver.ProcessMouseEvent(ev.Button);
break; break;
case SDL.SDL_EventType.SDL_MOUSEMOTION: case EventType.MOUSEMOTION:
driver.mouse_driver.ProcessMouseEvent(ev.motion); driver.mouse_driver.ProcessMouseEvent(ev.Motion);
break; break;
case SDL.SDL_EventType.SDL_MOUSEWHEEL: case EventType.MOUSEWHEEL:
driver.mouse_driver.ProcessWheelEvent(ev.wheel); driver.mouse_driver.ProcessWheelEvent(ev.Wheel);
break; break;
} }
} }
@ -192,7 +188,7 @@ namespace OpenTK.Platform.SDL2
joystick_driver.Dispose(); joystick_driver.Dispose();
lock (SDL.Sync) lock (SDL.Sync)
{ {
SDL.SDL_DelEventWatch(EventFilterPointer, IntPtr.Zero); SDL.DelEventWatch(EventFilterDelegate, IntPtr.Zero);
} }
DriverHandles.Remove(driver_handle); DriverHandles.Remove(driver_handle);
} }

View file

@ -60,7 +60,7 @@ namespace OpenTK.Platform.SDL2
{ {
joysticks.Clear(); joysticks.Clear();
int count = SDL.SDL_NumJoysticks(); int count = SDL.NumJoysticks();
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
JoystickDevice<Sdl2JoystickDetails> joystick = null; JoystickDevice<Sdl2JoystickDetails> joystick = null;
@ -69,16 +69,16 @@ namespace OpenTK.Platform.SDL2
int num_hats = 0; int num_hats = 0;
int num_balls = 0; int num_balls = 0;
IntPtr handle = SDL.SDL_JoystickOpen(i); IntPtr handle = SDL.JoystickOpen(i);
if (handle != IntPtr.Zero) if (handle != IntPtr.Zero)
{ {
num_axes = SDL.SDL_JoystickNumAxes(handle); num_axes = SDL.JoystickNumAxes(handle);
num_buttons = SDL.SDL_JoystickNumButtons(handle); num_buttons = SDL.JoystickNumButtons(handle);
num_hats = SDL.SDL_JoystickNumHats(handle); num_hats = SDL.JoystickNumHats(handle);
num_balls = SDL.SDL_JoystickNumBalls(handle); num_balls = SDL.JoystickNumBalls(handle);
joystick = new JoystickDevice<Sdl2JoystickDetails>(i, num_axes, num_buttons); joystick = new JoystickDevice<Sdl2JoystickDetails>(i, num_axes, num_buttons);
joystick.Description = SDL.SDL_JoystickName(handle); joystick.Description = SDL.JoystickName(handle);
joystick.Details.Handle = handle; joystick.Details.Handle = handle;
joystick.Details.HatCount = num_hats; joystick.Details.HatCount = num_hats;
joystick.Details.BallCount = num_balls; joystick.Details.BallCount = num_balls;
@ -101,7 +101,7 @@ namespace OpenTK.Platform.SDL2
public void Poll() public void Poll()
{ {
SDL.SDL_JoystickUpdate(); SDL.JoystickUpdate();
foreach (var j in joysticks) foreach (var j in joysticks)
{ {
var joystick = (JoystickDevice<Sdl2JoystickDetails>)j; var joystick = (JoystickDevice<Sdl2JoystickDetails>)j;
@ -110,13 +110,13 @@ namespace OpenTK.Platform.SDL2
for (int i = 0; i < joystick.Axis.Count; i++) for (int i = 0; i < joystick.Axis.Count; i++)
{ {
var axis = JoystickAxis.Axis0 + i; var axis = JoystickAxis.Axis0 + i;
joystick.SetAxis(axis, SDL.SDL_JoystickGetAxis(handle, i) * joystick.Details.RangeMultiplier); joystick.SetAxis(axis, SDL.JoystickGetAxis(handle, i) * joystick.Details.RangeMultiplier);
} }
for (int i = 0; i < joystick.Button.Count; i++) for (int i = 0; i < joystick.Button.Count; i++)
{ {
var button = JoystickButton.Button0 + i; var button = JoystickButton.Button0 + i;
joystick.SetButton(button, SDL.SDL_JoystickGetButton(handle, i) != 0); joystick.SetButton(button, SDL.JoystickGetButton(handle, i) != 0);
} }
} }
} }
@ -161,7 +161,7 @@ namespace OpenTK.Platform.SDL2
{ {
var joystick = (JoystickDevice<Sdl2JoystickDetails>)j; var joystick = (JoystickDevice<Sdl2JoystickDetails>)j;
IntPtr handle = joystick.Details.Handle; IntPtr handle = joystick.Details.Handle;
SDL.SDL_JoystickClose(handle); SDL.JoystickClose(handle);
} }
joysticks.Clear(); joysticks.Clear();

View file

@ -31,93 +31,93 @@ using OpenTK.Input;
namespace OpenTK.Platform.SDL2 namespace OpenTK.Platform.SDL2
{ {
using Code = SDL.SDL_Scancode; using Code = Scancode;
class Sdl2KeyMap : Dictionary<SDL.SDL_Scancode, Key> class Sdl2KeyMap : Dictionary<Scancode, Key>
{ {
public Sdl2KeyMap() public Sdl2KeyMap()
{ {
Add(Code.SDL_SCANCODE_ESCAPE, Key.Escape); Add(Code.ESCAPE, Key.Escape);
// Function keys // Function keys
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
{ {
Add(Code.SDL_SCANCODE_F1 + i, Key.F1 + i); Add(Code.F1 + i, Key.F1 + i);
} }
// Number keys (0-9) // Number keys (0-9)
Add(Code.SDL_SCANCODE_0, Key.Number0); Add(Code.Num0, Key.Number0);
for (int i = 0; i < 9; i++) for (int i = 0; i < 9; i++)
{ {
Add(Code.SDL_SCANCODE_1 + i, Key.Number1 + i); Add(Code.Num1 + i, Key.Number1 + i);
} }
// Letters (A-Z) // Letters (A-Z)
for (int i = 0; i < 26; i++) for (int i = 0; i < 26; i++)
{ {
Add(Code.SDL_SCANCODE_A + i, Key.A + i); Add(Code.A + i, Key.A + i);
} }
Add(Code.SDL_SCANCODE_TAB, Key.Tab); Add(Code.TAB, Key.Tab);
Add(Code.SDL_SCANCODE_CAPSLOCK, Key.CapsLock); Add(Code.CAPSLOCK, Key.CapsLock);
Add(Code.SDL_SCANCODE_LCTRL, Key.ControlLeft); Add(Code.LCTRL, Key.ControlLeft);
Add(Code.SDL_SCANCODE_LSHIFT, Key.ShiftLeft); Add(Code.LSHIFT, Key.ShiftLeft);
Add(Code.SDL_SCANCODE_LALT, Key.WinLeft); Add(Code.LALT, Key.WinLeft);
Add(Code.SDL_SCANCODE_MENU, Key.AltLeft); Add(Code.MENU, Key.AltLeft);
Add(Code.SDL_SCANCODE_SPACE, Key.Space); Add(Code.SPACE, Key.Space);
Add(Code.SDL_SCANCODE_RALT, Key.AltRight); Add(Code.RALT, Key.AltRight);
//Add(Code., Key.WinRight); //Add(Code., Key.WinRight);
Add(Code.SDL_SCANCODE_APPLICATION, Key.Menu); Add(Code.APPLICATION, Key.Menu);
Add(Code.SDL_SCANCODE_RCTRL, Key.ControlRight); Add(Code.RCTRL, Key.ControlRight);
Add(Code.SDL_SCANCODE_RSHIFT, Key.ShiftRight); Add(Code.RSHIFT, Key.ShiftRight);
Add(Code.SDL_SCANCODE_RETURN, Key.Enter); Add(Code.RETURN, Key.Enter);
Add(Code.SDL_SCANCODE_BACKSPACE, Key.BackSpace); Add(Code.BACKSPACE, Key.BackSpace);
Add(Code.SDL_SCANCODE_SEMICOLON, Key.Semicolon); // Varies by keyboard, ;: on Win2K/US Add(Code.SEMICOLON, Key.Semicolon); // Varies by keyboard, ;: on Win2K/US
Add(Code.SDL_SCANCODE_SLASH, Key.Slash); // Varies by keyboard, /? on Win2K/US Add(Code.SLASH, Key.Slash); // Varies by keyboard, /? on Win2K/US
Add(Code.SDL_SCANCODE_GRAVE, Key.Tilde); // Varies by keyboard, `~ on Win2K/US Add(Code.GRAVE, Key.Tilde); // Varies by keyboard, `~ on Win2K/US
Add(Code.SDL_SCANCODE_LEFTBRACKET, Key.BracketLeft); // Varies by keyboard, [{ on Win2K/US Add(Code.LEFTBRACKET, Key.BracketLeft); // Varies by keyboard, [{ on Win2K/US
Add(Code.SDL_SCANCODE_BACKSLASH, Key.BackSlash); // Varies by keyboard, \| on Win2K/US Add(Code.BACKSLASH, Key.BackSlash); // Varies by keyboard, \| on Win2K/US
Add(Code.SDL_SCANCODE_RIGHTBRACKET, Key.BracketRight); // Varies by keyboard, ]} on Win2K/US Add(Code.RIGHTBRACKET, Key.BracketRight); // Varies by keyboard, ]} on Win2K/US
Add(Code.SDL_SCANCODE_APOSTROPHE, Key.Quote); // Varies by keyboard, '" on Win2K/US Add(Code.APOSTROPHE, Key.Quote); // Varies by keyboard, '" on Win2K/US
Add(Code.SDL_SCANCODE_EQUALS, Key.Plus); Add(Code.EQUALS, Key.Plus);
Add(Code.SDL_SCANCODE_COMMA, Key.Comma); // Invariant: , Add(Code.COMMA, Key.Comma); // Invariant: ,
Add(Code.SDL_SCANCODE_MINUS, Key.Minus); // Invariant: - Add(Code.MINUS, Key.Minus); // Invariant: -
Add(Code.SDL_SCANCODE_PERIOD, Key.Period); // Invariant: . Add(Code.PERIOD, Key.Period); // Invariant: .
Add(Code.SDL_SCANCODE_HOME, Key.Home); Add(Code.HOME, Key.Home);
Add(Code.SDL_SCANCODE_END, Key.End); Add(Code.END, Key.End);
Add(Code.SDL_SCANCODE_DELETE, Key.Delete); Add(Code.DELETE, Key.Delete);
Add(Code.SDL_SCANCODE_PAGEUP, Key.PageUp); Add(Code.PAGEUP, Key.PageUp);
Add(Code.SDL_SCANCODE_PAGEDOWN, Key.PageDown); Add(Code.PAGEDOWN, Key.PageDown);
Add(Code.SDL_SCANCODE_PAUSE, Key.Pause); Add(Code.PAUSE, Key.Pause);
Add(Code.SDL_SCANCODE_NUMLOCKCLEAR, Key.NumLock); Add(Code.NUMLOCKCLEAR, Key.NumLock);
Add(Code.SDL_SCANCODE_SCROLLLOCK, Key.ScrollLock); Add(Code.SCROLLLOCK, Key.ScrollLock);
Add(Code.SDL_SCANCODE_PRINTSCREEN, Key.PrintScreen); Add(Code.PRINTSCREEN, Key.PrintScreen);
Add(Code.SDL_SCANCODE_CLEAR, Key.Clear); Add(Code.CLEAR, Key.Clear);
Add(Code.SDL_SCANCODE_INSERT, Key.Insert); Add(Code.INSERT, Key.Insert);
Add(Code.SDL_SCANCODE_SLEEP, Key.Sleep); Add(Code.SLEEP, Key.Sleep);
// Keypad // Keypad
for (int i = 0; i < 9; i++) for (int i = 0; i < 9; i++)
{ {
Add(Code.SDL_SCANCODE_KP_1 + i, Key.Keypad1 + i); Add(Code.KP_1 + i, Key.Keypad1 + i);
} }
Add(Code.SDL_SCANCODE_KP_0, Key.Keypad0); // Note: SDL2 goes KP_1..KP_9, then KP_0 Add(Code.KP_0, Key.Keypad0); // Note: SDL2 goes KP_1..KP_9, then KP_0
Add(Code.SDL_SCANCODE_KP_DECIMAL, Key.KeypadDecimal); Add(Code.KP_DECIMAL, Key.KeypadDecimal);
Add(Code.SDL_SCANCODE_KP_PLUS, Key.KeypadAdd); Add(Code.KP_PLUS, Key.KeypadAdd);
Add(Code.SDL_SCANCODE_KP_MINUS, Key.KeypadSubtract); Add(Code.KP_MINUS, Key.KeypadSubtract);
Add(Code.SDL_SCANCODE_KP_DIVIDE, Key.KeypadDivide); Add(Code.KP_DIVIDE, Key.KeypadDivide);
Add(Code.SDL_SCANCODE_KP_MULTIPLY, Key.KeypadMultiply); Add(Code.KP_MULTIPLY, Key.KeypadMultiply);
// Navigation // Navigation
Add(Code.SDL_SCANCODE_UP, Key.Up); Add(Code.UP, Key.Up);
Add(Code.SDL_SCANCODE_DOWN, Key.Down); Add(Code.DOWN, Key.Down);
Add(Code.SDL_SCANCODE_LEFT, Key.Left); Add(Code.LEFT, Key.Left);
Add(Code.SDL_SCANCODE_RIGHT, Key.Right); Add(Code.RIGHT, Key.Right);
} }
} }
} }

View file

@ -64,29 +64,29 @@ namespace OpenTK.Platform.SDL2
// Fixme: this does not appear to work as expected. // Fixme: this does not appear to work as expected.
void UpdateModifiers() void UpdateModifiers()
{ {
SDL.SDL_Keymod mod = SDL.SDL_GetModState(); Keymod mod = SDL.GetModState();
state.SetKeyState(Key.LAlt, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LALT, (mod & SDL.SDL_Keymod.KMOD_LALT) != 0); state.SetKeyState(Key.LAlt, (byte)Scancode.LALT, (mod & Keymod.LALT) != 0);
state.SetKeyState(Key.RAlt, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RALT, (mod & SDL.SDL_Keymod.KMOD_RALT) != 0); state.SetKeyState(Key.RAlt, (byte)Scancode.RALT, (mod & Keymod.RALT) != 0);
state.SetKeyState(Key.LControl, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LCTRL, (mod & SDL.SDL_Keymod.KMOD_LCTRL) != 0); state.SetKeyState(Key.LControl, (byte)Scancode.LCTRL, (mod & Keymod.LCTRL) != 0);
state.SetKeyState(Key.RControl, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RCTRL, (mod & SDL.SDL_Keymod.KMOD_RCTRL) != 0); state.SetKeyState(Key.RControl, (byte)Scancode.RCTRL, (mod & Keymod.CTRL) != 0);
state.SetKeyState(Key.LShift, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LSHIFT, (mod & SDL.SDL_Keymod.KMOD_LSHIFT) != 0); state.SetKeyState(Key.LShift, (byte)Scancode.LSHIFT, (mod & Keymod.LSHIFT) != 0);
state.SetKeyState(Key.RShift, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RSHIFT, (mod & SDL.SDL_Keymod.KMOD_RSHIFT) != 0); state.SetKeyState(Key.RShift, (byte)Scancode.RSHIFT, (mod & Keymod.RSHIFT) != 0);
state.SetKeyState(Key.Menu, (byte)SDL.SDL_Scancode.SDL_SCANCODE_APPLICATION, (mod & SDL.SDL_Keymod.KMOD_GUI) != 0); state.SetKeyState(Key.Menu, (byte)Scancode.APPLICATION, (mod & Keymod.GUI) != 0);
state.SetKeyState(Key.CapsLock, (byte)SDL.SDL_Scancode.SDL_SCANCODE_CAPSLOCK, (mod & SDL.SDL_Keymod.KMOD_CAPS) != 0); state.SetKeyState(Key.CapsLock, (byte)Scancode.CAPSLOCK, (mod & Keymod.CAPS) != 0);
state.SetKeyState(Key.NumLock, (byte)SDL.SDL_Scancode.SDL_SCANCODE_NUMLOCKCLEAR, (mod & SDL.SDL_Keymod.KMOD_NUM) != 0); state.SetKeyState(Key.NumLock, (byte)Scancode.NUMLOCKCLEAR, (mod & Keymod.NUM) != 0);
//state.SetKeyState(Key., (byte)SDL.SDL_Scancode.SDL_SCANCODE_MODE, (mod & SDL.SDL_Keymod.KMOD_MODE) != 0); //state.SetKeyState(Key., (byte)Scancode.MODE, (mod & Keymod.MODE) != 0);
} }
#endregion #endregion
#region Internal Members #region Internal Members
internal void ProcessKeyboardEvent(SDL.SDL_KeyboardEvent e) internal void ProcessKeyboardEvent(KeyboardEvent e)
{ {
Key key; Key key;
bool pressed = e.state != 0; bool pressed = e.State != 0;
var scancode = e.keysym.scancode; var scancode = e.Keysym.Scancode;
if (KeyMap.TryGetValue(scancode, out key)) if (KeyMap.TryGetValue(scancode, out key))
{ {
state.SetKeyState(key, (byte)scancode, pressed); state.SetKeyState(key, (byte)scancode, pressed);

View file

@ -54,23 +54,23 @@ namespace OpenTK.Platform.SDL2
#region Private Members #region Private Members
MouseButton TranslateButton(uint button) MouseButton TranslateButton(Button button)
{ {
switch (button) switch (button)
{ {
case SDL.SDL_BUTTON_LEFT: case Button.Left:
return MouseButton.Left; return MouseButton.Left;
case SDL.SDL_BUTTON_RIGHT: case Button.Right:
return MouseButton.Right; return MouseButton.Right;
case SDL.SDL_BUTTON_MIDDLE: case Button.Middle:
return MouseButton.Middle; return MouseButton.Middle;
case SDL.SDL_BUTTON_X1: case Button.X1:
return MouseButton.Button1; return MouseButton.Button1;
case SDL.SDL_BUTTON_X2: case Button.X2:
return MouseButton.Button2; return MouseButton.Button2;
default: default:
@ -95,24 +95,24 @@ namespace OpenTK.Platform.SDL2
#region Public Members #region Public Members
public void ProcessWheelEvent(SDL.SDL_MouseWheelEvent wheel) public void ProcessWheelEvent(MouseWheelEvent wheel)
{ {
state.WheelPrecise += wheel.y; state.WheelPrecise += wheel.Y;
mice[0].WheelPrecise += wheel.y; mice[0].WheelPrecise += wheel.Y;
} }
public void ProcessMouseEvent(SDL.SDL_MouseMotionEvent motion) public void ProcessMouseEvent(MouseMotionEvent motion)
{ {
state.X += motion.xrel; state.X += motion.Xrel;
state.Y += motion.yrel; state.Y += motion.Yrel;
mice[0].Position = new Point(motion.x, motion.y); mice[0].Position = new Point(motion.X, motion.Y);
} }
public void ProcessMouseEvent(SDL.SDL_MouseButtonEvent button) public void ProcessMouseEvent(MouseButtonEvent button)
{ {
bool pressed = button.state == SDL.SDL_PRESSED; bool pressed = button.State == State.Pressed;
SetButtonState(TranslateButton(button.button), pressed); SetButtonState(TranslateButton(button.Button), pressed);
mice[0][TranslateButton(button.button)] = pressed; mice[0][TranslateButton(button.Button)] = pressed;
} }
#endregion #endregion
@ -146,7 +146,7 @@ namespace OpenTK.Platform.SDL2
public void SetPosition(double x, double y) public void SetPosition(double x, double y)
{ {
SDL.SDL_WarpMouseInWindow(IntPtr.Zero, (int)x, (int)y); SDL.WarpMouseInWindow(IntPtr.Zero, (int)x, (int)y);
} }
#endregion #endregion

View file

@ -59,7 +59,7 @@ namespace OpenTK.Platform.SDL2
readonly IInputDriver input_driver = new Sdl2InputDriver(); readonly IInputDriver input_driver = new Sdl2InputDriver();
readonly SDL.SDL_EventFilter EventFilterDelegate = FilterEvents; readonly EventFilter EventFilterDelegate = FilterEvents;
static readonly Dictionary<uint, Sdl2NativeWindow> windows = static readonly Dictionary<uint, Sdl2NativeWindow> windows =
new Dictionary<uint, Sdl2NativeWindow>(); new Dictionary<uint, Sdl2NativeWindow>();
@ -73,24 +73,24 @@ namespace OpenTK.Platform.SDL2
{ {
var bounds = device.Bounds; var bounds = device.Bounds;
var flags = TranslateFlags(options); var flags = TranslateFlags(options);
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL; flags |= WindowFlags.OPENGL;
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE; flags |= WindowFlags.RESIZABLE;
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_HIDDEN; flags |= WindowFlags.HIDDEN;
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI; flags |= WindowFlags.ALLOW_HIGHDPI;
if ((flags & SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP) != 0 || if ((flags & WindowFlags.FULLSCREEN_DESKTOP) != 0 ||
(flags & SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN) != 0) (flags & WindowFlags.FULLSCREEN) != 0)
window_state = WindowState.Fullscreen; window_state = WindowState.Fullscreen;
IntPtr handle; IntPtr handle;
lock (SDL.Sync) lock (SDL.Sync)
{ {
handle = SDL.SDL_CreateWindow(title, bounds.Left + x, bounds.Top + y, width, height, flags); handle = SDL.CreateWindow(title, bounds.Left + x, bounds.Top + y, width, height, flags);
SDL.SDL_AddEventWatch(EventFilterDelegate, handle); SDL.AddEventWatch(EventFilterDelegate, handle);
SDL.SDL_PumpEvents(); SDL.PumpEvents();
} }
window = new Sdl2WindowInfo(handle, null); window = new Sdl2WindowInfo(handle, null);
window_id = SDL.SDL_GetWindowID(handle); window_id = SDL.GetWindowID(handle);
windows.Add(window_id, this); windows.Add(window_id, this);
window_title = title; window_title = title;
@ -100,19 +100,19 @@ namespace OpenTK.Platform.SDL2
#region Private Members #region Private Members
static SDL.SDL_WindowFlags TranslateFlags(GameWindowFlags flags) static WindowFlags TranslateFlags(GameWindowFlags flags)
{ {
switch (flags) switch (flags)
{ {
case GameWindowFlags.Fullscreen: case GameWindowFlags.Fullscreen:
return SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP; return WindowFlags.FULLSCREEN_DESKTOP;
default: default:
return (SDL.SDL_WindowFlags)0; return WindowFlags.Default;
} }
} }
static Key TranslateKey(SDL.SDL_Scancode scan) static Key TranslateKey(Scancode scan)
{ {
Key result = Key.Unknown; Key result = Key.Unknown;
if (map.ContainsKey(scan)) if (map.ContainsKey(scan))
@ -122,9 +122,9 @@ namespace OpenTK.Platform.SDL2
return result; return result;
} }
static Key TranslateKey(SDL.SDL_Keycode key) static Key TranslateKey(Keycode key)
{ {
SDL.SDL_Scancode scan = SDL.SDL_GetScancodeFromKey(key); Scancode scan = SDL.GetScancodeFromKey(key);
return TranslateKey(scan); return TranslateKey(scan);
} }
@ -135,53 +135,53 @@ namespace OpenTK.Platform.SDL2
try try
{ {
Sdl2NativeWindow window = null; Sdl2NativeWindow window = null;
SDL.SDL_Event ev = *(SDL.SDL_Event*)e; Event ev = *(Event*)e;
switch (ev.type) switch (ev.Type)
{ {
case SDL.SDL_EventType.SDL_WINDOWEVENT: case EventType.WINDOWEVENT:
if (windows.TryGetValue(ev.window.windowID, out window)) if (windows.TryGetValue(ev.Window.WindowID, out window))
{ {
ProcessWindowEvent(window, ev.window); ProcessWindowEvent(window, ev.Window);
processed = true; processed = true;
} }
break; break;
case SDL.SDL_EventType.SDL_KEYDOWN: case EventType.KEYDOWN:
case SDL.SDL_EventType.SDL_KEYUP: case EventType.KEYUP:
if (windows.TryGetValue(ev.key.windowID, out window)) if (windows.TryGetValue(ev.Key.WindowID, out window))
{ {
ProcessKeyEvent(window, ev); ProcessKeyEvent(window, ev);
processed = true; processed = true;
} }
break; break;
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: case EventType.MOUSEBUTTONDOWN:
case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: case EventType.MOUSEBUTTONUP:
if (windows.TryGetValue(ev.button.windowID, out window)) if (windows.TryGetValue(ev.Button.WindowID, out window))
{ {
ProcessButtonEvent(window, ev); ProcessButtonEvent(window, ev);
processed = true; processed = true;
} }
break; break;
case SDL.SDL_EventType.SDL_MOUSEMOTION: case EventType.MOUSEMOTION:
if (windows.TryGetValue(ev.motion.windowID, out window)) if (windows.TryGetValue(ev.Motion.WindowID, out window))
{ {
ProcessMotionEvent(window, ev); ProcessMotionEvent(window, ev);
processed = true; processed = true;
} }
break; break;
case SDL.SDL_EventType.SDL_MOUSEWHEEL: case EventType.MOUSEWHEEL:
if (windows.TryGetValue(ev.wheel.windowID, out window)) if (windows.TryGetValue(ev.Wheel.WindowID, out window))
{ {
ProcessWheelEvent(window, ev); ProcessWheelEvent(window, ev);
processed = true; processed = true;
} }
break; break;
case SDL.SDL_EventType.SDL_QUIT: case EventType.QUIT:
Debug.WriteLine("Sdl2 application quit"); Debug.WriteLine("Sdl2 application quit");
break; break;
} }
@ -194,68 +194,43 @@ namespace OpenTK.Platform.SDL2
return processed ? 0 : 1; return processed ? 0 : 1;
} }
static void ProcessButtonEvent(Sdl2NativeWindow window, SDL.SDL_Event ev) static void ProcessButtonEvent(Sdl2NativeWindow window, Event ev)
{ {
bool button_pressed = ev.button.state == SDL.SDL_PRESSED; bool button_pressed = ev.Button.State == State.Pressed;
// We need MouseUp events to be reported even if they occur // We need MouseUp events to be reported even if they occur
// outside the window. SetWindowGrab ensures we get them. // outside the window. SetWindowGrab ensures we get them.
if (window.CursorVisible) if (window.CursorVisible)
{ {
SDL.SDL_SetWindowGrab(window.window.Handle, SDL.SetWindowGrab(window.window.Handle,
button_pressed ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE); button_pressed ? true : false);
}
switch (ev.button.button)
{/*
case (byte)SDL.SDL_BUTTON_LEFT:
window.mouse[MouseButton.Left] = button_pressed;
break;
case (byte)SDL.SDL_BUTTON_MIDDLE:
window.mouse[MouseButton.Middle] = button_pressed;
break;
case (byte)SDL.SDL_BUTTON_RIGHT:
window.mouse[MouseButton.Right] = button_pressed;
break;
case (byte)SDL.SDL_BUTTON_X1:
window.mouse[MouseButton.Button1] = button_pressed;
break;
case (byte)SDL.SDL_BUTTON_X2:
window.mouse[MouseButton.Button2] = button_pressed;
break;
*/
} }
} }
static void ProcessKeyEvent(Sdl2NativeWindow window, SDL.SDL_Event ev) static void ProcessKeyEvent(Sdl2NativeWindow window, Event ev)
{ {
bool key_pressed = ev.key.state == SDL.SDL_PRESSED; bool key_pressed = ev.Key.State == State.Pressed;
var key = ev.key.keysym; var key = ev.Key.Keysym;
//window.keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed); //window.keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed);
} }
static void ProcessMotionEvent(Sdl2NativeWindow window, SDL.SDL_Event ev) static void ProcessMotionEvent(Sdl2NativeWindow window, Event ev)
{ {
float scale = window.ClientSize.Width / (float)window.Size.Width; float scale = window.ClientSize.Width / (float)window.Size.Width;
//window.mouse.Position = new Point( //window.mouse.Position = new Point(
// (int)(ev.motion.x * scale), (int)(ev.motion.y * scale)); // (int)(ev.motion.x * scale), (int)(ev.motion.y * scale));
} }
static void ProcessWheelEvent(Sdl2NativeWindow window, SDL.SDL_Event ev) static void ProcessWheelEvent(Sdl2NativeWindow window, Event ev)
{ {
//window.mouse.Wheel += ev.wheel.y; //window.mouse.Wheel += ev.wheel.y;
} }
static void ProcessWindowEvent(Sdl2NativeWindow window, SDL.SDL_WindowEvent e) static void ProcessWindowEvent(Sdl2NativeWindow window, WindowEvent e)
{ {
switch (e.windowEvent) switch (e.Event)
{ {
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE: case WindowEventID.CLOSE:
var close_args = new System.ComponentModel.CancelEventArgs(); var close_args = new System.ComponentModel.CancelEventArgs();
window.Closing(window, close_args); window.Closing(window, close_args);
if (!close_args.Cancel) if (!close_args.Cancel)
@ -265,66 +240,66 @@ namespace OpenTK.Platform.SDL2
} }
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_ENTER: case WindowEventID.ENTER:
window.MouseEnter(window, EventArgs.Empty); window.MouseEnter(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_LEAVE: case WindowEventID.LEAVE:
window.MouseLeave(window, EventArgs.Empty); window.MouseLeave(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_EXPOSED: case WindowEventID.EXPOSED:
// do nothing // do nothing
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: case WindowEventID.FOCUS_GAINED:
window.is_focused = true; window.is_focused = true;
window.FocusedChanged(window, EventArgs.Empty); window.FocusedChanged(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: case WindowEventID.FOCUS_LOST:
window.is_focused = false; window.is_focused = false;
window.FocusedChanged(window, EventArgs.Empty); window.FocusedChanged(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_HIDDEN: case WindowEventID.HIDDEN:
window.is_visible = false; window.is_visible = false;
window.VisibleChanged(window, EventArgs.Empty); window.VisibleChanged(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SHOWN: case WindowEventID.SHOWN:
window.is_visible = true; window.is_visible = true;
window.VisibleChanged(window, EventArgs.Empty); window.VisibleChanged(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MAXIMIZED: case WindowEventID.MAXIMIZED:
window.previous_window_state = window.window_state; window.previous_window_state = window.window_state;
window.window_state = OpenTK.WindowState.Maximized; window.window_state = OpenTK.WindowState.Maximized;
window.WindowStateChanged(window, EventArgs.Empty); window.WindowStateChanged(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MINIMIZED: case WindowEventID.MINIMIZED:
window.previous_window_state = window.window_state; window.previous_window_state = window.window_state;
window.window_state = OpenTK.WindowState.Minimized; window.window_state = OpenTK.WindowState.Minimized;
window.WindowStateChanged(window, EventArgs.Empty); window.WindowStateChanged(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESTORED: case WindowEventID.RESTORED:
window.window_state = window.previous_window_state; window.window_state = window.previous_window_state;
window.WindowStateChanged(window, EventArgs.Empty); window.WindowStateChanged(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MOVED: case WindowEventID.MOVED:
window.Move(window, EventArgs.Empty); window.Move(window, EventArgs.Empty);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED: case WindowEventID.RESIZED:
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED: case WindowEventID.SIZE_CHANGED:
window.Resize(window, EventArgs.Empty); window.Resize(window, EventArgs.Empty);
break; break;
default: default:
Debug.Print("SDL2 unhandled event: {0}", e.type); Debug.Print("SDL2 unhandled event: {0}", e.Type);
break; break;
} }
} }
@ -337,12 +312,12 @@ namespace OpenTK.Platform.SDL2
{ {
lock (SDL.Sync) lock (SDL.Sync)
{ {
SDL.SDL_DelEventWatch(EventFilterDelegate, window.Handle); SDL.DelEventWatch(EventFilterDelegate, window.Handle);
if (windows.ContainsKey(window_id)) if (windows.ContainsKey(window_id))
{ {
windows.Remove(window_id); windows.Remove(window_id);
} }
SDL.SDL_DestroyWindow(window.Handle); SDL.DestroyWindow(window.Handle);
} }
} }
@ -352,19 +327,17 @@ namespace OpenTK.Platform.SDL2
void GrabCursor(bool grab) void GrabCursor(bool grab)
{ {
SDL.SDL_ShowCursor(grab ? 0 : 1); SDL.ShowCursor(!grab);
SDL.SDL_SetWindowGrab(window.Handle, SDL.SetWindowGrab(window.Handle, grab);
grab ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE); SDL.SetRelativeMouseMode(grab);
SDL.SDL_SetRelativeMouseMode(
grab ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE);
} }
// Hack to force WindowState events to be pumped // Hack to force WindowState events to be pumped
void HideShowWindowHack() void HideShowWindowHack()
{ {
SDL.SDL_HideWindow(window.Handle); SDL.HideWindow(window.Handle);
ProcessEvents(); ProcessEvents();
SDL.SDL_ShowWindow(window.Handle); SDL.ShowWindow(window.Handle);
ProcessEvents(); ProcessEvents();
} }
@ -376,16 +349,16 @@ namespace OpenTK.Platform.SDL2
switch (state) switch (state)
{ {
case WindowState.Fullscreen: case WindowState.Fullscreen:
SDL.SDL_SetWindowFullscreen(window.Handle, 0); SDL.SetWindowFullscreen(window.Handle, 0);
break; break;
case WindowState.Maximized: case WindowState.Maximized:
SDL.SDL_RestoreWindow(window.Handle); SDL.RestoreWindow(window.Handle);
HideShowWindowHack(); HideShowWindowHack();
break; break;
case WindowState.Minimized: case WindowState.Minimized:
SDL.SDL_RestoreWindow(window.Handle); SDL.RestoreWindow(window.Handle);
break; break;
} }
@ -423,13 +396,13 @@ namespace OpenTK.Platform.SDL2
{ {
Debug.Print("SDL2 destroying window {0}", window.Handle); Debug.Print("SDL2 destroying window {0}", window.Handle);
SDL.SDL_Event e = new SDL.SDL_Event(); Event e = new Event();
e.type = SDL.SDL_EventType.SDL_WINDOWEVENT; e.Type = EventType.WINDOWEVENT;
e.window.windowEvent = SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE; e.Window.Event = WindowEventID.CLOSE;
e.window.windowID = window_id; e.Window.WindowID = window_id;
lock (SDL.Sync) lock (SDL.Sync)
{ {
SDL.SDL_PushEvent(ref e); SDL.PushEvent(ref e);
} }
} }
} }
@ -441,7 +414,7 @@ namespace OpenTK.Platform.SDL2
{ {
if (Exists) if (Exists)
{ {
SDL.SDL_PumpEvents(); SDL.PumpEvents();
} }
} }
} }
@ -484,15 +457,15 @@ namespace OpenTK.Platform.SDL2
ImageLockMode.ReadWrite, ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb); PixelFormat.Format32bppArgb);
IntPtr surface = SDL.SDL_CreateRGBSurfaceFrom( IntPtr surface = SDL.CreateRGBSurfaceFrom(
data.Scan0, data.Width, data.Height, 32, data.Stride, data.Scan0, data.Width, data.Height, 32, data.Stride,
0x00ff0000u, 0x0000ff00u, 0x000000ffu, 0xff000000u); 0x00ff0000u, 0x0000ff00u, 0x000000ffu, 0xff000000u);
// Update the window icon // Update the window icon
SDL.SDL_SetWindowIcon(window.Handle, surface); SDL.SetWindowIcon(window.Handle, surface);
// Free the SDL surface as it is no longer needed // Free the SDL surface as it is no longer needed
SDL.SDL_FreeSurface(surface); SDL.FreeSurface(surface);
bmp.UnlockBits(data); bmp.UnlockBits(data);
} }
@ -500,7 +473,7 @@ namespace OpenTK.Platform.SDL2
else else
{ {
// Clear the window icon // Clear the window icon
SDL.SDL_SetWindowIcon(window.Handle, IntPtr.Zero); SDL.SetWindowIcon(window.Handle, IntPtr.Zero);
} }
icon = value; icon = value;
@ -518,7 +491,7 @@ namespace OpenTK.Platform.SDL2
{ {
if (Exists) if (Exists)
{ {
return SDL.SDL_GetWindowTitle(window.Handle); return SDL.GetWindowTitle(window.Handle);
} }
return String.Empty; return String.Empty;
} }
@ -529,7 +502,7 @@ namespace OpenTK.Platform.SDL2
{ {
if (Exists) if (Exists)
{ {
SDL.SDL_SetWindowTitle(window.Handle, value); SDL.SetWindowTitle(window.Handle, value);
window_title = value; window_title = value;
} }
} }
@ -557,9 +530,9 @@ namespace OpenTK.Platform.SDL2
if (Exists) if (Exists)
{ {
if (value) if (value)
SDL.SDL_ShowWindow(window.Handle); SDL.ShowWindow(window.Handle);
else else
SDL.SDL_HideWindow(window.Handle); SDL.HideWindow(window.Handle);
} }
} }
} }
@ -600,14 +573,14 @@ namespace OpenTK.Platform.SDL2
{ {
case WindowState.Fullscreen: case WindowState.Fullscreen:
RestoreWindow(); RestoreWindow();
if (SDL.SDL_SetWindowFullscreen(window.Handle, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP) < 0) if (SDL.SetWindowFullscreen(window.Handle, (uint)WindowFlags.FULLSCREEN_DESKTOP) < 0)
{ {
if (SDL.SDL_SetWindowFullscreen(window.Handle, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN) < 0) if (SDL.SetWindowFullscreen(window.Handle, (uint)WindowFlags.FULLSCREEN) < 0)
{ {
Debug.Print("SDL2 failed to enter fullscreen mode: {0}", SDL.SDL_GetError()); Debug.Print("SDL2 failed to enter fullscreen mode: {0}", SDL.GetError());
} }
} }
SDL.SDL_RaiseWindow(window.Handle); SDL.RaiseWindow(window.Handle);
// There is no "fullscreen" message in the event loop // There is no "fullscreen" message in the event loop
// so we have to mark that ourselves // so we have to mark that ourselves
window_state = WindowState.Fullscreen; window_state = WindowState.Fullscreen;
@ -615,12 +588,12 @@ namespace OpenTK.Platform.SDL2
case WindowState.Maximized: case WindowState.Maximized:
RestoreWindow(); RestoreWindow();
SDL.SDL_MaximizeWindow(window.Handle); SDL.MaximizeWindow(window.Handle);
HideShowWindowHack(); HideShowWindowHack();
break; break;
case WindowState.Minimized: case WindowState.Minimized:
SDL.SDL_MinimizeWindow(window.Handle); SDL.MinimizeWindow(window.Handle);
break; break;
case WindowState.Normal: case WindowState.Normal:
@ -654,12 +627,12 @@ namespace OpenTK.Platform.SDL2
switch (value) switch (value)
{ {
case WindowBorder.Resizable: case WindowBorder.Resizable:
SDL.SDL_SetWindowBordered(window.Handle, SDL.SDL_bool.SDL_TRUE); SDL.SetWindowBordered(window.Handle, true);
window_border = WindowBorder.Resizable; window_border = WindowBorder.Resizable;
break; break;
case WindowBorder.Hidden: case WindowBorder.Hidden:
SDL.SDL_SetWindowBordered(window.Handle, SDL.SDL_bool.SDL_FALSE); SDL.SetWindowBordered(window.Handle, false);
window_border = WindowBorder.Hidden; window_border = WindowBorder.Hidden;
break; break;
@ -700,7 +673,7 @@ namespace OpenTK.Platform.SDL2
if (Exists) if (Exists)
{ {
int x, y; int x, y;
SDL.SDL_GetWindowPosition(window.Handle, out x, out y); SDL.GetWindowPosition(window.Handle, out x, out y);
return new Point(x, y); return new Point(x, y);
} }
return new Point(); return new Point();
@ -712,7 +685,7 @@ namespace OpenTK.Platform.SDL2
{ {
if (Exists) if (Exists)
{ {
SDL.SDL_SetWindowPosition(window.Handle, value.X, value.Y); SDL.SetWindowPosition(window.Handle, value.X, value.Y);
} }
} }
} }
@ -727,7 +700,7 @@ namespace OpenTK.Platform.SDL2
if (Exists) if (Exists)
{ {
int w, h; int w, h;
SDL.SDL_GetWindowSize(window.Handle, out w, out h); SDL.GetWindowSize(window.Handle, out w, out h);
return new Size(w, h); return new Size(w, h);
} }
return new Size(); return new Size();
@ -739,7 +712,7 @@ namespace OpenTK.Platform.SDL2
{ {
if (Exists) if (Exists)
{ {
SDL.SDL_SetWindowSize(window.Handle, value.Width, value.Height); SDL.SetWindowSize(window.Handle, value.Width, value.Height);
} }
} }
} }
@ -814,11 +787,11 @@ namespace OpenTK.Platform.SDL2
{ {
// SDL > 2.0.0 supports SDL_GL_GetDrawableSize for // SDL > 2.0.0 supports SDL_GL_GetDrawableSize for
// hidpi windows. // hidpi windows.
SDL.SDL_GL_GetDrawableSize(window.Handle, out w, out h); SDL.GL.GetDrawableSize(window.Handle, out w, out h);
} }
else else
{ {
SDL.SDL_GetWindowSize(window.Handle, out w, out h); SDL.GetWindowSize(window.Handle, out w, out h);
} }
return new Size(w, h); return new Size(w, h);
} }

View file

@ -272,7 +272,7 @@ namespace OpenTK.Platform
public static IWindowInfo CreateSdl2WindowInfo(IntPtr windowHandle) public static IWindowInfo CreateSdl2WindowInfo(IntPtr windowHandle)
{ {
return new OpenTK.Platform.SDL2.Sdl2WindowInfo( return new OpenTK.Platform.SDL2.Sdl2WindowInfo(
SDL2.SDL.SDL_CreateWindowFrom(windowHandle), null); SDL2.SDL.CreateWindowFrom(windowHandle), null);
} }
#endregion #endregion