Skip to content

Aspect ratio calculation for VideoModes #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"AppVeyor",
"AzurePipelines",
"Bamboo",
"Bitbucket",
"Bitrise",
"GitHubActions",
"GitLab",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
Silk.NET.Windowing.VideoMode.AspectRatioEstimate.get -> Silk.NET.Maths.Vector2D<int>?
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
Silk.NET.Windowing.VideoMode.AspectRatioEstimate.get -> Silk.NET.Maths.Vector2D<int>?
34 changes: 30 additions & 4 deletions src/Windowing/Silk.NET.Windowing.Common/Structs/VideoMode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Silk.NET.Maths;

namespace Silk.NET.Windowing
Expand All @@ -11,12 +12,11 @@ public VideoMode(Vector2D<int>? resolution = null, int? refreshRate = null)
{
Resolution = resolution;
RefreshRate = refreshRate;
AspectRatioEstimate = resolution != null ? CalculateAspectRatio(resolution.Value) : null;
}

public VideoMode(int refreshRate)
: this(null, refreshRate)
{
}
: this(null, refreshRate) { }

/// <summary>
/// Resolution of the full screen window.
Expand All @@ -28,9 +28,35 @@ public VideoMode(int refreshRate)
/// </summary>
public int? RefreshRate { get; }

/// <summary>
/// Aspect ratio of the full screen window. This value will not be 100% accurate to the actual value, however
/// is more accurate to what a user may expect the ratio to be (for example a resolution of 1366x768 will appear
/// as 16:9, even though it is not quite 16:9 in reality).
/// </summary>
public Vector2D<int>? AspectRatioEstimate { get; }

/// <summary>
/// The default video mode. This uses the window size for resolution and doesn't care about other values.
/// </summary>
public static VideoMode Default => new VideoMode();

private static Vector2D<int> CalculateAspectRatio(Vector2D<int> res)
{
// Calculate the width-height ratio.
float ratio = res.X / (float) res.Y;

// Count up until the lowest value as the aspect ratio cannot be higher than the lowest value.
int lowestValue = res.X < res.Y ? res.X : res.Y;
for (int i = 1; i < lowestValue; i++)
{
// Multiply both together and calculate a good enough value, a bias of 0.1 seems to work well.
float multiplied = ratio * i;
if (multiplied - (int) multiplied < 0.1f)
return new Vector2D<int>((int) multiplied, i);
}

return res;
}

}
}
}