Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Support for SDL2 backend #22

merged 52 commits into from Oct 3, 2013


None yet
2 participants

thefiddler commented Oct 2, 2013

With SDL2 officially released, it makes sense to add a SDL2 backend to OpenTK. This backend is used automatically if the correct unmanaged SDL2 library is available. Prebuilt versions of SDL2 are included in the Dependencies/ folder.


  • Improved stability on Mac OS X
  • Simplified porting to new platforms
  • Future proofing
  • Support for haptic and multitouch devices


  • OpenTK.GLControl is not supported by the SDL2 backend. This is a SDL2 limitation (SDL_CreateWindowFrom does not set the SDL_WINDOW_OPENGL flag and context creation fails.)
  • Only one MouseDevice and one KeyboardDevice is supported. This is a SDL2 limitation.
  • "Retina" displays are not supported on Mac OS X. This is a SDL2 limitation.

Current status:

  • Feature parity with the native backends, except for SDL2 limitations.
  • DisplayDevice.ChangeResolution is not yet implemented (call will succeed but resolution will not change.)

This branch also contains a fix for NRE in MouseDevice on Windows and a more stable Example Browser (examples are launched on separate processes.)

thefiddler added some commits Sep 27, 2013

Squashed 'Source/OpenTK/Platform/SDL2/sdl2-cs/' content from commit b…

git-subtree-dir: Source/OpenTK/Platform/SDL2/sdl2-cs
git-subtree-split: b434bacba859a036b11ec3ac76c3baa058764769
Integrated sdl2-cs bindings for sdl2
This is using the sdl2/pure branch of https://github.com/thefiddler/SDL2-CS.git which contains the SDL2 bindings without MiniTK.
Implemented SDL2 platform using sdl2-cs
This is a new platform that can be used then sdl2 is installed on the target system. SDL2 is commercially supported by Valve and provides better support for MacOS compared to our current implementation (Cocoa vs Carbon). It will also help us introduce faster support for new platforms. Existing platforms remain as a fallback and will be automatically used if sdl2 is not installed.

Please note that this is still a work in progress. The new mouse and keyboard API is not supported yet. Due to limitations of sdl2, multiple mice/keyboards are also not supported.
Use Wgl.Import.GetCurrentContext DllImport directly
Wgl is implemented using the regular DllImport + delegate approach, in
order to support extensions. The delegates have not been armed at this
point, so we need to use the DllImport directly.

