Allow window size/scale properties to be accessed from other threads.

This commit is contained in:
Paul Chote
2018-06-13 18:10:54 +00:00
parent 72eb5543c2
commit ea068a36f7

View File

@@ -23,22 +23,63 @@ namespace OpenRA.Platforms.Default
public IGraphicsContext Context { get { return context; } } public IGraphicsContext Context { get { return context; } }
internal readonly IntPtr Window; readonly IntPtr window;
bool disposed; bool disposed;
public Size WindowSize { get; private set; } readonly object syncObject = new object();
public float WindowScale { get; private set; } Size windowSize;
Size surfaceSize;
float windowScale;
internal IntPtr Window
{
get
{
lock (syncObject)
return window;
}
}
public Size WindowSize
{
get
{
lock (syncObject)
return windowSize;
}
}
public float WindowScale
{
get
{
lock (syncObject)
return windowScale;
}
}
internal Size SurfaceSize
{
get
{
lock (syncObject)
return surfaceSize;
}
}
internal Size SurfaceSize { get; private set; }
public event Action<float, float> OnWindowScaleChanged = (before, after) => { }; public event Action<float, float> OnWindowScaleChanged = (before, after) => { };
[DllImport("user32.dll")] [DllImport("user32.dll")]
static extern bool SetProcessDPIAware(); static extern bool SetProcessDPIAware();
public Sdl2PlatformWindow(Size windowSize, WindowMode windowMode) public Sdl2PlatformWindow(Size requestWindowSize, WindowMode windowMode)
{ {
Console.WriteLine("Using SDL 2 with OpenGL renderer"); Console.WriteLine("Using SDL 2 with OpenGL renderer");
WindowSize = windowSize;
// Lock the Window/Surface properties until initialization is complete
lock (syncObject)
{
windowSize = requestWindowSize;
// Disable legacy scaling on Windows // Disable legacy scaling on Windows
if (Platform.CurrentPlatform == PlatformType.Windows && !Game.Settings.Graphics.DisableWindowsDPIScaling) if (Platform.CurrentPlatform == PlatformType.Windows && !Game.Settings.Graphics.DisableWindowsDPIScaling)
@@ -55,13 +96,13 @@ namespace OpenRA.Platforms.Default
SDL.SDL_GetCurrentDisplayMode(0, out display); SDL.SDL_GetCurrentDisplayMode(0, out display);
Console.WriteLine("Desktop resolution: {0}x{1}", display.w, display.h); Console.WriteLine("Desktop resolution: {0}x{1}", display.w, display.h);
if (WindowSize.Width == 0 && WindowSize.Height == 0) if (windowSize.Width == 0 && windowSize.Height == 0)
{ {
Console.WriteLine("No custom resolution provided, using desktop resolution"); Console.WriteLine("No custom resolution provided, using desktop resolution");
WindowSize = new Size(display.w, display.h); windowSize = new Size(display.w, display.h);
} }
Console.WriteLine("Using resolution: {0}x{1}", WindowSize.Width, WindowSize.Height); Console.WriteLine("Using resolution: {0}x{1}", windowSize.Width, windowSize.Height);
var windowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL.SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI; var windowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL.SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI;
@@ -69,11 +110,11 @@ namespace OpenRA.Platforms.Default
if (Platform.CurrentPlatform == PlatformType.OSX && windowMode == WindowMode.Fullscreen) if (Platform.CurrentPlatform == PlatformType.OSX && windowMode == WindowMode.Fullscreen)
SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1"); SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1");
Window = SDL.SDL_CreateWindow("OpenRA", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, window = SDL.SDL_CreateWindow("OpenRA", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED,
WindowSize.Width, WindowSize.Height, windowFlags); windowSize.Width, windowSize.Height, windowFlags);
SurfaceSize = WindowSize; surfaceSize = windowSize;
WindowScale = 1; windowScale = 1;
// Enable high resolution rendering for Retina displays // Enable high resolution rendering for Retina displays
if (Platform.CurrentPlatform == PlatformType.OSX) if (Platform.CurrentPlatform == PlatformType.OSX)
@@ -81,17 +122,18 @@ namespace OpenRA.Platforms.Default
// OSX defines the window size in "points", with a device-dependent number of pixels per point. // OSX defines the window size in "points", with a device-dependent number of pixels per point.
// The window scale is simply the ratio of GL pixels / window points. // The window scale is simply the ratio of GL pixels / window points.
int width, height; int width, height;
SDL.SDL_GL_GetDrawableSize(Window, out width, out height); SDL.SDL_GL_GetDrawableSize(Window, out width, out height);
SurfaceSize = new Size(width, height); surfaceSize = new Size(width, height);
WindowScale = width * 1f / WindowSize.Width; windowScale = width * 1f / windowSize.Width;
} }
else if (Platform.CurrentPlatform == PlatformType.Windows) else if (Platform.CurrentPlatform == PlatformType.Windows)
{ {
float ddpi, hdpi, vdpi; float ddpi, hdpi, vdpi;
if (!Game.Settings.Graphics.DisableWindowsDPIScaling && SDL.SDL_GetDisplayDPI(0, out ddpi, out hdpi, out vdpi) == 0) if (!Game.Settings.Graphics.DisableWindowsDPIScaling && SDL.SDL_GetDisplayDPI(0, out ddpi, out hdpi, out vdpi) == 0)
{ {
WindowScale = ddpi / 96; windowScale = ddpi / 96;
WindowSize = new Size((int)(SurfaceSize.Width / WindowScale), (int)(SurfaceSize.Height / WindowScale)); windowSize = new Size((int)(surfaceSize.Width / windowScale), (int)(surfaceSize.Height / windowScale));
} }
} }
else else
@@ -100,12 +142,12 @@ namespace OpenRA.Platforms.Default
var scaleVariable = Environment.GetEnvironmentVariable("OPENRA_DISPLAY_SCALE"); var scaleVariable = Environment.GetEnvironmentVariable("OPENRA_DISPLAY_SCALE");
if (scaleVariable != null && float.TryParse(scaleVariable, out scale)) if (scaleVariable != null && float.TryParse(scaleVariable, out scale))
{ {
WindowScale = 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); Console.WriteLine("Using window scale {0:F2}", windowScale);
if (Game.Settings.Game.LockMouseWindow) if (Game.Settings.Game.LockMouseWindow)
GrabWindowMouseFocus(); GrabWindowMouseFocus();
@@ -126,8 +168,8 @@ namespace OpenRA.Platforms.Default
{ {
int width, height; int width, height;
SDL.SDL_GetWindowSize(Window, out width, out height); SDL.SDL_GetWindowSize(Window, out width, out height);
WindowSize = SurfaceSize = new Size(width, height); windowSize = surfaceSize = new Size(width, height);
WindowScale = 1; windowScale = 1;
} }
} }
else if (windowMode == WindowMode.PseudoFullscreen) else if (windowMode == WindowMode.PseudoFullscreen)
@@ -140,6 +182,7 @@ namespace OpenRA.Platforms.Default
SDL.SDL_SetWindowFullscreen(Window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP); SDL.SDL_SetWindowFullscreen(Window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
} }
}
context = new Sdl2GraphicsContext(this); context = new Sdl2GraphicsContext(this);
@@ -207,11 +250,15 @@ namespace OpenRA.Platforms.Default
if (width != SurfaceSize.Width || height != SurfaceSize.Height) if (width != SurfaceSize.Width || height != SurfaceSize.Height)
{ {
var oldScale = WindowScale; float oldScale;
SurfaceSize = new Size(width, height); lock (syncObject)
WindowScale = width * 1f / WindowSize.Width; {
oldScale = windowScale;
surfaceSize = new Size(width, height);
windowScale = width * 1f / windowSize.Width;
}
OnWindowScaleChanged(oldScale, WindowScale); OnWindowScaleChanged(oldScale, windowScale);
} }
} }
} }