Skip to content

Commit 5be866f

Browse files
authored
Approximate aspect ratio calculation on VideoMode (#982)
Note: Ratios that can be simplified like 16:10 will be reported as 8:5, blame monitor manufacturers
1 parent 63304c4 commit 5be866f

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

.nuke/build.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"AppVeyor",
3030
"AzurePipelines",
3131
"Bamboo",
32+
"Bitbucket",
3233
"Bitrise",
3334
"GitHubActions",
3435
"GitLab",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
Silk.NET.Windowing.VideoMode.AspectRatioEstimate.get -> Silk.NET.Maths.Vector2D<int>?
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
Silk.NET.Windowing.VideoMode.AspectRatioEstimate.get -> Silk.NET.Maths.Vector2D<int>?

src/Windowing/Silk.NET.Windowing.Common/Structs/VideoMode.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using Silk.NET.Maths;
56

67
namespace Silk.NET.Windowing
@@ -11,12 +12,11 @@ public VideoMode(Vector2D<int>? resolution = null, int? refreshRate = null)
1112
{
1213
Resolution = resolution;
1314
RefreshRate = refreshRate;
15+
AspectRatioEstimate = resolution != null ? CalculateAspectRatio(resolution.Value) : null;
1416
}
1517

1618
public VideoMode(int refreshRate)
17-
: this(null, refreshRate)
18-
{
19-
}
19+
: this(null, refreshRate) { }
2020

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

31+
/// <summary>
32+
/// Aspect ratio of the full screen window. This value will not be 100% accurate to the actual value, however
33+
/// is more accurate to what a user may expect the ratio to be (for example a resolution of 1366x768 will appear
34+
/// as 16:9, even though it is not quite 16:9 in reality).
35+
/// </summary>
36+
public Vector2D<int>? AspectRatioEstimate { get; }
37+
3138
/// <summary>
3239
/// The default video mode. This uses the window size for resolution and doesn't care about other values.
3340
/// </summary>
3441
public static VideoMode Default => new VideoMode();
42+
43+
private static Vector2D<int> CalculateAspectRatio(Vector2D<int> res)
44+
{
45+
// Calculate the width-height ratio.
46+
float ratio = res.X / (float) res.Y;
47+
48+
// Count up until the lowest value as the aspect ratio cannot be higher than the lowest value.
49+
int lowestValue = res.X < res.Y ? res.X : res.Y;
50+
for (int i = 1; i < lowestValue; i++)
51+
{
52+
// Multiply both together and calculate a good enough value, a bias of 0.1 seems to work well.
53+
float multiplied = ratio * i;
54+
if (multiplied - (int) multiplied < 0.1f)
55+
return new Vector2D<int>((int) multiplied, i);
56+
}
57+
58+
return res;
59+
}
60+
3561
}
36-
}
62+
}

0 commit comments

Comments
 (0)