2 // The Open Toolkit Library License
4 // Copyright (c) 2006 - 2013 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.Platform;
28 using System.Diagnostics;
33 /// Provides static methods to manage an OpenTK application.
35 public sealed class Toolkit : IDisposable
37 private Factory platform_factory;
38 private static Toolkit toolkit;
40 private volatile static bool initialized;
41 private static readonly object InitLock = new object();
43 private Toolkit(Factory factory)
47 throw new ArgumentNullException("factory");
49 platform_factory = factory;
53 /// Initializes OpenTK with default options.
57 /// You *must* call this method if you are combining OpenTK with a
58 /// third-party windowing toolkit (e.g. GTK#). In this case, this should be the
59 /// first method called by your application:
61 /// static void Main()
63 /// using (OpenTK.Toolkit.Init())
71 /// The reason is that some toolkits do not configure the underlying platform
72 /// correctly or configure it in a way that is incompatible with OpenTK.
73 /// Calling this method first ensures that OpenTK is given the chance to
74 /// initialize itself and configure the platform correctly.
78 /// An IDisposable instance that you can use to dispose of the resources
79 /// consumed by OpenTK.
81 public static Toolkit Init()
83 return Init(ToolkitOptions.Default);
87 /// Initializes OpenTK with the specified options. Use this method
88 /// to influence the OpenTK.Platform implementation that will be used.
92 /// You *must* call this method if you are combining OpenTK with a
93 /// third-party windowing toolkit (e.g. GTK#). In this case, this should be the
94 /// first method called by your application:
96 /// static void Main()
98 /// using (OpenTK.Toolkit.Init())
106 /// The reason is that some toolkits do not configure the underlying platform
107 /// correctly or configure it in a way that is incompatible with OpenTK.
108 /// Calling this method first ensures that OpenTK is given the chance to
109 /// initialize itself and configure the platform correctly.
112 /// <param name="options">A <c>ToolkitOptions</c> instance
113 /// containing the desired options.</param>
115 /// An IDisposable instance that you can use to dispose of the resources
116 /// consumed by OpenTK.
118 public static Toolkit Init(ToolkitOptions options)
122 throw new ArgumentNullException("options");
130 Configuration.Init(options);
133 // The actual initialization takes place in the
134 // platform-specific factory constructors.
135 toolkit = new Toolkit(new Factory());
141 internal static ToolkitOptions Options { get; private set; }
144 /// Disposes of the resources consumed by this instance.
146 public void Dispose()
149 GC.SuppressFinalize(this);
152 private void Dispose(bool manual)
160 platform_factory.Dispose();
161 platform_factory = null;
171 /// Finalizes this instance.
175 Debug.Print("[Warning] {0} leaked, did you forget to call Dispose()?");
176 // We may not Dispose() the toolkit from the finalizer thread,
177 // as that will crash on many operating systems.