Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
811ef54
proof of concept for Hi DPI
ypujante Oct 26, 2023
7c457a4
fixed fullscreen mode
ypujante Oct 28, 2023
6c0e378
removed log statements
ypujante Oct 28, 2023
cbeb0e1
Make sure it works for "default" shell
ypujante Oct 30, 2023
ba13c6f
fixes fullscreen mode (user triggered)
ypujante Oct 30, 2023
9f7a2a2
[PR Review]: Fixes code style
ypujante Nov 2, 2023
454cd0f
[PR Review]: Reverting changes (separate issue)
ypujante Nov 2, 2023
15f1ff1
[PR Review]: getting rid of initialization
ypujante Nov 2, 2023
e38a34c
[PR Review]: fix bad api call
ypujante Nov 2, 2023
b291873
Merge branch 'hi-dpi-aware'
ypujante Nov 3, 2023
51f3288
Merge branch 'emscripten-core:main' into hi-dpi-aware
ypujante Nov 4, 2023
08a5b69
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Nov 6, 2023
f4320c3
added comprehensive test
ypujante Nov 7, 2023
875f440
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Nov 7, 2023
91aa2ad
fixes error in CI
ypujante Nov 7, 2023
80b982b
changes from code review
ypujante Nov 7, 2023
7d4fdcd
Merge branch 'emscripten-core:main' into hi-dpi-aware
ypujante Nov 11, 2023
c30e05e
reverting automatic merge issue :(
ypujante Nov 11, 2023
f73c502
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Nov 14, 2023
6ff6773
simplified code further
ypujante Nov 14, 2023
11d5832
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Nov 21, 2023
241e027
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Nov 22, 2023
f3de65e
Moved API to GLFW
ypujante Nov 22, 2023
a999fd0
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Nov 29, 2023
7cc2f59
reverted all changes to library_browser.js
ypujante Nov 29, 2023
a81f9a0
Implemented hi dpi aware in GLFW only
ypujante Nov 29, 2023
80be65a
install/remove single listener (devicePixelRatio)
ypujante Nov 29, 2023
86aff8e
removed SDL reference
ypujante Nov 30, 2023
73f6b2f
renamed Browser->GLFW
ypujante Nov 30, 2023
4ae7e31
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Nov 30, 2023
95a6918
apply changes to Browser.calculateMouseEvent
ypujante Nov 30, 2023
7a7e2ab
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Nov 30, 2023
2365d0e
removed duplicated code due to merge :(
ypujante Nov 30, 2023
1f4f37f
renamed exported function
ypujante Nov 30, 2023
767676a
use GLFW_SCALE_TO_MONITOR dynamically
ypujante Dec 1, 2023
9d29c8d
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Dec 1, 2023
eb2dbf9
removed unecessary options
ypujante Dec 1, 2023
66c6e2a
fixed missing ;
ypujante Dec 4, 2023
4187761
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Dec 4, 2023
822f55d
renamed attribute
ypujante Dec 4, 2023
8a58de4
added descriptive comment
ypujante Dec 4, 2023
be0979e
fixed failing test (due to variable rename)
ypujante Dec 5, 2023
1b87dd7
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Dec 5, 2023
a3fb66d
Merge remote-tracking branch 'origin/main' into hi-dpi-aware
ypujante Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions src/library_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var LibraryBrowser = {
#endif
Module["requestAnimationFrame"] = (func) => Browser.requestAnimationFrame(func);
Module["setCanvasSize"] = (width, height, noUpdates) => Browser.setCanvasSize(width, height, noUpdates);
Module["setHiDPIAware"] = (isHiDPIAware) => Browser.setHiDPIAware(isHiDPIAware);
Module["pauseMainLoop"] = () => Browser.mainLoop.pause();
Module["resumeMainLoop"] = () => Browser.mainLoop.resume();
Module["getUserMedia"] = () => Browser.getUserMedia();
Expand Down Expand Up @@ -99,6 +100,7 @@ var LibraryBrowser = {
}
},
isFullscreen: false,
isHiDPIAware: false,
pointerLock: false,
moduleContextCreatedCallbacks: [],
workers: [],
Expand Down Expand Up @@ -331,7 +333,7 @@ var LibraryBrowser = {
if (Browser.resizeCanvas) {
Browser.setFullscreenCanvasSize();
} else {
Browser.updateCanvasDimensions(canvas);
Browser.setCanvasSize(canvas);
}
} else {
// remove the full screen specific parent of the canvas again to restore the HTML structure from before going full screen
Expand All @@ -341,7 +343,7 @@ var LibraryBrowser = {
if (Browser.resizeCanvas) {
Browser.setWindowedCanvasSize();
} else {
Browser.updateCanvasDimensions(canvas);
Browser.setCanvasSize(canvas);
}
}
if (Module['onFullScreen']) Module['onFullScreen'](Browser.isFullscreen);
Expand Down Expand Up @@ -563,6 +565,12 @@ var LibraryBrowser = {
var cw = Module["canvas"].width;
var ch = Module["canvas"].height;

if (Browser.isHiDPIAware) {
const scale = Browser.getHiDPIScale();
cw /= scale;
ch /= scale;
}

// Neither .scrollX or .pageXOffset are defined in a spec, but
// we prefer .scrollX because it is currently in a spec draft.
// (see: http://www.w3.org/TR/2013/WD-cssom-view-20131217/)
Expand Down Expand Up @@ -620,7 +628,11 @@ var LibraryBrowser = {

updateResizeListeners() {
var canvas = Module['canvas'];
Browser.resizeListeners.forEach((listener) => listener(canvas.width, canvas.height));
if (Browser.isHiDPIAware) {
Browser.resizeListeners.forEach((listener) => listener(canvas.clientWidth, canvas.clientHeight, canvas.width, canvas.height));
} else {
Browser.resizeListeners.forEach((listener) => listener(canvas.width, canvas.height, canvas.width, canvas.height));
}
},

setCanvasSize(width, height, noUpdates) {
Expand Down Expand Up @@ -654,6 +666,8 @@ var LibraryBrowser = {
},

updateCanvasDimensions(canvas, wNative, hNative) {
const scale = Browser.getHiDPIScale();

if (wNative && hNative) {
canvas.widthNative = wNative;
canvas.heightNative = hNative;
Expand All @@ -678,24 +692,39 @@ var LibraryBrowser = {
h = Math.round(h * factor);
}
if (Browser.resizeCanvas) {
if (canvas.width != w) canvas.width = w;
if (canvas.height != h) canvas.height = h;
if (typeof canvas.style != 'undefined') {
wNative = w;
hNative = h;
}
const wNativeScaled = Math.floor(wNative * scale);
const hNativeScaled = Math.floor(hNative * scale);
if (canvas.width != wNativeScaled) canvas.width = wNativeScaled;
if (canvas.height != hNativeScaled) canvas.height = hNativeScaled;
if (typeof canvas.style != 'undefined') {
if (wNativeScaled != wNative || hNativeScaled != hNative) {
canvas.style.setProperty( "width", wNative + "px", "important");
canvas.style.setProperty("height", hNative + "px", "important");
} else {
canvas.style.removeProperty( "width");
canvas.style.removeProperty("height");
}
} else {
if (canvas.width != wNative) canvas.width = wNative;
if (canvas.height != hNative) canvas.height = hNative;
if (typeof canvas.style != 'undefined') {
if (w != wNative || h != hNative) {
canvas.style.setProperty( "width", w + "px", "important");
canvas.style.setProperty("height", h + "px", "important");
} else {
canvas.style.removeProperty( "width");
canvas.style.removeProperty("height");
}
}
}
},

getDevicePixelRatio() {
return (typeof devicePixelRatio == 'number' && devicePixelRatio) || 1.0;
},

getHiDPIScale() {
return Browser.isHiDPIAware ? Browser.getDevicePixelRatio() : 1.0;
},

setHiDPIAware(isHiDPIAware) {
isHiDPIAware = !!isHiDPIAware; // coerce to boolean
if (Browser.isHiDPIAware != isHiDPIAware) {
Browser.isHiDPIAware = isHiDPIAware;
const canvas = Module['canvas'];
Browser.updateCanvasDimensions(canvas, canvas.clientWidth, canvas.clientHeight);
Browser.updateResizeListeners();
}
},
},
Expand Down
66 changes: 36 additions & 30 deletions src/library_glfw.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

var LibraryGLFW = {
$GLFW_Window__docs: '/** @constructor */',
$GLFW_Window: function(id, width, height, title, monitor, share) {
$GLFW_Window: function(id, width, height, framebufferWidth, framebufferHeight, title, monitor, share) {
this.id = id;
this.x = 0;
this.y = 0;
Expand All @@ -46,6 +46,8 @@ var LibraryGLFW = {
this.storedY = 0; // Used to store Y before fullscreen
this.width = width;
this.height = height;
this.framebufferWidth = framebufferWidth;
this.framebufferHeight = framebufferHeight;
this.storedWidth = width; // Used to store width before fullscreen
this.storedHeight = height; // Used to store height before fullscreen
this.title = title;
Expand Down Expand Up @@ -550,44 +552,46 @@ var LibraryGLFW = {
event.preventDefault();
},

onCanvasResize: (width, height) => {
onCanvasResize: (width, height, framebufferWidth, framebufferHeight) => {
if (!GLFW.active) return;

var resizeNeeded = true;
var resizeNeeded = false;

// If the client is requesting fullscreen mode
if (document["fullscreen"] || document["fullScreen"] || document["mozFullScreen"] || document["webkitIsFullScreen"]) {
GLFW.active.storedX = GLFW.active.x;
GLFW.active.storedY = GLFW.active.y;
GLFW.active.storedWidth = GLFW.active.width;
GLFW.active.storedHeight = GLFW.active.height;
GLFW.active.x = GLFW.active.y = 0;
GLFW.active.width = screen.width;
GLFW.active.height = screen.height;
GLFW.active.fullscreen = true;

if (!GLFW.active.fullscreen) {
resizeNeeded = width != screen.width || height != screen.height;
GLFW.active.storedX = GLFW.active.x;
GLFW.active.storedY = GLFW.active.y;
GLFW.active.storedWidth = GLFW.active.width;
GLFW.active.storedHeight = GLFW.active.height;
GLFW.active.x = GLFW.active.y = 0;
GLFW.active.width = screen.width;
GLFW.active.height = screen.height;
GLFW.active.fullscreen = true;
}
// If the client is reverting from fullscreen mode
} else if (GLFW.active.fullscreen == true) {
resizeNeeded = width != GLFW.active.storedWidth || height != GLFW.active.storedHeight;
GLFW.active.x = GLFW.active.storedX;
GLFW.active.y = GLFW.active.storedY;
GLFW.active.width = GLFW.active.storedWidth;
GLFW.active.height = GLFW.active.storedHeight;
GLFW.active.fullscreen = false;

// If the width/height values do not match current active window sizes
} else if (GLFW.active.width != width || GLFW.active.height != height) {
GLFW.active.width = width;
GLFW.active.height = height;
} else {
resizeNeeded = false;
}

// If any of the above conditions were true, we need to resize the canvas
if (resizeNeeded) {
// resets the canvas size to counter the aspect preservation of Browser.updateCanvasDimensions
Browser.setCanvasSize(GLFW.active.width, GLFW.active.height, true);
// TODO: Client dimensions (clientWidth/clientHeight) vs pixel dimensions (width/height) of
// the canvas should drive window and framebuffer size respectfully.
// width or height is changed (fullscreen / exit fullscreen) which will call this listener back
// with proper framebufferWidth/framebufferHeight
Browser.setCanvasSize(GLFW.active.width, GLFW.active.height);
} else if (GLFW.active.width != width ||
GLFW.active.height != height ||
GLFW.active.framebufferWidth != framebufferWidth ||
GLFW.active.framebufferHeight != framebufferHeight) {
GLFW.active.width = width;
GLFW.active.height = height;
GLFW.active.framebufferWidth = framebufferWidth;
GLFW.active.framebufferHeight = framebufferHeight;
GLFW.onWindowSizeChanged();
GLFW.onFramebufferSizeChanged();
}
Expand All @@ -611,7 +615,7 @@ var LibraryGLFW = {

#if USE_GLFW == 3
if (GLFW.active.framebufferSizeFunc) {
{{{ makeDynCall('viii', 'GLFW.active.framebufferSizeFunc') }}}(GLFW.active.id, GLFW.active.width, GLFW.active.height);
{{{ makeDynCall('viii', 'GLFW.active.framebufferSizeFunc') }}}(GLFW.active.id, GLFW.active.framebufferWidth, GLFW.active.framebufferHeight);
}
#endif
},
Expand Down Expand Up @@ -1045,7 +1049,8 @@ var LibraryGLFW = {
if (!Module.ctx && useWebGL) return 0;

// Get non alive id
var win = new GLFW_Window(id, width, height, title, monitor, share);
const canvas = Module['canvas'];
var win = new GLFW_Window(id, canvas.clientWidth, canvas.clientHeight, canvas.width, canvas.height, title, monitor, share);

// Set window to array
if (id - 1 == GLFW.windows.length) {
Expand Down Expand Up @@ -1160,9 +1165,10 @@ var LibraryGLFW = {
Module["canvas"].addEventListener('drop', GLFW.onDrop, true);
Module["canvas"].addEventListener('dragover', GLFW.onDragover, true);

Browser.resizeListeners.push((width, height) => {
GLFW.onCanvasResize(width, height);
Browser.resizeListeners.push((width, height, framebufferWidth, framebufferHeight) => {
GLFW.onCanvasResize(width, height, framebufferWidth, framebufferHeight);
});

return 1; // GL_TRUE
},

Expand Down Expand Up @@ -1369,8 +1375,8 @@ var LibraryGLFW = {

var win = GLFW.WindowFromId(winid);
if (win) {
ww = win.width;
wh = win.height;
ww = win.framebufferWidth;
wh = win.framebufferHeight;
}

if (width) {
Expand Down