Files
sbox-public/engine/Tests/Sandbox.Test.Unit/ScreenTest.cs
Sam Pavlovic d8d0f9b67f Guard Aspect against NaN and build CameraComponent FoV from current camera rect instead of screen's (#5512)
* Guard Aspect against NaN and build CameraComponent FoV from current camera rect instead of screen's

Getting screen's aspect is actually wrong if we have more than one camera in play (eg splitscreen)

Screen.Aspect not being set until playing is our intended behaviour for what I remember

Fixes https://github.com/Facepunch/sbox/issues/5319

* Add tests and update comment
2026-07-30 14:56:59 +00:00

34 lines
839 B
C#

namespace EngineTests;
[TestClass]
public class ScreenTest
{
[TestMethod]
public void AspectIsFiniteBeforeScreenSizeIsKnown()
{
var oldSize = Screen.Size;
try
{
// Before first play the swapchain reports (0, 0) — https://github.com/Facepunch/sbox/issues/5319
Screen.Size = new Vector2( 0, 0 );
Assert.AreEqual( 1f, Screen.Aspect );
Assert.IsTrue( float.IsFinite( Screen.CreateVerticalFieldOfView( 90f ) ) );
Screen.Size = new Vector2( 1920, 1080 );
Assert.AreEqual( 1920f / 1080f, Screen.Aspect );
}
finally
{
Screen.Size = oldSize;
}
}
[TestMethod]
public void CreateVerticalFieldOfViewWithExplicitAspect()
{
// 90° through the inverse 16:9 aspect gives the classic 58.72° vertical fov
Assert.AreEqual( 58.7155f, Screen.CreateVerticalFieldOfView( 90f, 1080f / 1920f ), 0.001f );
}
}