[OpenTK] Introduce OpenTK (#336)
[platform/core/csapi/tizenfx.git] / external / src / OpenTK / OpenTK / Platform / SDL2 / Sdl2Factory.cs
1 //
2 // The Open Toolkit Library License
3 //
4 // Copyright (c) 2006 - 2013 Stefanos Apostolopoulos for the Open Toolkit library.
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
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.
24 //
25
26 using System;
27 using OpenTK.Graphics;
28 using OpenTK.Input;
29
30 namespace OpenTK.Platform.SDL2
31 {
32     internal class Sdl2Factory : PlatformFactoryBase
33     {
34         private readonly object inputDriverLock = new object();
35         private Sdl2InputDriver inputDriver;
36
37         /// <summary>
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.
42         /// </summary>
43         /// <remarks>>
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.
46         /// </remarks>
47         public static bool UseFullscreenDesktop { get; set; }
48
49         public Sdl2Factory()
50         {
51             UseFullscreenDesktop = true;
52         }
53
54         public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device)
55         {
56             return new Sdl2NativeWindow(x, y, width, height, title, options, device);
57         }
58
59         public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
60         {
61             return new Sdl2DisplayDeviceDriver();
62         }
63
64         public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
65         {
66             return new Sdl2GraphicsContext(mode, window, shareContext, major, minor, flags);
67         }
68
69         public override IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
70         {
71             throw new NotImplementedException();
72         }
73
74         public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
75         {
76             return (GraphicsContext.GetCurrentContextDelegate)delegate
77             {
78                 return Sdl2GraphicsContext.GetCurrentContext();
79             };
80         }
81
82         public override IKeyboardDriver2 CreateKeyboardDriver()
83         {
84             return GetInputDriver().KeyboardDriver;
85         }
86
87         public override IMouseDriver2 CreateMouseDriver()
88         {
89             return GetInputDriver().MouseDriver;
90         }
91
92         public override IJoystickDriver2 CreateJoystickDriver()
93         {
94             return GetInputDriver().JoystickDriver;
95         }
96
97         protected override void Dispose(bool manual)
98         {
99             if (!IsDisposed)
100             {
101                 if (manual)
102                 {
103                     if (inputDriver != null)
104                     {
105                         inputDriver.Dispose();
106                         inputDriver = null;
107                     }
108                 }
109
110                 base.Dispose(manual);
111             }
112         }
113
114         private Sdl2InputDriver GetInputDriver()
115         {
116             if (inputDriver == null)
117             {
118                 lock (inputDriverLock)
119                 {
120                     // Check again inside the lock
121                     if (inputDriver == null)
122                     {
123                         inputDriver = new Sdl2InputDriver();
124                     }
125                 }
126             }
127
128             return inputDriver;
129         }
130     }
131 }
132