Skip to content

Commit

Permalink
Define window size in effective pixel coordinates.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote committed Jan 26, 2020
1 parent f08d4ad commit af8e807
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions OpenRA.Platforms.Default/Sdl2PlatformWindow.cs
Expand Up @@ -29,7 +29,7 @@ sealed class Sdl2PlatformWindow : ThreadAffine, IPlatformWindow
readonly object syncObject = new object();
Size windowSize;
Size surfaceSize;
float windowScale;
float windowScale = 1f;
int2? lockedMousePosition;

internal IntPtr Window
Expand Down Expand Up @@ -73,13 +73,11 @@ public Size SurfaceSize
[DllImport("user32.dll")]
static extern bool SetProcessDPIAware();

public Sdl2PlatformWindow(Size requestWindowSize, WindowMode windowMode, int batchSize)
public Sdl2PlatformWindow(Size requestEffectiveWindowSize, WindowMode windowMode, int batchSize)
{
// Lock the Window/Surface properties until initialization is complete
lock (syncObject)
{
windowSize = requestWindowSize;

// Disable legacy scaling on Windows
if (Platform.CurrentPlatform == PlatformType.Windows)
SetProcessDPIAware();
Expand Down Expand Up @@ -113,12 +111,31 @@ public Sdl2PlatformWindow(Size requestWindowSize, WindowMode windowMode, int bat
SDL.SDL_DisplayMode display;
SDL.SDL_GetCurrentDisplayMode(0, out display);

// Windows and Linux define window sizes in native pixel units.
// Query the display/dpi scale so we can convert our requested effective size to pixels.
// This is not necessary on macOS, which defines window sizes in effective units ("points").
if (Platform.CurrentPlatform == PlatformType.Windows)
{
float ddpi, hdpi, vdpi;
if (SDL.SDL_GetDisplayDPI(0, out ddpi, out hdpi, out vdpi) == 0)
windowScale = ddpi / 96;
}
else if (Platform.CurrentPlatform != PlatformType.OSX)
{
float scale = 1;
var scaleVariable = Environment.GetEnvironmentVariable("OPENRA_DISPLAY_SCALE");
if (scaleVariable != null && float.TryParse(scaleVariable, out scale))
windowScale = scale;
}

Console.WriteLine("Desktop resolution: {0}x{1}", display.w, display.h);
if (windowSize.Width == 0 && windowSize.Height == 0)
if (requestEffectiveWindowSize.Width == 0 && requestEffectiveWindowSize.Height == 0)
{
Console.WriteLine("No custom resolution provided, using desktop resolution");
windowSize = new Size(display.w, display.h);
surfaceSize = windowSize = new Size(display.w, display.h);
}
else
surfaceSize = windowSize = new Size((int)(requestEffectiveWindowSize.Width * windowScale), (int)(requestEffectiveWindowSize.Height * windowScale));

Console.WriteLine("Using resolution: {0}x{1}", windowSize.Width, windowSize.Height);

Expand Down Expand Up @@ -156,9 +173,6 @@ public Sdl2PlatformWindow(Size requestWindowSize, WindowMode windowMode, int bat
}
}

surfaceSize = windowSize;
windowScale = 1;

// Enable high resolution rendering for Retina displays
if (Platform.CurrentPlatform == PlatformType.OSX)
{
Expand All @@ -170,25 +184,8 @@ public Sdl2PlatformWindow(Size requestWindowSize, WindowMode windowMode, int bat
surfaceSize = new Size(width, height);
windowScale = width * 1f / windowSize.Width;
}
else if (Platform.CurrentPlatform == PlatformType.Windows)
{
float ddpi, hdpi, vdpi;
if (SDL.SDL_GetDisplayDPI(0, out ddpi, out hdpi, out vdpi) == 0)
{
windowScale = ddpi / 96;
windowSize = new Size((int)(surfaceSize.Width / windowScale), (int)(surfaceSize.Height / windowScale));
}
}
else
{
float scale = 1;
var scaleVariable = Environment.GetEnvironmentVariable("OPENRA_DISPLAY_SCALE");
if (scaleVariable != null && float.TryParse(scaleVariable, out scale))
{
windowScale = scale;
windowSize = new Size((int)(surfaceSize.Width / windowScale), (int)(surfaceSize.Height / windowScale));
}
}
windowSize = new Size((int)(surfaceSize.Width / windowScale), (int)(surfaceSize.Height / windowScale));

Console.WriteLine("Using window scale {0:F2}", windowScale);

Expand Down

0 comments on commit af8e807

Please sign in to comment.