From b510a30ee8b105c9cda3b30eefc9cc10baf56030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aksel=20Kornesj=C3=B6?= Date: Thu, 7 Jan 2021 18:08:06 +0100 Subject: [PATCH 1/3] Added highDPI flag that enables it when creating a SDL window. The window size will be the same as a non high DPI one, but the underlying window buffer will be set to the correct pixel size, based on SDL_GL_GetDrawableSize instead of SDL_GetWindowSize. --- .gitignore | 1 + h3d/Engine.hx | 8 +++++++- hxd/System.hl.hx | 5 +++-- hxd/Window.hl.hx | 18 ++++++++++++++++-- hxd/Window.hx | 10 ++++++++++ hxd/Window.js.hx | 11 +++++++++++ 6 files changed, 48 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index f8055b0719..04c35efa25 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ bin .vscode /tools/mikktspace/out *.exe +.DS_store \ No newline at end of file diff --git a/h3d/Engine.hx b/h3d/Engine.hx index e07ba63838..5599505de6 100644 --- a/h3d/Engine.hx +++ b/h3d/Engine.hx @@ -280,7 +280,13 @@ class Engine { if( height < 32 ) height = 32; this.width = width; this.height = height; - if( !driver.isDisposed() ) driver.resize(width, height); + + // Set the driver size to actual pixel width + // (same as window size on normal displays, differs when high DPI is enabled) + var pw = window.pixelWidth; + var ph = window.pixelHeight; + + if( !driver.isDisposed() ) driver.resize(pw, ph); } public function begin() { diff --git a/hxd/System.hl.hx b/hxd/System.hl.hx index 98cb74c0f4..76b376553c 100644 --- a/hxd/System.hl.hx +++ b/hxd/System.hl.hx @@ -97,6 +97,7 @@ class System { var size = haxe.macro.Compiler.getDefine("windowSize"); var title = haxe.macro.Compiler.getDefine("windowTitle"); var fixed = haxe.macro.Compiler.getDefine("windowFixed") == "1"; + var highDPI = haxe.macro.Compiler.getDefine("highDPI") == "1"; if( title == null ) title = ""; if( size != null ) { @@ -108,10 +109,10 @@ class System { #if hlsdl sdl.Sdl.init(); @:privateAccess Window.initChars(); - @:privateAccess Window.inst = new Window(title, width, height, fixed); + @:privateAccess Window.inst = new Window(title, width, height, fixed, highDPI); init(); #elseif hldx - @:privateAccess Window.inst = new Window(title, width, height, fixed); + @:privateAccess Window.inst = new Window(title, width, height, fixed, highDPI); init(); #else @:privateAccess Window.inst = new Window(title, width, height); diff --git a/hxd/Window.hl.hx b/hxd/Window.hl.hx index 41ee95dfb7..797ae9306f 100644 --- a/hxd/Window.hl.hx +++ b/hxd/Window.hl.hx @@ -26,6 +26,9 @@ class Window { public var width(get, never) : Int; public var height(get, never) : Int; + public var pixelWidth(get, never) : Int; + public var pixelHeight(get, never) : Int; + public var mouseX(get, never) : Int; public var mouseY(get, never) : Int; public var mouseLock(get, set) : Bool; @@ -50,13 +53,16 @@ class Window { static inline var TOUCH_SCALE = #if (hl_ver >= version("1.12.0")) 10000 #else 100 #end; #end - function new(title:String, width:Int, height:Int, fixed:Bool = false) { + function new(title:String, width:Int, height:Int, fixed:Bool = false, highDPI = false) { this.windowWidth = width; this.windowHeight = height; eventTargets = new List(); resizeEvents = new List(); #if hlsdl - final sdlFlags = if (!fixed) sdl.Window.SDL_WINDOW_SHOWN | sdl.Window.SDL_WINDOW_RESIZABLE else sdl.Window.SDL_WINDOW_SHOWN; + var sdlFlags = if (!fixed) sdl.Window.SDL_WINDOW_SHOWN | sdl.Window.SDL_WINDOW_RESIZABLE else sdl.Window.SDL_WINDOW_SHOWN; + if (highDPI) { + sdlFlags |= sdl.Window.SDL_WINDOW_ALLOW_HIGHDPI; + } window = new sdl.Window(title, width, height, sdl.Window.SDL_WINDOWPOS_CENTERED, sdl.Window.SDL_WINDOWPOS_CENTERED, sdlFlags); #elseif hldx final dxFlags = if (!fixed) dx.Window.RESIZABLE else 0; @@ -133,6 +139,14 @@ class Window { function get_height() : Int { return windowHeight; } + + function get_pixelWidth() : Int { + return window.pixelWidth; + } + + function get_pixelHeight() : Int { + return window.pixelHeight; + } function get_mouseLock() : Bool { return false; diff --git a/hxd/Window.hx b/hxd/Window.hx index 79e5db182b..f6c051855b 100644 --- a/hxd/Window.hx +++ b/hxd/Window.hx @@ -14,6 +14,9 @@ class Window { public var width(get, never) : Int; public var height(get, never) : Int; + public var pixelWidth(get, never) : Int; + public var pixelHeight(get, never) : Int; + public var mouseX(get, never) : Int; public var mouseY(get, never) : Int; public var mouseLock(get, set) : Bool; @@ -95,6 +98,13 @@ class Window { return 0; } + function get_pixelWidth() : Int { + return 0; + } + function get_pixelHeight() : Int { + return 0; + } + function get_mouseLock() : Bool { return false; } diff --git a/hxd/Window.js.hx b/hxd/Window.js.hx index 086815c923..e99c563510 100644 --- a/hxd/Window.js.hx +++ b/hxd/Window.js.hx @@ -14,6 +14,9 @@ class Window { public var width(get, never) : Int; public var height(get, never) : Int; + public var pixelWidth(get, never) : Int; + public var pixelHeight(get, never) : Int; + public var mouseX(get, never) : Int; public var mouseY(get, never) : Int; public var mouseLock(get, set) : Bool; @@ -201,6 +204,14 @@ class Window { return Math.round(canvasPos.height * getPixelRatio()); } + function get_pixelHeight() { + return height; + } + + function get_pixelWidth() { + return width; + } + function get_mouseX() { return Math.round((curMouseX - canvasPos.left) * getPixelRatio()); } From 721a3d80baf87d39a1226d3ec54fa0850c091cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aksel=20Kornesj=C3=B6?= Date: Fri, 8 Jan 2021 21:42:39 +0100 Subject: [PATCH 2/3] Swapped pixelWidth and pixelHeight for windowToPixelRatio --- h3d/Engine.hx | 5 +++-- hxd/Window.hl.hx | 7 +++++-- hxd/Window.hx | 10 +++------- hxd/Window.js.hx | 11 +++-------- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/h3d/Engine.hx b/h3d/Engine.hx index 5599505de6..757963d4f2 100644 --- a/h3d/Engine.hx +++ b/h3d/Engine.hx @@ -283,8 +283,9 @@ class Engine { // Set the driver size to actual pixel width // (same as window size on normal displays, differs when high DPI is enabled) - var pw = window.pixelWidth; - var ph = window.pixelHeight; + var r = window.windowToPixelRatio; + var pw = Std.int(width / r); + var ph = Std.int(height / r); if( !driver.isDisposed() ) driver.resize(pw, ph); } diff --git a/hxd/Window.hl.hx b/hxd/Window.hl.hx index 797ae9306f..2f58c63afa 100644 --- a/hxd/Window.hl.hx +++ b/hxd/Window.hl.hx @@ -26,8 +26,7 @@ class Window { public var width(get, never) : Int; public var height(get, never) : Int; - public var pixelWidth(get, never) : Int; - public var pixelHeight(get, never) : Int; + public var windowToPixelRatio(get, never) : Float; public var mouseX(get, never) : Int; public var mouseY(get, never) : Int; @@ -148,6 +147,10 @@ class Window { return window.pixelHeight; } + function get_windowToPixelRatio() : Float { + return window.windowToPixelRatio; + } + function get_mouseLock() : Bool { return false; } diff --git a/hxd/Window.hx b/hxd/Window.hx index f6c051855b..c71dc533eb 100644 --- a/hxd/Window.hx +++ b/hxd/Window.hx @@ -14,8 +14,7 @@ class Window { public var width(get, never) : Int; public var height(get, never) : Int; - public var pixelWidth(get, never) : Int; - public var pixelHeight(get, never) : Int; + public var windowToPixelRatio(get, never) : Float; public var mouseX(get, never) : Int; public var mouseY(get, never) : Int; @@ -98,11 +97,8 @@ class Window { return 0; } - function get_pixelWidth() : Int { - return 0; - } - function get_pixelHeight() : Int { - return 0; + function get_windowToPixelRatio() : Float { + return 0.0; } function get_mouseLock() : Bool { diff --git a/hxd/Window.js.hx b/hxd/Window.js.hx index e99c563510..ed048d7746 100644 --- a/hxd/Window.js.hx +++ b/hxd/Window.js.hx @@ -14,8 +14,7 @@ class Window { public var width(get, never) : Int; public var height(get, never) : Int; - public var pixelWidth(get, never) : Int; - public var pixelHeight(get, never) : Int; + public var windowToPixelRatio(get, never) : Float; public var mouseX(get, never) : Int; public var mouseY(get, never) : Int; @@ -204,12 +203,8 @@ class Window { return Math.round(canvasPos.height * getPixelRatio()); } - function get_pixelHeight() { - return height; - } - - function get_pixelWidth() { - return width; + function get_windowToPixelRatio() { + return 1.0; } function get_mouseX() { From 54c5a54ca7eac0728e9304d9a8e17505688381e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aksel=20Kornesj=C3=B6?= Date: Fri, 8 Jan 2021 21:56:26 +0100 Subject: [PATCH 3/3] removed forgotten functions --- hxd/Window.hl.hx | 8 -------- 1 file changed, 8 deletions(-) diff --git a/hxd/Window.hl.hx b/hxd/Window.hl.hx index 2f58c63afa..246f1a5fe2 100644 --- a/hxd/Window.hl.hx +++ b/hxd/Window.hl.hx @@ -139,14 +139,6 @@ class Window { return windowHeight; } - function get_pixelWidth() : Int { - return window.pixelWidth; - } - - function get_pixelHeight() : Int { - return window.pixelHeight; - } - function get_windowToPixelRatio() : Float { return window.windowToPixelRatio; }