fix(settings): a borderless screen draws into a buffer of the monitor's size, not the resolution row's

With fullscreenwindowed the simulator takes the window's size from the monitor and leaves width and height unread (application.cpp), but its full renderer still draws into gfx.framebuffer.width and height (opengl33renderer.cpp). Those were written from the resolution row, greyed out in that mode: 1280x720 stretched over a larger monitor. Borderless, they are now -1, the simulator's own word for a buffer of the window's size, or with a smaller render scale that part of the chosen monitor's size, the primary one's when none is chosen. A size left in a borderless file goes on the next save.
This commit is contained in:
maj00r
2026-11-22 14:53:18 +01:00
parent d429d041ff
commit 5ec7a4b539
6 changed files with 154 additions and 5 deletions

View File

@@ -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<string>();
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()
{

View File

@@ -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<DisplayMonitor>(), "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");

View File

@@ -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<IReadOnlyList<DisplayMonitor>>(() => 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);

View File

@@ -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)
{

View File

@@ -36,6 +36,24 @@ public static class Monitors
return Array.Empty<DisplayMonitor>();
}
/// <summary>
/// The size of the monitor a full screen of the simulator goes to (application.cpp): the one
/// <paramref name="id"/> names, or the primary one when it names none or one not connected.
/// Null where neither is among <paramref name="monitors"/>.
/// </summary>
public static (int Width, int Height)? SizeOf(IReadOnlyList<DisplayMonitor> 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);
}
/// <summary>The id GLFW gives a monitor: its name with spaces as underscores, then where it starts.</summary>
public static string IdOf(string name, int x, int y) => $"{name.Replace(' ', '_')}:{x},{y}";

View File

@@ -11,6 +11,15 @@ namespace StarterNG.Infrastructure.Settings;
public sealed class SettingsSerializer
{
private readonly Func<string, (int Width, int Height)?>? _monitorSize;
/// <param name="monitorSize">
/// The size of the monitor a full screen of the simulator goes to, by what fullscreenmonitor
/// says; null where it is not known.
/// </param>
public SettingsSerializer(Func<string, (int Width, int Height)?>? monitorSize = null) =>
_monitorSize = monitorSize;
/// <summary>
/// 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
/// <summary>The keys of the frame buffer's size.</summary>
public static readonly string[] FramebufferKeys = { "gfx.framebuffer.width", "gfx.framebuffer.height" };
/// <summary>What the simulator takes for "as large as the window" (opengl33renderer.cpp), its own default.</summary>
public const int FramebufferOfWindow = -1;
/// <summary>
/// The frame buffer of <paramref name="s"/>: 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.
/// </summary>
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);
/// <summary>
/// <paramref name="keys"/> 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