[OpenTK] Do not crash when no monitor connected

NativeWindow now checks for a non-null device before attempting to
access its properties. Affects
https://github.com/mono/MonoGame/issues/2416
This commit is contained in:
thefiddler 2014-05-01 09:47:55 +02:00
parent bd712e4775
commit c0b0990687
2 changed files with 22 additions and 9 deletions

View file

@ -74,8 +74,8 @@ namespace OpenTK
/// <exception cref="System.ArgumentOutOfRangeException">If width or height is less than 1.</exception> /// <exception cref="System.ArgumentOutOfRangeException">If width or height is less than 1.</exception>
/// <exception cref="System.ArgumentNullException">If mode or device is null.</exception> /// <exception cref="System.ArgumentNullException">If mode or device is null.</exception>
public NativeWindow(int width, int height, string title, GameWindowFlags options, GraphicsMode mode, DisplayDevice device) public NativeWindow(int width, int height, string title, GameWindowFlags options, GraphicsMode mode, DisplayDevice device)
: this(device.Bounds.Left + (device.Bounds.Width - width) / 2, : this(device != null ? device.Bounds.Left + (device.Bounds.Width - width) / 2 : 0,
device.Bounds.Top + (device.Bounds.Height - height) / 2, device != null ? device.Bounds.Top + (device.Bounds.Height - height) / 2 : 0,
width, height, title, options, mode, device) { } width, height, title, options, mode, device) { }
/// <summary>Constructs a new NativeWindow with the specified attributes.</summary> /// <summary>Constructs a new NativeWindow with the specified attributes.</summary>
@ -98,8 +98,6 @@ namespace OpenTK
throw new ArgumentOutOfRangeException("height", "Must be greater than zero."); throw new ArgumentOutOfRangeException("height", "Must be greater than zero.");
if (mode == null) if (mode == null)
throw new ArgumentNullException("mode"); throw new ArgumentNullException("mode");
if (device == null)
throw new ArgumentNullException("device");
this.options = options; this.options = options;
this.device = device; this.device = device;
@ -107,8 +105,11 @@ namespace OpenTK
implementation = Factory.Default.CreateNativeWindow(x, y, width, height, title, mode, options, this.device); implementation = Factory.Default.CreateNativeWindow(x, y, width, height, title, mode, options, this.device);
if ((options & GameWindowFlags.Fullscreen) != 0) if ((options & GameWindowFlags.Fullscreen) != 0)
{
if (this.device != null)
{ {
this.device.ChangeResolution(width, height, mode.ColorFormat.BitsPerPixel, 0); this.device.ChangeResolution(width, height, mode.ColorFormat.BitsPerPixel, 0);
}
WindowState = WindowState.Fullscreen; WindowState = WindowState.Fullscreen;
} }
} }
@ -712,9 +713,11 @@ namespace OpenTK
{ {
if ((options & GameWindowFlags.Fullscreen) != 0) if ((options & GameWindowFlags.Fullscreen) != 0)
{ {
//if (WindowState == WindowState.Fullscreen) WindowState = WindowState.Normal; // TODO: Revise. if (device != null)
{
device.RestoreResolution(); device.RestoreResolution();
} }
}
implementation.Dispose(); implementation.Dispose();
GC.SuppressFinalize(this); GC.SuppressFinalize(this);

View file

@ -572,14 +572,24 @@ namespace OpenTK.Platform.SDL2
public override Point PointToClient(Point point) public override Point PointToClient(Point point)
{ {
var origin = DisplayDevice.Default.Bounds.Location; var origin = Point.Empty;
var display = DisplayDevice.Default;
if (display != null)
{
origin = display.Bounds.Location;
}
var client = Location; var client = Location;
return new Point(point.X + client.X - origin.X, point.Y + client.Y - origin.Y); return new Point(point.X + client.X - origin.X, point.Y + client.Y - origin.Y);
} }
public override Point PointToScreen(Point point) public override Point PointToScreen(Point point)
{ {
var origin = DisplayDevice.Default.Bounds.Location; var origin = Point.Empty;
var display = DisplayDevice.Default;
if (display != null)
{
origin = display.Bounds.Location;
}
var client = Location; var client = Location;
return new Point(point.X + origin.X - client.X, point.Y + origin.Y - client.Y); return new Point(point.X + origin.X - client.X, point.Y + origin.Y - client.Y);
} }