[OpenTK] Platform backends should inherit from PlatformFactoryBase

This reduces code duplication wrt deprecated interfaces and common
support code between backends.
This commit is contained in:
thefiddler 2014-01-06 14:49:05 +01:00
parent d84be0d594
commit 94c3c24bfb
6 changed files with 73 additions and 217 deletions

View file

@ -33,6 +33,7 @@ using System.Text;
namespace OpenTK.Platform namespace OpenTK.Platform
{ {
using Graphics; using Graphics;
using Input;
sealed class Factory : IPlatformFactory sealed class Factory : IPlatformFactory
{ {
@ -134,27 +135,32 @@ namespace OpenTK.Platform
return default_implementation.CreateGraphicsMode(); return default_implementation.CreateGraphicsMode();
} }
public OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() public IKeyboardDriver2 CreateKeyboardDriver()
{ {
return default_implementation.CreateKeyboardDriver(); return default_implementation.CreateKeyboardDriver();
} }
public OpenTK.Input.IMouseDriver2 CreateMouseDriver() public IMouseDriver2 CreateMouseDriver()
{ {
return default_implementation.CreateMouseDriver(); return default_implementation.CreateMouseDriver();
} }
public OpenTK.Input.IGamePadDriver CreateGamePadDriver() public IGamePadDriver CreateGamePadDriver()
{ {
return default_implementation.CreateGamePadDriver(); return default_implementation.CreateGamePadDriver();
} }
public Input.IJoystickDriver2 CreateJoystickDriver() public IJoystickDriver2 CreateJoystickDriver()
{ {
return default_implementation.CreateJoystickDriver(); return default_implementation.CreateJoystickDriver();
} }
class UnsupportedPlatform : IPlatformFactory public IJoystickDriver CreateLegacyJoystickDriver()
{
return default_implementation.CreateLegacyJoystickDriver();
}
class UnsupportedPlatform : PlatformFactoryBase
{ {
#region Fields #region Fields
@ -165,92 +171,51 @@ namespace OpenTK.Platform
#region IPlatformFactory Members #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); throw new PlatformNotSupportedException(error_string);
} }
public IDisplayDeviceDriver CreateDisplayDeviceDriver() public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
{ {
throw new PlatformNotSupportedException(error_string); 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); 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); 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); throw new PlatformNotSupportedException(error_string);
} }
public GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() public override IGraphicsMode CreateGraphicsMode()
{ {
throw new PlatformNotSupportedException(error_string); throw new PlatformNotSupportedException(error_string);
} }
public IGraphicsMode CreateGraphicsMode() public override IKeyboardDriver2 CreateKeyboardDriver()
{ {
throw new PlatformNotSupportedException(error_string); throw new PlatformNotSupportedException(error_string);
} }
public OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() public override IMouseDriver2 CreateMouseDriver()
{ {
throw new PlatformNotSupportedException(error_string); throw new PlatformNotSupportedException(error_string);
} }
public OpenTK.Input.IMouseDriver2 CreateMouseDriver() public override IJoystickDriver2 CreateJoystickDriver()
{ {
throw new PlatformNotSupportedException(error_string); 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 #endregion
} }

View file

@ -54,5 +54,7 @@ namespace OpenTK.Platform
OpenTK.Input.IGamePadDriver CreateGamePadDriver(); OpenTK.Input.IGamePadDriver CreateGamePadDriver();
Input.IJoystickDriver2 CreateJoystickDriver(); Input.IJoystickDriver2 CreateJoystickDriver();
Input.IJoystickDriver CreateLegacyJoystickDriver();
} }
} }

View file

