diff --git a/StarterNG.Tests/GraphicsSettingsTests.cs b/StarterNG.Tests/GraphicsSettingsTests.cs index 74bbaf7..f63e300 100644 --- a/StarterNG.Tests/GraphicsSettingsTests.cs +++ b/StarterNG.Tests/GraphicsSettingsTests.cs @@ -172,6 +172,84 @@ public class GraphicsSettingsTests Assert.Null(Line(text, "gfx.buffer.percent")); } + // Borderless, the simulator takes the window's size from the monitor (application.cpp) and + // leaves width and height unread; -1 is its own word for a buffer of the window's size + // (opengl33renderer.cpp). + [Fact] + public void A_borderless_screen_draws_into_a_buffer_of_the_windows_own_size() + { + var s = Read("fullscreenwindowed yes\nwidth 1280\nheight 720\n"); + + string text = Write(s); + + Assert.Equal("gfx.framebuffer.width -1", Line(text, "gfx.framebuffer.width")); + Assert.Equal("gfx.framebuffer.height -1", Line(text, "gfx.framebuffer.height")); + Assert.Equal("width 1280", Line(text, "width")); + Assert.Equal(100, Read(text).BufferScalePercent); + } + + [Fact] + public void A_borderless_screen_with_a_smaller_buffer_takes_its_part_of_the_monitor() + { + var s = Read("fullscreenwindowed yes\nfullscreenmonitor HDMI-1:1920,0\nwidth 1280\nheight 720\nstarter.bufferscale 50\n"); + var asked = new List(); + var serializer = new SettingsSerializer(id => + { + asked.Add(id); + return (2560, 1440); + }); + var c = new ConfigFile(); + + serializer.Write(s, c); + + Assert.Equal("1280", c.GetRaw("gfx.framebuffer.width")); + Assert.Equal("720", c.GetRaw("gfx.framebuffer.height")); + Assert.Equal("50", c.GetRaw("starter.bufferscale")); + Assert.Equal(new[] { "HDMI-1:1920,0" }, asked.Distinct()); + var read = new SimulatorSettings(); + serializer.Read(read, c); + Assert.Equal(50, read.BufferScalePercent); + } + + [Fact] + public void A_borderless_screen_on_a_monitor_of_unknown_size_keeps_the_windows_own_buffer() + { + var s = Read("fullscreenwindowed yes\nwidth 1280\nheight 720\nstarter.bufferscale 50\n"); + var c = new ConfigFile(); + + new SettingsSerializer(_ => null).Write(s, c); + + Assert.Equal("-1", c.GetRaw("gfx.framebuffer.width")); + Assert.Equal("-1", c.GetRaw("gfx.framebuffer.height")); + } + + [Fact] + public void A_buffer_of_the_window_left_in_a_borderless_file_goes_on_a_save() + { + // As this starter wrote it before: the size of the resolution row, which borderless does not use. + var installation = WithIni("fullscreenwindowed yes\nwidth 1280\nheight 720\nstarter.bufferscale 100\n" + + "gfx.framebuffer.width 1280\ngfx.framebuffer.height 720\n"); + + installation.SettingsStore.Save(); + + string saved = installation.Files.ReadAllText(UserIni); + Assert.Equal("gfx.framebuffer.width -1", Line(saved, "gfx.framebuffer.width")); + Assert.Equal("gfx.framebuffer.height -1", Line(saved, "gfx.framebuffer.height")); + } + + [Fact] + public void A_buffer_the_file_has_for_a_window_or_a_full_screen_stays_until_changed() + { + var installation = WithIni("fullscreenwindowed no\nwidth 1280\nheight 720\nstarter.bufferscale 100\n" + + "gfx.framebuffer.width 1280\ngfx.framebuffer.height 720\n"); + + installation.SettingsStore.Save(); + + string saved = installation.Files.ReadAllText(UserIni); + Assert.Equal("gfx.framebuffer.width 1280", Line(saved, "gfx.framebuffer.width")); + Assert.Equal("gfx.framebuffer.height 720", Line(saved, "gfx.framebuffer.height")); + } + [Fact] public void A_level_is_recognised_after_it_is_applied_and_survives_a_save() { diff --git a/StarterNG.Tests/MonitorsTests.cs b/StarterNG.Tests/MonitorsTests.cs index 832279f..45c90d6 100644 --- a/StarterNG.Tests/MonitorsTests.cs +++ b/StarterNG.Tests/MonitorsTests.cs @@ -59,6 +59,22 @@ public class MonitorsTests Assert.Equal("HDMI-1:1920,0", c.GetRaw("fullscreenmonitor")); } + [Fact] + public void The_size_is_of_the_monitor_named_or_of_the_primary_one() + { + var monitors = new[] + { + new DisplayMonitor("eDP-1:0,360", "eDP-1", 1920, 1080, true), + new DisplayMonitor("HDMI-1:1920,0", "HDMI-1", 2560, 1440, false) + }; + + Assert.Equal((2560, 1440), Monitors.SizeOf(monitors, "HDMI-1:1920,0")); + Assert.Equal((1920, 1080), Monitors.SizeOf(monitors, "")); + // Not connected: the simulator goes to the primary one. + Assert.Equal((1920, 1080), Monitors.SizeOf(monitors, "DP-3:4480,0")); + Assert.Null(Monitors.SizeOf(Array.Empty(), "HDMI-1:1920,0")); + } + // ── A name outside ASCII: the simulator compares the file's bytes with GLFW's UTF-8 name ── private static readonly string UserIni = Path.Combine("/home/kolejarz", ".config", "MaSzyna", "eu07.ini"); diff --git a/StarterNG/Application/AppServices.cs b/StarterNG/Application/AppServices.cs index 838b163..fdd3d6f 100644 --- a/StarterNG/Application/AppServices.cs +++ b/StarterNG/Application/AppServices.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using StarterNG.Application.Abstractions; using StarterNG.Domain.Settings; @@ -63,7 +64,10 @@ public sealed class AppServices SceneryFiles = new SceneryFileWatch(files, Sceneries, Library, State); Executables = new ExecutableLocator(files, paths, environment, log); - SettingsStore = new SettingsStore(files, clock, log, settingsPaths, new SettingsSerializer(), Executables); + // Read when a borderless screen with a smaller buffer is first written, and once. + var monitors = new Lazy>(() => Monitors.List(environment)); + SettingsStore = new SettingsStore(files, clock, log, settingsPaths, + new SettingsSerializer(id => Monitors.SizeOf(monitors.Value, id)), Executables); MissingVehicleLog = new MissingVehicleLog(SettingsStore.Settings, missingAssets, Library, log); Favorites = new FavoriteSceneries(files, log, settingsPaths.FavoritesPath()); Tapes = new HaslerTapes(files, paths, log, clock, Timetables, () => State.SimulatorRunning); diff --git a/StarterNG/Application/SettingsStore.cs b/StarterNG/Application/SettingsStore.cs index 68c2fd6..0409e56 100644 --- a/StarterNG/Application/SettingsStore.cs +++ b/StarterNG/Application/SettingsStore.cs @@ -481,7 +481,8 @@ public sealed class SettingsStore return false; if (overCap.Contains(key)) return false; - // The simulator goes by it to the letter: the file's own, which it does not find, is no choice to keep. + // The simulator goes by these to the letter: a buffer of a window's size stretched + // over the monitor, a monitor's name it does not find. The file's own is no choice to keep. if (AsWritten(key) && _config.GetRaw(key) != value) return false; string? synced = _synced.GetValueOrDefault(key); @@ -491,7 +492,9 @@ public sealed class SettingsStore } bool AsWritten(string key) => - string.Equals(key, "fullscreenmonitor", StringComparison.OrdinalIgnoreCase); + string.Equals(key, "fullscreenmonitor", StringComparison.OrdinalIgnoreCase) || + (settings.FullscreenWindowed && + SettingsSerializer.FramebufferKeys.Contains(key, StringComparer.OrdinalIgnoreCase)); void Add(string key, string? value) { diff --git a/StarterNG/Infrastructure/Monitors.cs b/StarterNG/Infrastructure/Monitors.cs index 898d850..8f47520 100644 --- a/StarterNG/Infrastructure/Monitors.cs +++ b/StarterNG/Infrastructure/Monitors.cs @@ -36,6 +36,24 @@ public static class Monitors return Array.Empty(); } + /// + /// The size of the monitor a full screen of the simulator goes to (application.cpp): the one + /// names, or the primary one when it names none or one not connected. + /// Null where neither is among . + /// + public static (int Width, int Height)? SizeOf(IReadOnlyList monitors, string id) + { + DisplayMonitor? primary = null; + foreach (var m in monitors) + { + if (id.Length > 0 && m.Id == id) + return (m.Width, m.Height); + if (m.Primary) + primary ??= m; + } + return primary is null ? null : (primary.Width, primary.Height); + } + /// The id GLFW gives a monitor: its name with spaces as underscores, then where it starts. public static string IdOf(string name, int x, int y) => $"{name.Replace(' ', '_')}:{x},{y}"; diff --git a/StarterNG/Infrastructure/Settings/SettingsSerializer.cs b/StarterNG/Infrastructure/Settings/SettingsSerializer.cs index c38c302..21671be 100644 --- a/StarterNG/Infrastructure/Settings/SettingsSerializer.cs +++ b/StarterNG/Infrastructure/Settings/SettingsSerializer.cs @@ -11,6 +11,15 @@ namespace StarterNG.Infrastructure.Settings; public sealed class SettingsSerializer { + private readonly Func? _monitorSize; + + /// + /// The size of the monitor a full screen of the simulator goes to, by what fullscreenmonitor + /// says; null where it is not known. + /// + public SettingsSerializer(Func? monitorSize = null) => + _monitorSize = monitorSize; + /// /// An ini of before the tapes of the trains, its recording turned on ("blackbox yes", with the physics log of every /// vehicle, hundreds of files on a big scenery): moved to the tapes of the trains, the log of every vehicle off. @@ -282,8 +291,9 @@ public sealed class SettingsSerializer c.SetInt("height", s.Height); c.SetInt("starter.bufferscale", s.BufferScalePercent); // The simulator draws into a buffer of this size and stretches it to the window. - c.SetInt("gfx.framebuffer.width", (int)Math.Round(s.Width * s.BufferScalePercent / 100.0)); - c.SetInt("gfx.framebuffer.height", (int)Math.Round(s.Height * s.BufferScalePercent / 100.0)); + var (bufferWidth, bufferHeight) = FramebufferSize(s); + c.SetInt("gfx.framebuffer.width", bufferWidth); + c.SetInt("gfx.framebuffer.height", bufferHeight); c.SetInt("maxtexturesize", s.MaxTextureSize); c.SetInt("maxcabtexturesize", s.MaxCabTextureSize); c.SetDouble("anisotropicfiltering", s.TextureFiltering); @@ -385,6 +395,26 @@ public sealed class SettingsSerializer /// The keys of the frame buffer's size. public static readonly string[] FramebufferKeys = { "gfx.framebuffer.width", "gfx.framebuffer.height" }; + /// What the simulator takes for "as large as the window" (opengl33renderer.cpp), its own default. + public const int FramebufferOfWindow = -1; + + /// + /// The frame buffer of : its part of the window. Borderless, the window + /// is the monitor whatever width and height say (application.cpp), so the buffer is the + /// window's own, or its part of the monitor's size where that is known. + /// + private (int Width, int Height) FramebufferSize(SimulatorSettings s) + { + if (!s.FullscreenWindowed) + return (Part(s.Width, s.BufferScalePercent), Part(s.Height, s.BufferScalePercent)); + if (s.BufferScalePercent < 100 && + _monitorSize?.Invoke(s.FullscreenMonitor.Trim()) is { Width: > 0, Height: > 0 } monitor) + return (Part(monitor.Width, s.BufferScalePercent), Part(monitor.Height, s.BufferScalePercent)); + return (FramebufferOfWindow, FramebufferOfWindow); + } + + private static int Part(int size, int percent) => (int)Math.Round(size * percent / 100.0); + /// /// with the keys that say the same setting along with them: the /// starter's own percent goes wherever the frame buffer's size does, or it would stand