From 94c3c24bfbabc971367c335aa280cb78a4c86ec5 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Mon, 6 Jan 2014 14:49:05 +0100 Subject: [PATCH] [OpenTK] Platform backends should inherit from PlatformFactoryBase This reduces code duplication wrt deprecated interfaces and common support code between backends. --- Source/OpenTK/Platform/Factory.cs | 75 ++++++-------------- Source/OpenTK/Platform/IPlatformFactory.cs | 2 + Source/OpenTK/Platform/MacOS/MacOSFactory.cs | 55 ++++---------- Source/OpenTK/Platform/SDL2/Sdl2Factory.cs | 52 ++++---------- Source/OpenTK/Platform/Windows/WinFactory.cs | 48 ++++--------- Source/OpenTK/Platform/X11/X11Factory.cs | 58 +++------------ 6 files changed, 73 insertions(+), 217 deletions(-) diff --git a/Source/OpenTK/Platform/Factory.cs b/Source/OpenTK/Platform/Factory.cs index e49eb36b..571deb44 100644 --- a/Source/OpenTK/Platform/Factory.cs +++ b/Source/OpenTK/Platform/Factory.cs @@ -33,6 +33,7 @@ using System.Text; namespace OpenTK.Platform { using Graphics; + using Input; sealed class Factory : IPlatformFactory { @@ -134,27 +135,32 @@ namespace OpenTK.Platform return default_implementation.CreateGraphicsMode(); } - public OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() + public IKeyboardDriver2 CreateKeyboardDriver() { return default_implementation.CreateKeyboardDriver(); } - public OpenTK.Input.IMouseDriver2 CreateMouseDriver() + public IMouseDriver2 CreateMouseDriver() { return default_implementation.CreateMouseDriver(); } - public OpenTK.Input.IGamePadDriver CreateGamePadDriver() + public IGamePadDriver CreateGamePadDriver() { return default_implementation.CreateGamePadDriver(); } - public Input.IJoystickDriver2 CreateJoystickDriver() + public IJoystickDriver2 CreateJoystickDriver() { return default_implementation.CreateJoystickDriver(); } - class UnsupportedPlatform : IPlatformFactory + public IJoystickDriver CreateLegacyJoystickDriver() + { + return default_implementation.CreateLegacyJoystickDriver(); + } + + class UnsupportedPlatform : PlatformFactoryBase { #region Fields @@ -165,92 +171,51 @@ namespace OpenTK.Platform #region IPlatformFactory Members - public INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) + public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { throw new PlatformNotSupportedException(error_string); } - public IDisplayDeviceDriver CreateDisplayDeviceDriver() + public override IDisplayDeviceDriver CreateDisplayDeviceDriver() { throw new PlatformNotSupportedException(error_string); } - public IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { throw new PlatformNotSupportedException(error_string); } - public IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { throw new PlatformNotSupportedException(error_string); } - public IGraphicsContext CreateESContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, int major, int minor, GraphicsContextFlags flags) + public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() { throw new PlatformNotSupportedException(error_string); } - public GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() + public override IGraphicsMode CreateGraphicsMode() { throw new PlatformNotSupportedException(error_string); } - public IGraphicsMode CreateGraphicsMode() + public override IKeyboardDriver2 CreateKeyboardDriver() { throw new PlatformNotSupportedException(error_string); } - public OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() + public override IMouseDriver2 CreateMouseDriver() { throw new PlatformNotSupportedException(error_string); } - public OpenTK.Input.IMouseDriver2 CreateMouseDriver() + public override IJoystickDriver2 CreateJoystickDriver() { throw new PlatformNotSupportedException(error_string); } - public OpenTK.Input.IGamePadDriver CreateGamePadDriver() - { - throw new PlatformNotSupportedException(error_string); - } - - public Input.IJoystickDriver2 CreateJoystickDriver() - { - throw new PlatformNotSupportedException(error_string); - } - - #endregion - - #region IDisposable Members - - void Dispose(bool manual) - { - if (!disposed) - { - if (manual) - { - // nothing to do - } - else - { - Debug.Print("{0} leaked, did you forget to call Dispose()?", GetType()); - } - disposed = true; - } - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~UnsupportedPlatform() - { - Dispose(false); - } - #endregion } diff --git a/Source/OpenTK/Platform/IPlatformFactory.cs b/Source/OpenTK/Platform/IPlatformFactory.cs index c59654f3..c5abb9c5 100644 --- a/Source/OpenTK/Platform/IPlatformFactory.cs +++ b/Source/OpenTK/Platform/IPlatformFactory.cs @@ -54,5 +54,7 @@ namespace OpenTK.Platform OpenTK.Input.IGamePadDriver CreateGamePadDriver(); Input.IJoystickDriver2 CreateJoystickDriver(); + + Input.IJoystickDriver CreateLegacyJoystickDriver(); } } diff --git a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs index 697f2352..8a72db56 100644 --- a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs +++ b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs @@ -35,38 +35,33 @@ namespace OpenTK.Platform.MacOS { using Graphics; - class MacOSFactory : IPlatformFactory + class MacOSFactory : PlatformFactoryBase { - #region Fields - - bool disposed; readonly IInputDriver2 InputDriver = new HIDInput(); - #endregion - #region IPlatformFactory Members - public virtual INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) + public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { return new CarbonGLNative(x, y, width, height, title, mode, options, device); } - public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() + public override IDisplayDeviceDriver CreateDisplayDeviceDriver() { return new QuartzDisplayDeviceDriver(); } - public virtual IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { return new AglContext(mode, window, shareContext); } - public virtual IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { return new AglContext(handle, window, shareContext); } - public virtual GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() + public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() { return (GraphicsContext.GetCurrentContextDelegate)delegate { @@ -74,27 +69,17 @@ namespace OpenTK.Platform.MacOS }; } - public virtual IGraphicsMode CreateGraphicsMode() - { - throw new NotSupportedException(); - } - - public virtual OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() + public override IKeyboardDriver2 CreateKeyboardDriver() { return InputDriver.KeyboardDriver; } - public virtual OpenTK.Input.IMouseDriver2 CreateMouseDriver() + public override IMouseDriver2 CreateMouseDriver() { return InputDriver.MouseDriver; } - public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() - { - return InputDriver.GamePadDriver; - } - - public IJoystickDriver2 CreateJoystickDriver() + public override IJoystickDriver2 CreateJoystickDriver() { return InputDriver.JoystickDriver; } @@ -103,33 +88,19 @@ namespace OpenTK.Platform.MacOS #region IDisposable Members - void Dispose(bool manual) + protected override void Dispose(bool manual) { - if (!disposed) + if (!IsDisposed) { if (manual) { InputDriver.Dispose(); } - else - { - Debug.Print("{0} leaked, did you forget to call Dispose()?", GetType()); - } - disposed = true; + + base.Dispose(manual); } } - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~MacOSFactory() - { - Dispose(false); - } - #endregion } } diff --git a/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs b/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs index b8015f6b..25a3c6d9 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs @@ -32,10 +32,9 @@ using OpenTK.Input; namespace OpenTK.Platform.SDL2 { - class Sdl2Factory : IPlatformFactory + class Sdl2Factory : PlatformFactoryBase { readonly Sdl2InputDriver InputDriver = new Sdl2InputDriver(); - bool disposed; /// /// Gets or sets a value indicating whether to use SDL2 fullscreen-desktop mode @@ -56,27 +55,27 @@ namespace OpenTK.Platform.SDL2 #region IPlatformFactory implementation - public INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) + public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { return new Sdl2NativeWindow(x, y, width, height, title, options, device, InputDriver); } - public IDisplayDeviceDriver CreateDisplayDeviceDriver() + public override IDisplayDeviceDriver CreateDisplayDeviceDriver() { return new Sdl2DisplayDeviceDriver(); } - virtual public IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { return new Sdl2GraphicsContext(mode, window, shareContext, major, minor, flags); } - public IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { throw new NotImplementedException(); } - public GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() + public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() { return (GraphicsContext.GetCurrentContextDelegate)delegate { @@ -84,27 +83,17 @@ namespace OpenTK.Platform.SDL2 }; } - public IGraphicsMode CreateGraphicsMode() - { - return new Sdl2GraphicsMode(); - } - - public IKeyboardDriver2 CreateKeyboardDriver() + public override IKeyboardDriver2 CreateKeyboardDriver() { return InputDriver.KeyboardDriver; } - public IMouseDriver2 CreateMouseDriver() + public override IMouseDriver2 CreateMouseDriver() { return InputDriver.MouseDriver; } - public IGamePadDriver CreateGamePadDriver() - { - return InputDriver.GamePadDriver; - } - - public IJoystickDriver2 CreateJoystickDriver() + public override IJoystickDriver2 CreateJoystickDriver() { return InputDriver.JoystickDriver; } @@ -113,34 +102,19 @@ namespace OpenTK.Platform.SDL2 #region IDisposable Members - void Dispose(bool manual) + protected override void Dispose(bool manual) { - if (!disposed) + if (!IsDisposed) { if (manual) { - Debug.Print("Disposing {0}", GetType()); InputDriver.Dispose(); } - else - { - Debug.WriteLine("Sdl2Factory leaked, did you forget to call Dispose()?"); - } - disposed = true; + + base.Dispose(manual); } } - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~Sdl2Factory() - { - Dispose(false); - } - #endregion } } diff --git a/Source/OpenTK/Platform/Windows/WinFactory.cs b/Source/OpenTK/Platform/Windows/WinFactory.cs index 4fa46e2e..877377ab 100644 --- a/Source/OpenTK/Platform/Windows/WinFactory.cs +++ b/Source/OpenTK/Platform/Windows/WinFactory.cs @@ -37,9 +37,8 @@ using OpenTK.Input; namespace OpenTK.Platform.Windows { - class WinFactory : IPlatformFactory + class WinFactory : PlatformFactoryBase { - bool disposed; readonly object SyncRoot = new object(); IInputDriver2 inputDriver; @@ -83,27 +82,27 @@ namespace OpenTK.Platform.Windows #region IPlatformFactory Members - public virtual INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) + public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { return new WinGLNative(x, y, width, height, title, options, device); } - public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() + public override IDisplayDeviceDriver CreateDisplayDeviceDriver() { return new WinDisplayDeviceDriver(); } - public virtual IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { return new WinGLContext(mode, (WinWindowInfo)window, shareContext, major, minor, flags); } - public virtual IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { return new WinGLContext(handle, (WinWindowInfo)window, shareContext, major, minor, flags); } - public virtual GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() + public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() { return (GraphicsContext.GetCurrentContextDelegate)delegate { @@ -111,27 +110,22 @@ namespace OpenTK.Platform.Windows }; } - public virtual IGraphicsMode CreateGraphicsMode() - { - throw new NotSupportedException(); - } - - public virtual OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() + public override OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() { return InputDriver.KeyboardDriver; } - public virtual OpenTK.Input.IMouseDriver2 CreateMouseDriver() + public override OpenTK.Input.IMouseDriver2 CreateMouseDriver() { return InputDriver.MouseDriver; } - public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() + public override OpenTK.Input.IGamePadDriver CreateGamePadDriver() { return InputDriver.GamePadDriver; } - public IJoystickDriver2 CreateJoystickDriver() + public override IJoystickDriver2 CreateJoystickDriver() { return InputDriver.JoystickDriver; } @@ -155,33 +149,19 @@ namespace OpenTK.Platform.Windows #region IDisposable Members - void Dispose(bool manual) + protected override void Dispose(bool manual) { - if (!disposed) + if (!IsDisposed) { if (manual) { InputDriver.Dispose(); } - else - { - Debug.Print("{0} leaked, did you forget to call Dispose()?", GetType()); - } - disposed = true; + + base.Dispose(manual); } } - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~WinFactory() - { - Dispose(false); - } - #endregion } } diff --git a/Source/OpenTK/Platform/X11/X11Factory.cs b/Source/OpenTK/Platform/X11/X11Factory.cs index 0fb80058..985f21cb 100644 --- a/Source/OpenTK/Platform/X11/X11Factory.cs +++ b/Source/OpenTK/Platform/X11/X11Factory.cs @@ -28,10 +28,11 @@ using System; using System.Diagnostics; using OpenTK.Graphics; +using OpenTK.Input; namespace OpenTK.Platform.X11 { - class X11Factory : IPlatformFactory + class X11Factory : PlatformFactoryBase { bool disposed; @@ -47,27 +48,27 @@ namespace OpenTK.Platform.X11 #region IPlatformFactory Members - public virtual INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) + public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { return new X11GLNative(x, y, width, height, title, mode, options, device); } - public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() + public override IDisplayDeviceDriver CreateDisplayDeviceDriver() { return new X11DisplayDevice(); } - public virtual IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { return new X11GLContext(mode, window, shareContext, directRendering, major, minor, flags); } - public virtual IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) + public override IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags) { return new X11GLContext(handle, window, shareContext, directRendering, major, minor, flags); } - public virtual GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() + public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() { return (GraphicsContext.GetCurrentContextDelegate)delegate { @@ -75,17 +76,17 @@ namespace OpenTK.Platform.X11 }; } - public virtual IGraphicsMode CreateGraphicsMode() + public override IGraphicsMode CreateGraphicsMode() { throw new NotSupportedException(); } - public virtual OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() + public override IKeyboardDriver2 CreateKeyboardDriver() { return new X11Keyboard(); } - public virtual OpenTK.Input.IMouseDriver2 CreateMouseDriver() + public override IMouseDriver2 CreateMouseDriver() { if (XI2Mouse.IsSupported(IntPtr.Zero)) return new XI2Mouse(); // Requires xorg 1.7 or higher. @@ -93,48 +94,11 @@ namespace OpenTK.Platform.X11 return new X11Mouse(); // Always supported. } - public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() + public override IJoystickDriver2 CreateJoystickDriver() { return new X11Joystick(); } - public virtual OpenTK.Input.IJoystickDriver2 CreateJoystickDriver() - { - return new X11Joystick(); - } - - - #endregion - - #region IDisposable Members - - void Dispose(bool manual) - { - if (!disposed) - { - if (manual) - { - // nothing to do - } - else - { - Debug.Print("{0} leaked, did you forget to call Dispose()?", GetType()); - } - disposed = true; - } - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~X11Factory() - { - Dispose(false); - } - #endregion } }