@ -35,38 +35,33 @@ namespace OpenTK.Platform.MacOS
{ {
using Graphics; using Graphics;
class MacOSFactory : IPlatformFactory class MacOSFactory : PlatformFactoryBase
{ {
#region Fields
bool disposed;
readonly IInputDriver2 InputDriver = new HIDInput(); readonly IInputDriver2 InputDriver = new HIDInput();
#endregion
#region IPlatformFactory Members #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); return new CarbonGLNative(x, y, width, height, title, mode, options, device);
} }
public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
{ {
return new QuartzDisplayDeviceDriver(); 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); 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); return new AglContext(handle, window, shareContext);
} }
public virtual GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{ {
return (GraphicsContext.GetCurrentContextDelegate)delegate return (GraphicsContext.GetCurrentContextDelegate)delegate
{ {
@ -74,27 +69,17 @@ namespace OpenTK.Platform.MacOS
}; };
} }
public virtual IGraphicsMode CreateGraphicsMode() public override IKeyboardDriver2 CreateKeyboardDriver()
{
throw new NotSupportedException();
}
public virtual OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver()
{ {
return InputDriver.KeyboardDriver; return InputDriver.KeyboardDriver;
} }
public virtual OpenTK.Input.IMouseDriver2 CreateMouseDriver() public override IMouseDriver2 CreateMouseDriver()
{ {
return InputDriver.MouseDriver; return InputDriver.MouseDriver;
} }
public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() public override IJoystickDriver2 CreateJoystickDriver()
{
return InputDriver.GamePadDriver;
}
public IJoystickDriver2 CreateJoystickDriver()
{ {
return InputDriver.JoystickDriver; return InputDriver.JoystickDriver;
} }
@ -103,33 +88,19 @@ namespace OpenTK.Platform.MacOS
#region IDisposable Members #region IDisposable Members
void Dispose(bool manual) protected override void Dispose(bool manual)
{ {
if (!disposed) if (!IsDisposed)
{ {
if (manual) if (manual)
{ {
InputDriver.Dispose(); InputDriver.Dispose();
} }
else
{ base.Dispose(manual);
Debug.Print("{0} leaked, did you forget to call Dispose()?", GetType());
}
disposed = true;
} }
} }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~MacOSFactory()
{
Dispose(false);
}
#endregion #endregion
} }
} }

View file

@ -32,10 +32,9 @@ using OpenTK.Input;
namespace OpenTK.Platform.SDL2 namespace OpenTK.Platform.SDL2
{ {
class Sdl2Factory : IPlatformFactory class Sdl2Factory : PlatformFactoryBase
{ {
readonly Sdl2InputDriver InputDriver = new Sdl2InputDriver(); readonly Sdl2InputDriver InputDriver = new Sdl2InputDriver();
bool disposed;
/// <summary> /// <summary>
/// Gets or sets a value indicating whether to use SDL2 fullscreen-desktop mode /// Gets or sets a value indicating whether to use SDL2 fullscreen-desktop mode
@ -56,27 +55,27 @@ namespace OpenTK.Platform.SDL2
#region IPlatformFactory implementation #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); return new Sdl2NativeWindow(x, y, width, height, title, options, device, InputDriver);
} }
public IDisplayDeviceDriver CreateDisplayDeviceDriver() public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
{ {
return new Sdl2DisplayDeviceDriver(); 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); 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(); throw new NotImplementedException();
} }
public GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{ {
return (GraphicsContext.GetCurrentContextDelegate)delegate return (GraphicsContext.GetCurrentContextDelegate)delegate
{ {
@ -84,27 +83,17 @@ namespace OpenTK.Platform.SDL2
}; };
} }
public IGraphicsMode CreateGraphicsMode() public override IKeyboardDriver2 CreateKeyboardDriver()
{
return new Sdl2GraphicsMode();
}
public IKeyboardDriver2 CreateKeyboardDriver()
{ {
return InputDriver.KeyboardDriver; return InputDriver.KeyboardDriver;
} }
public IMouseDriver2 CreateMouseDriver() public override IMouseDriver2 CreateMouseDriver()
{ {
return InputDriver.MouseDriver; return InputDriver.MouseDriver;
} }
public IGamePadDriver CreateGamePadDriver() public override IJoystickDriver2 CreateJoystickDriver()
{
return InputDriver.GamePadDriver;
}
public IJoystickDriver2 CreateJoystickDriver()
{ {
return InputDriver.JoystickDriver; return InputDriver.JoystickDriver;
} }
@ -113,34 +102,19 @@ namespace OpenTK.Platform.SDL2
#region IDisposable Members #region IDisposable Members
void Dispose(bool manual) protected override void Dispose(bool manual)
{ {
if (!disposed) if (!IsDisposed)
{ {
if (manual) if (manual)
{ {
Debug.Print("Disposing {0}", GetType());
InputDriver.Dispose(); InputDriver.Dispose();
} }
else
{ base.Dispose(manual);
Debug.WriteLine("Sdl2Factory leaked, did you forget to call Dispose()?");
}
disposed = true;
} }
} }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~Sdl2Factory()
{
Dispose(false);
}
#endregion #endregion
} }
} }

View file

