2 // The Open Toolkit Library License
4 // Copyright (c) 2006 - 2013 Stefanos Apostolopoulos for the Open Toolkit library.
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights to
9 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 // the Software, and to permit persons to whom the Software is furnished to do
11 // so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
27 using OpenTK.Graphics;
30 namespace OpenTK.Platform.SDL2
32 internal class Sdl2Factory : PlatformFactoryBase
34 private readonly object inputDriverLock = new object();
35 private Sdl2InputDriver inputDriver;
38 /// Gets or sets a value indicating whether to use SDL2 fullscreen-desktop mode
39 /// for fullscreen windows. When true, then GameWindow instances will not change
40 /// DisplayDevice resolutions when going fullscreen. When false, fullscreen GameWindows
41 /// will change the device resolution to match their size.
44 /// This is a workaround for the lack of ChangeResolution support in SDL2.
45 /// When and if this changes upstream, we should remove this code.
47 public static bool UseFullscreenDesktop { get; set; }
51 UseFullscreenDesktop = true;
54 public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device)
56 return new Sdl2NativeWindow(x, y, width, height, title, options, device);
59 public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
61 return new Sdl2DisplayDeviceDriver();
64 public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
66 return new Sdl2GraphicsContext(mode, window, shareContext, major, minor, flags);
69 public override IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
71 throw new NotImplementedException();
74 public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
76 return (GraphicsContext.GetCurrentContextDelegate)delegate
78 return Sdl2GraphicsContext.GetCurrentContext();
82 public override IKeyboardDriver2 CreateKeyboardDriver()
84 return GetInputDriver().KeyboardDriver;
87 public override IMouseDriver2 CreateMouseDriver()
89 return GetInputDriver().MouseDriver;
92 public override IJoystickDriver2 CreateJoystickDriver()
94 return GetInputDriver().JoystickDriver;
97 protected override void Dispose(bool manual)
103 if (inputDriver != null)
105 inputDriver.Dispose();
110 base.Dispose(manual);
114 private Sdl2InputDriver GetInputDriver()
116 if (inputDriver == null)
118 lock (inputDriverLock)
120 // Check again inside the lock
121 if (inputDriver == null)
123 inputDriver = new Sdl2InputDriver();