Skip to content

Commit

Permalink
Monitor video modes and gamma correction (#114)
Browse files Browse the repository at this point in the history
* Start implementing monitor video modes

* Move MonitorPlayground to dotnet 3.0

Rider's built-in dotnet SDK only supports 3.0 right now

* Add Gamma

* Add GetAllVideoModes

* Review changes
  • Loading branch information
devvoid authored Feb 3, 2020
1 parent 6278016 commit a8778a5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/Lab/MonitorPlayground/MonitorPlayground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
64 changes: 47 additions & 17 deletions src/Windowing/Silk.NET.Windowing.Common/Interfaces/IMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
// This file is part of Silk.NET.
//
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System.Drawing;

namespace Silk.NET.Windowing.Common
{
public interface IMonitor : IWindowHost
{
string Name { get; }
int Index { get; }
Rectangle Bounds { get; }
VideoMode VideoMode { get; }
}
}
// This file is part of Silk.NET.
//
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System.Collections.Generic;
using System.Drawing;

namespace Silk.NET.Windowing.Common
{
/// <summary>
/// An interface representing a monitor.
/// </summary>
public interface IMonitor : IWindowHost
{
/// <summary>
/// The name of this monitor.
/// </summary>
string Name { get; }

/// <summary>
/// The index of this monitor.
/// </summary>
int Index { get; }

/// <summary>
/// The bounds of this monitor.
/// </summary>
Rectangle Bounds { get; }

/// <summary>
/// The current video mode of this monitor.
/// </summary>
VideoMode VideoMode { get; }

/// <summary>
/// This monitor's gamma correction.
/// </summary>
float Gamma { get; set; }

/// <summary>
/// Get all video modes that this monitor supports.
/// </summary>
/// <returns>An array of all video modes.</returns>
IEnumerable<VideoMode> GetAllVideoModes();
}
}
144 changes: 85 additions & 59 deletions src/Windowing/Silk.NET.Windowing.Desktop/GlfwMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,85 @@
// This file is part of Silk.NET.
//
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System;
using System.Drawing;
using Silk.NET.GLFW;
using Silk.NET.Windowing.Common;
using VideoMode = Silk.NET.Windowing.Common.VideoMode;

namespace Silk.NET.Windowing.Desktop
{
internal unsafe class GlfwMonitor : IMonitor
{
public Monitor* Handle { get; }

public GlfwMonitor(Monitor* monitor, int index)
{
Handle = monitor;
Index = index;
}

public IWindow CreateWindow(WindowOptions opts)
{
if (opts.WindowState == WindowState.Fullscreen)
{
return new GlfwWindow(opts, null, this);
}

opts.Position = new Point(opts.Position.X + Bounds.X, opts.Position.Y + Bounds.Y);
return new GlfwWindow(opts, null, null);
}

public string Name => GlfwProvider.GLFW.Value.GetMonitorName(Handle);
public int Index { get; }

public Rectangle Bounds
{
get
{
GlfwProvider.GLFW.Value.GetMonitorWorkarea(Handle, out var x, out var y, out var w, out var h);
return new Rectangle(x, y, w, h);
}
}

public VideoMode VideoMode
{
get
{
var videoMode = GlfwProvider.GLFW.Value.GetVideoMode(Handle);
return new VideoMode(
new Size(videoMode->Width, videoMode->Height),
videoMode->RefreshRate
);
}
}
}
}
// This file is part of Silk.NET.
//
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System.Collections.Generic;
using System.Drawing;
using Silk.NET.GLFW;
using Silk.NET.Windowing.Common;
using VideoMode = Silk.NET.Windowing.Common.VideoMode;

namespace Silk.NET.Windowing.Desktop
{
internal unsafe class GlfwMonitor : IMonitor
{
private float _gamma = 1.0f;

public Monitor* Handle { get; }

public GlfwMonitor(Monitor* monitor, int index)
{
Handle = monitor;
Index = index;
}

public IWindow CreateWindow(WindowOptions opts)
{
if (opts.WindowState == WindowState.Fullscreen)
{
return new GlfwWindow(opts, null, this);
}

opts.Position = new Point(opts.Position.X + Bounds.X, opts.Position.Y + Bounds.Y);
return new GlfwWindow(opts, null, null);
}

public string Name => GlfwProvider.GLFW.Value.GetMonitorName(Handle);
public int Index { get; }

public Rectangle Bounds
{
get
{
GlfwProvider.GLFW.Value.GetMonitorWorkarea(Handle, out var x, out var y, out var w, out var h);
return new Rectangle(x, y, w, h);
}
}

public VideoMode VideoMode
{
get
{
var videoMode = GlfwProvider.GLFW.Value.GetVideoMode(Handle);
return new VideoMode(
new Size(videoMode->Width, videoMode->Height),
videoMode->RefreshRate
);
}
}

public float Gamma
{
get => _gamma;
set
{
_gamma = value;
GlfwProvider.GLFW.Value.SetGamma(Handle, value);
}
}

public IEnumerable<VideoMode> GetAllVideoModes()
{
var rawVideoModes = GlfwProvider.GLFW.Value.GetVideoModes(Handle, out var count);

var videoModes = new List<VideoMode>();

for (var i = 0; i < count; i++)
{
videoModes.Add(new VideoMode(new Size(rawVideoModes[i].Width, rawVideoModes[i].Height), rawVideoModes[i].RefreshRate));
}

return videoModes;
}
}
}
17 changes: 4 additions & 13 deletions src/Windowing/Silk.NET.Windowing.Desktop/GlfwWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,7 @@ public bool IsVisible
public bool ShouldSwapAutomatically { get; }

/// <inheritdoc />
public unsafe VideoMode VideoMode
{
get
{
var monitor = Monitor;
return monitor != null
? monitor.VideoMode
: _initialOptions.VideoMode;
}
}
public VideoMode VideoMode => Monitor?.VideoMode ?? _initialOptions.VideoMode;

/// <inheritdoc />
public int? PreferredDepthBufferBits => _initialOptions.PreferredDepthBufferBits;
Expand All @@ -193,7 +184,7 @@ public Point Position
}
}

/// <inheritdoc />
/// <inheritdoc cref="Size" />
public Size Size
{
get => _size;
Expand Down Expand Up @@ -315,8 +306,8 @@ public WindowState WindowState
_glfw.SetWindowMonitor
(
_windowPtr, monitor, 0, 0,
resolution.HasValue ? resolution.Value.Width : mode->Width,
resolution.HasValue ? resolution.Value.Height : mode->Height,
resolution?.Width ?? mode->Width,
resolution?.Height ?? mode->Height,
videoMode.RefreshRate ?? mode->RefreshRate
);
break;
Expand Down

0 comments on commit a8778a5

Please sign in to comment.