using System.Drawing; using OpenTK.Mathematics; using OpenTK.Platform; using TK = OpenTK.Platform.Toolkit; namespace Dashboard.OpenTK.PAL2 { public class PhysicalWindow : IPhysicalWindow { public WindowHandle WindowHandle { get; } public IDeviceContext DeviceContext { get; } public bool DoubleBuffered => true; // Always true for OpenTK windows. public string Title { get => TK.Window.GetTitle(WindowHandle); set => TK.Window.SetTitle(WindowHandle, value); } public SizeF OuterSize { get { TK.Window.GetSize(WindowHandle, out Vector2i size); return new SizeF(size.X, size.Y); } set => TK.Window.SetSize(WindowHandle, new Vector2i((int)value.Width, (int)value.Height)); } public SizeF ClientSize { get { TK.Window.GetClientSize(WindowHandle, out Vector2i size); return new SizeF(size.X, size.Y); } set => TK.Window.SetClientSize(WindowHandle, new Vector2i((int)value.Width, (int)value.Height)); } public PhysicalWindow(WindowHandle window, IDeviceContext dc) { WindowHandle = window; DeviceContext = dc; } public PhysicalWindow(WindowHandle window, OpenGLContextHandle context, ISwapGroup? swapGroup = null) { WindowHandle = window; DeviceContext = new OpenGLDeviceContext(window, context, swapGroup); } public PhysicalWindow(GraphicsApiHints hints) { WindowHandle = TK.Window.Create(hints); DeviceContext = CreateDeviceContext(WindowHandle, hints); } private static IDeviceContext CreateDeviceContext(WindowHandle window, GraphicsApiHints hints) { switch (hints.Api) { case GraphicsApi.OpenGL: case GraphicsApi.OpenGLES: OpenGLContextHandle context = TK.OpenGL.CreateFromWindow(window); return new OpenGLDeviceContext(window, context); default: throw new Exception($"Unknown graphics API {hints.Api}."); } } private bool _isDisposed = false; public void Dispose() { if (_isDisposed) return; _isDisposed = true; (DeviceContext as IDisposable)?.Dispose(); TK.Window.Destroy(WindowHandle); } } }