The whole wgl API should be revisited (we are using only a tiny
Use OpenGL window flag and switch to "fake" fullscreen mode
The OpenGL flag is required when using SDL2 on Windows. Fake fullscreen
works much better on modern monitors and systems with multiple monitors.
Increased the text surface size
This is necessary for high-dpi monitors which render text at double
Implemented WindowState, WindowBorder and Icon
SDL2 suffers many of the issues we encountered when implementing
OpenTK.Platform.Windows. Workarounds are now in place to make
WindowState changes behave reasonably.
Fixed decoding of Icons
The rgba mask for decoding icon surfaces was incorrect. Icons should
appear as expected now.
Free SDL surface after updating the window icon
The SDL surface is no longer needed and will cause a memory leak if not
freed. Additionally, the decoded BitmapData should not be unlocked until
after updating the window icon, as it may be moved by the GC otherwise.
Added high-res icons to Example Browser
The Example Browser should now look better on high resolution screens.
Renamed Sdl2InputBase to Sdl2InputDriver
Sdl2InputDriver describes the role and function of this class more
Fixed return values of keyboard functions
SDL_GetKeyFromScancode and SDL_GetScancodeFromKey should return a
SDL_Keycode or SDL_Scancode respectively, instead of void.
Use scancodes for KeyboardDevice states
This resolves the issue where different layouts would return different
keys when pressing the same physical key. A new API must be introduced
for text input.
Attempted to fix key modifier state
SDL2 does not report keydown and keyup events on modifiers keys until
you release them. Attempted to fix this using SDL_GetModifierState, but
this does not appear to work. Should hit upstream.
Workaround for NRE on WinRawMouse driver
On some systems, the Windows Raw Input driver fails to find the name of
a mouse device, causing a NRE. Since the name is not vital information,
a dummy name is generated as a workaround.
Implemented Sdl2GLControl
SDL2 still fails with "not an OpenGL window".
Removed terrible hack for SDL_WINDOW_OPENGL
SDL2 will refuse to create an OpenGL context on a window with the SDL_WINDOW_FOREIGN flag (i.e. windows that are passed to SDL2 through SDL_CreateWindowFrom). This must be fixed upstream.
Improved resource disposal
Added IDisposable interface to all classes holding native data that must be freed. OpenTK.Toolkit.Init() now returns an IDisposable instance that can be used to cleanup all native data held by OpenTK. This is useful when re-initializing OpenTK (possibly in a new AppDomain), as is the case in the Example browser.
Clean up OpenTK after every example
Use the IDisposable instance returned by OpenTK.Toolkit.Init() to shutdown OpenTK after the end of each example run.
Fixed build under xbuild
Mono xbuild does not recognize the DebugType none element in the project configuration. Remove this in order to make it happy.
MakeCurrent(null) should make context non-current
This fixes the crash in the "GameWindow Threading" example.
Fixed invalid version string on git
Git commit hashcodes are not compatible with the 16bit integer values found in .Net version numbers. Use the git commit count as a version number instead.
Use separate process for executing examples
Using a separate, isolated process protects against incompatible interactions between the Example Browser (WinForms) and the executing example (native or SDL). It also protects the main GUI from crashes in the example code.
Marked class as partial
This allows extension of the class without touching the upstream code.
Improved diagnostics in Dispose()
Leaked and disposed resources are now always reported.
Added missing x64 unmanaged dependencies
The .gitignore file ignores directories starting with x64, causing these depedendencies to be missed. Fixed by adding them manually.
Fixed unsetting current context in SDL2
SDL_GL_MakeCurrent(IntPtr.Zero, IntPtr.Zero) is the correct way to
remove the current OpenGL context from the calling thread. Fixes
threaded rendering on Windows.
Create Sdl2NativeWindow hidden
This conforms to the other platform drivers and looks better in general
(no delay between window creation and OpenGL context creation.)
MouseButtonUp and MouseMove fixes
MouseMove no longer leaves the window borders when the cursor is
invisible. MouseButtonUp events are now reported even when they occur
outside the window. This aligns SDL2 behavior with the native drivers.
Improved debugging messages
Exceptions that occur in the example process are now logged.
Added support for OpenGL ES through SDL2
If SDL2 is supported, the PlatformFactory will now use it for creating the OpenGL ES context. Previously, it would revert to the native drivers when GraphicsContextFlag.Embedded was specified.
Reload WGL entry points in WinGLContext()
This fixes AccessViolationException when creating multiple contexts on
Intel hardware.
Serialize Sdl2NativeWindow methods
This change, along with moving DestroyWindow() to the Dispose() method
fixes crashes on shutdown.
Merge branch 'sdl2' of https://github.com/thefiddler/opentk
Load ES entry points even on desktop
Some drivers / operating systems export ES entry points as extensions on regular "desktop" GraphicsContexts. Attempt to load them even if the user has not explicitly asked for an ES context.
NRE -> GraphicsContextMissingException
Throw a GraphicsContextMissingException if GraphicsBindingBase.LoadAll() is called without a current GraphicsContext.
Fallback to desktop context if embedded fails
Starting with OpenGL 4.1 and the ARB_ES2_compatibility extension, desktop contexts can execute OpenGL|ES code. This fallback will allow platforms to execute OpenGL|ES code even if EGL is not available (e.g. Nvidia/Windows).
Throw extension if context creation fails
This aligns behavior with that of the other platforms.

andykorth added a commit that referenced this pull request Oct 3, 2013

@andykorth andykorth merged commit a9e2a10 into andykorth:master Oct 3, 2013

thefiddler added a commit to thefiddler/opentk that referenced this pull request Jan 3, 2014

Remove bogus GraphicsMode.Index check. Fixes #22
GraphicsMode.Index is set by the platform-specific context constructor,
which is invoked after the X11GLControl constructor. It does not make
sense to check GraphicsMode.Index in the X11GLControl constructor, as it
is never set at that point.

thefiddler added a commit to thefiddler/opentk that referenced this pull request Jan 3, 2014

Remove bogus GraphicsMode.Index check. Fixes #22
GraphicsMode.Index is set by the platform-specific context constructor,
which is invoked after the X11GLControl constructor. It does not make
sense to check GraphicsMode.Index in the X11GLControl constructor, as it
is never set at that point.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment