Skip to content

Commit 5c2dd4f

Browse files
authored
Merge pull request opentk#1770 from NogginBops/fullscreen-api
Update fullscreen/monitor API to be more reasonable.
2 parents 84f49ca + e3d6846 commit 5c2dd4f

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

src/OpenTK.Windowing.Desktop/NativeWindow.cs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
using System.Runtime.ExceptionServices;
1010
using System.Runtime.InteropServices;
1111
using System.Threading;
12-
using System.Threading.Tasks;
1312
using OpenTK.Core;
1413
using OpenTK.Mathematics;
1514
using OpenTK.Windowing.Common;
1615
using OpenTK.Windowing.Common.Input;
1716
using OpenTK.Windowing.GraphicsLibraryFramework;
1817

18+
using Monitor = OpenTK.Windowing.GraphicsLibraryFramework.Monitor;
19+
1920
namespace OpenTK.Windowing.Desktop
2021
{
2122
/// <summary>
@@ -275,31 +276,19 @@ public string Title
275276
/// </summary>
276277
public IGLFWGraphicsContext Context { get; }
277278

278-
private MonitorHandle _currentMonitor;
279-
280279
/// <summary>
281-
/// Gets or sets the current <see cref="MonitorHandle"/>.
280+
/// Gets the current <see cref="MonitorInfo"/> of the monitor that the window is currently on.
281+
/// To make the window fullscreen use <see cref="MakeFullscreen(MonitorHandle, int?, int?, int?)"/> or <see cref="WindowState"/>.
282282
/// </summary>
283-
public unsafe MonitorHandle CurrentMonitor
283+
/// <seealso cref="MakeFullscreen(MonitorHandle, int?, int?, int?)"/>
284+
/// <seealso cref="WindowState"/>
285+
public unsafe MonitorInfo CurrentMonitor
284286
{
285-
get => _currentMonitor;
287+
get => Monitors.GetMonitorFromWindow(WindowPtr);
286288

289+
[Obsolete("Use MakeFullscreen to set the current monitor. Setting this property does nothing anymore.")]
287290
set
288291
{
289-
GraphicsLibraryFramework.Monitor* monitor = value.ToUnsafePtr<GraphicsLibraryFramework.Monitor>();
290-
VideoMode* mode = GLFW.GetVideoMode(monitor);
291-
Vector2i location = ClientLocation;
292-
Vector2i size = ClientSize;
293-
GLFW.SetWindowMonitor(
294-
WindowPtr,
295-
monitor,
296-
location.X,
297-
location.Y,
298-
size.X,
299-
size.Y,
300-
mode->RefreshRate);
301-
302-
_currentMonitor = value;
303292
}
304293
}
305294

@@ -373,13 +362,13 @@ public unsafe WindowState WindowState
373362
// Set the new window state before any potential callback is called,
374363
// so that the new state is available in for example OnResize.
375364
// - Noggin_bops 2023-09-25
376-
var previousWindowState = _windowState;
365+
WindowState previousWindowState = _windowState;
377366
_windowState = value;
378367

379368
if (previousWindowState == WindowState.Fullscreen && value != WindowState.Fullscreen)
380369
{
381370
// We are going from fullscreen to something else.
382-
GLFW.SetWindowMonitor(WindowPtr, null, _cachedWindowLocation.X, _cachedWindowLocation.Y, _cachedWindowClientSize.X, _cachedWindowClientSize.Y, 0);
371+
GLFW.SetWindowMonitor(WindowPtr, null, _cachedWindowLocation.X, _cachedWindowLocation.Y, _cachedWindowClientSize.X, _cachedWindowClientSize.Y, GLFW.DontCare);
383372
}
384373

385374
switch (value)
@@ -397,10 +386,13 @@ public unsafe WindowState WindowState
397386
break;
398387

399388
case WindowState.Fullscreen:
400-
_cachedWindowClientSize = ClientSize;
401-
_cachedWindowLocation = ClientLocation;
402-
var monitor = CurrentMonitor.ToUnsafePtr<GraphicsLibraryFramework.Monitor>();
403-
var modePtr = GLFW.GetVideoMode(monitor);
389+
if (previousWindowState != WindowState.Fullscreen)
390+
{
391+
_cachedWindowClientSize = ClientSize;
392+
_cachedWindowLocation = ClientLocation;
393+
}
394+
Monitor* monitor = CurrentMonitor.Handle.ToUnsafePtr<Monitor>();
395+
VideoMode* modePtr = GLFW.GetVideoMode(monitor);
404396
GLFW.SetWindowMonitor(WindowPtr, monitor, 0, 0, modePtr->Width, modePtr->Height, modePtr->RefreshRate);
405397
break;
406398
}
@@ -418,7 +410,7 @@ public unsafe WindowBorder WindowBorder
418410

419411
set
420412
{
421-
GLFW.GetVersion(out var major, out var minor, out _);
413+
GLFW.GetVersion(out int major, out int minor, out _);
422414

423415
// It isn't possible to implement this in versions of GLFW older than 3.3,
424416
// as SetWindowAttrib didn't exist before then.
@@ -685,7 +677,7 @@ public MouseCursor Cursor
685677

686678
unsafe
687679
{
688-
var oldCursor = _glfwCursor;
680+
Cursor* oldCursor = _glfwCursor;
689681
_glfwCursor = null;
690682

691683
// Create the new GLFW cursor
@@ -694,7 +686,7 @@ public MouseCursor Cursor
694686
// User provided mouse cursor.
695687
fixed (byte* ptr = value.Data)
696688
{
697-
var cursorImg = new GraphicsLibraryFramework.Image(value.Width, value.Height, ptr);
689+
GraphicsLibraryFramework.Image cursorImg = new GraphicsLibraryFramework.Image(value.Width, value.Height, ptr);
698690
_glfwCursor = GLFW.CreateCursor(cursorImg, value.X, value.Y);
699691
}
700692
}
@@ -811,8 +803,6 @@ public unsafe NativeWindow(NativeWindowSettings settings)
811803

812804
_title = settings.Title;
813805

814-
_currentMonitor = settings.CurrentMonitor;
815-
816806
switch (settings.WindowBorder)
817807
{
818808
case WindowBorder.Hidden:
@@ -828,7 +818,7 @@ public unsafe NativeWindow(NativeWindowSettings settings)
828818
break;
829819
}
830820

831-
var isOpenGl = false;
821+
bool isOpenGl = false;
832822
API = settings.API;
833823
switch (settings.API)
834824
{
@@ -2130,5 +2120,29 @@ public void CenterWindow(Vector2i newSize)
21302120
// Actually move the window.
21312121
ClientRectangle = new Box2i(x, y, x + newSize.X, y + newSize.Y);
21322122
}
2123+
2124+
/// <summary>
2125+
/// Make the window fullscreen with the specified resolution and refresh rate.
2126+
/// This function is meant to provide greater control than <see cref="WindowState"/> and <see cref="CurrentMonitor"/> when making the window fullscreen.
2127+
/// </summary>
2128+
/// <param name="monitor">The monitor on which to make the window fullscreen.</param>
2129+
/// <param name="horizontalResolution">The horizontal resolution to switch the screen to, or <see langword="null"/> to use the monitor resolution.</param>
2130+
/// <param name="verticalResolution">The vertical resoltion to switch the screen to, or <see langword="null"/> to use the monitor resolution.</param>
2131+
/// <param name="refreshRate">The refresh rate to use, or <see langword="null"/> to not change refresh rate.</param>
2132+
/// <seealso cref="CurrentMonitor"/>
2133+
/// <seealso cref="WindowState"/>
2134+
public unsafe void MakeFullscreen(MonitorHandle monitor, int? horizontalResolution = null, int? verticalResolution = null, int? refreshRate = null)
2135+
{
2136+
if (_windowState != WindowState.Fullscreen)
2137+
{
2138+
_cachedWindowClientSize = ClientSize;
2139+
_cachedWindowLocation = ClientLocation;
2140+
}
2141+
_windowState = WindowState.Fullscreen;
2142+
Monitor* monitorPtr = monitor.ToUnsafePtr<Monitor>();
2143+
GLFW.GetMonitorPos(monitorPtr, out int x, out int y);
2144+
VideoMode* mode = GLFW.GetVideoMode(monitorPtr);
2145+
GLFW.SetWindowMonitor(WindowPtr, monitorPtr, x, y, horizontalResolution ?? mode->Width, verticalResolution ?? mode->Height, refreshRate ?? -1);
2146+
}
21332147
}
21342148
}

