Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[OpenTK] Do not crash when no monitor connected

NativeWindow now checks for a non-null device before attempting to
access its properties. Affects
mono/MonoGame#2416
  • Loading branch information...
commit c0b0990687ae27fbd06380e875d739996bab2f12 1 parent bd712e4
@thefiddler thefiddler authored
View
17 Source/OpenTK/NativeWindow.cs
@@ -74,8 +74,8 @@ public NativeWindow()
/// <exception cref="System.ArgumentOutOfRangeException">If width or height is less than 1.</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)
- : this(device.Bounds.Left + (device.Bounds.Width - width) / 2,
- device.Bounds.Top + (device.Bounds.Height - height) / 2,
+ : this(device != null ? device.Bounds.Left + (device.Bounds.Width - width) / 2 : 0,
+ device != null ? device.Bounds.Top + (device.Bounds.Height - height) / 2 : 0,
width, height, title, options, mode, device) { }
/// <summary>Constructs a new NativeWindow with the specified attributes.</summary>
@@ -98,8 +98,6 @@ public NativeWindow(int x, int y, int width, int height, string title, GameWindo
throw new ArgumentOutOfRangeException("height", "Must be greater than zero.");
if (mode == null)
throw new ArgumentNullException("mode");
- if (device == null)
- throw new ArgumentNullException("device");
this.options = options;
this.device = device;
@@ -108,7 +106,10 @@ public NativeWindow(int x, int y, int width, int height, string title, GameWindo
if ((options & GameWindowFlags.Fullscreen) != 0)
{
- this.device.ChangeResolution(width, height, mode.ColorFormat.BitsPerPixel, 0);
+ if (this.device != null)
+ {
+ this.device.ChangeResolution(width, height, mode.ColorFormat.BitsPerPixel, 0);
+ }
WindowState = WindowState.Fullscreen;
}
}
@@ -712,8 +713,10 @@ public virtual void Dispose()
{
if ((options & GameWindowFlags.Fullscreen) != 0)
{
- //if (WindowState == WindowState.Fullscreen) WindowState = WindowState.Normal; // TODO: Revise.
- device.RestoreResolution();
+ if (device != null)
+ {
+ device.RestoreResolution();
+ }
}
implementation.Dispose();
GC.SuppressFinalize(this);
View
14 Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs
@@ -572,14 +572,24 @@ public override void ProcessEvents()
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;
return new Point(point.X + client.X - origin.X, point.Y + client.Y - origin.Y);
}
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;
return new Point(point.X + origin.X - client.X, point.Y + origin.Y - client.Y);
}
Please sign in to comment.
Something went wrong with that request. Please try again.