@ -37,9 +37,8 @@ using OpenTK.Input;
namespace OpenTK.Platform.Windows namespace OpenTK.Platform.Windows
{ {
class WinFactory : IPlatformFactory class WinFactory : PlatformFactoryBase
{ {
bool disposed;
readonly object SyncRoot = new object(); readonly object SyncRoot = new object();
IInputDriver2 inputDriver; IInputDriver2 inputDriver;
@ -83,27 +82,27 @@ namespace OpenTK.Platform.Windows
#region IPlatformFactory Members #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); return new WinGLNative(x, y, width, height, title, options, device);
} }
public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
{ {
return new WinDisplayDeviceDriver(); 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); 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); return new WinGLContext(handle, (WinWindowInfo)window, shareContext, major, minor, flags);
} }
public virtual GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{ {
return (GraphicsContext.GetCurrentContextDelegate)delegate return (GraphicsContext.GetCurrentContextDelegate)delegate
{ {
@ -111,27 +110,22 @@ namespace OpenTK.Platform.Windows
}; };
} }
public virtual IGraphicsMode CreateGraphicsMode() public override OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver()
{
throw new NotSupportedException();
}
public virtual OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver()
{ {
return InputDriver.KeyboardDriver; return InputDriver.KeyboardDriver;
} }
public virtual OpenTK.Input.IMouseDriver2 CreateMouseDriver() public override OpenTK.Input.IMouseDriver2 CreateMouseDriver()
{ {
return InputDriver.MouseDriver; return InputDriver.MouseDriver;
} }
public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() public override OpenTK.Input.IGamePadDriver CreateGamePadDriver()
{ {
return InputDriver.GamePadDriver; return InputDriver.GamePadDriver;
} }
public IJoystickDriver2 CreateJoystickDriver() public override IJoystickDriver2 CreateJoystickDriver()
{ {
return InputDriver.JoystickDriver; return InputDriver.JoystickDriver;
} }
@ -155,33 +149,19 @@ namespace OpenTK.Platform.Windows
#region IDisposable Members #region IDisposable Members
void Dispose(bool manual) protected override void Dispose(bool manual)
{ {
if (!disposed) if (!IsDisposed)
{ {
if (manual) if (manual)
{ {
InputDriver.Dispose(); InputDriver.Dispose();
} }
else
{ base.Dispose(manual);
Debug.Print("{0} leaked, did you forget to call Dispose()?", GetType());
}
disposed = true;
} }
} }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~WinFactory()
{
Dispose(false);
}
#endregion #endregion
} }
} }

View file

@ -28,10 +28,11 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using OpenTK.Graphics; using OpenTK.Graphics;
using OpenTK.Input;
namespace OpenTK.Platform.X11 namespace OpenTK.Platform.X11
{ {
class X11Factory : IPlatformFactory class X11Factory : PlatformFactoryBase
{ {
bool disposed; bool disposed;
@ -47,27 +48,27 @@ namespace OpenTK.Platform.X11
#region IPlatformFactory Members #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); return new X11GLNative(x, y, width, height, title, mode, options, device);
} }
public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
{ {
return new X11DisplayDevice(); 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); 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); return new X11GLContext(handle, window, shareContext, directRendering, major, minor, flags);
} }
public virtual GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext() public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{ {
return (GraphicsContext.GetCurrentContextDelegate)delegate return (GraphicsContext.GetCurrentContextDelegate)delegate
{ {
@ -75,17 +76,17 @@ namespace OpenTK.Platform.X11
}; };
} }
public virtual IGraphicsMode CreateGraphicsMode() public override IGraphicsMode CreateGraphicsMode()
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
public virtual OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() public override IKeyboardDriver2 CreateKeyboardDriver()
{ {
return new X11Keyboard(); return new X11Keyboard();
} }
public virtual OpenTK.Input.IMouseDriver2 CreateMouseDriver() public override IMouseDriver2 CreateMouseDriver()
{ {
if (XI2Mouse.IsSupported(IntPtr.Zero)) if (XI2Mouse.IsSupported(IntPtr.Zero))
return new XI2Mouse(); // Requires xorg 1.7 or higher. return new XI2Mouse(); // Requires xorg 1.7 or higher.
@ -93,48 +94,11 @@ namespace OpenTK.Platform.X11
return new X11Mouse(); // Always supported. return new X11Mouse(); // Always supported.
} }
public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() public override IJoystickDriver2 CreateJoystickDriver()
{ {
return new X11Joystick(); 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 #endregion
} }
} }