src/OpenTK.Windowing.Desktop/NativeWindowSettings.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public NativeWindowSettings()
116116
public Version APIVersion { get; set; } = new Version(3, 3);
117117

118118
/// <summary>
119-
/// Gets or sets the monitor to open the new window on.
119+
/// Gets or sets the monitor to make the window fullscreen on if <see cref="WindowState"/> = <see cref="WindowState.Fullscreen"/> is set.
120120
/// </summary>
121121
public MonitorHandle CurrentMonitor { get; set; }
122122

@@ -136,13 +136,13 @@ public NativeWindowSettings()
136136
public bool StartVisible { get; set; } = true;
137137

138138
/// <summary>
139-
/// Gets or sets the initial value for <see cref="NativeWindow.WindowState"/> on the new window.
140-
/// This setting is ignored if <c><see cref="StartVisible"/> = false</c>.
139+
/// Gets or sets the initial value for <see cref="NativeWindow.WindowState"/> on the new window.
140+
/// This setting is ignored if <c><see cref="StartVisible"/> = false</c>.
141141
/// </summary>
142142
public WindowState WindowState { get; set; } = WindowState.Normal;
143143

144144
/// <summary>
145-
/// Gets or sets the initial value for <see cref="NativeWindow.WindowBorder"/> on the new window.
145+
/// Gets or sets the initial value for <see cref="NativeWindow.WindowBorder"/> on the new window.
146146
/// </summary>
147147
public WindowBorder WindowBorder { get; set; } = WindowBorder.Resizable;
148148

@@ -155,12 +155,12 @@ public NativeWindowSettings()
155155
public Vector2i? Location { get; set; }
156156

157157
/// <summary>
158-
/// Gets or sets the initial size of the contents of the window.
158+
/// Gets or sets the initial size of the contents of the window.
159159
/// </summary>
160160
public Vector2i ClientSize { get; set; } = new Vector2i(640, 360);
161161

162162
/// <summary>
163-
/// Gets or sets the minimum size of the contents of the window.
163+
/// Gets or sets the minimum size of the contents of the window.
164164
/// </summary>
165165
/// <remarks>
166166
/// Set to <c>null</c> to remove the minimum size constraint.
@@ -169,7 +169,7 @@ public NativeWindowSettings()
169169
public Vector2i? MinimumClientSize { get; set; } = null;
170170

171171
/// <summary>
172-
/// Gets or sets the maximum size of the contents of the window.
172+
/// Gets or sets the maximum size of the contents of the window.
173173
/// </summary>
174174
/// <remarks>
175175
/// Set to <c>null</c> to remove the minimum size constraint.
@@ -178,7 +178,7 @@ public NativeWindowSettings()
178178
public Vector2i? MaximumClientSize { get; set; } = null;
179179

180180
/// <summary>
181-
/// Gets or sets the initial size of the contents of the window.
181+
/// Gets or sets the initial size of the contents of the window.
182182
/// </summary>
183183
[Obsolete("Use the " + nameof(ClientSize) + " property to get or set the initial size of the contents of the window.")]
184184
public Vector2i Size
@@ -188,7 +188,7 @@ public Vector2i Size
188188
}
189189

190190
/// <summary>
191-
/// Gets or sets the minimum size of the contents of the window.
191+
/// Gets or sets the minimum size of the contents of the window.
192192
/// </summary>
193193
/// <remarks>
194194
/// Set to <c>null</c> to remove the minimum size constraint.
@@ -202,7 +202,7 @@ public Vector2i? MinimumSize
202202
}
203203

204204
/// <summary>
205-
/// Gets or sets the maximum size of the contents of the window.
205+
/// Gets or sets the maximum size of the contents of the window.
206206
/// </summary>
207207
/// <remarks>
208208
/// Set to <c>null</c> to remove the minimum size constraint.

0 commit comments

Comments
 (0)