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 ;
45using Silk . NET . Maths ;
56
67namespace 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