Skip to content

Commit 7d639f7

Browse files
authored
Fix glfw3 default hints being modified (emscripten-core#20770)
1 parent 9de528b commit 7d639f7

3 files changed

Lines changed: 116 additions & 5 deletions

File tree

src/library_glfw.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var LibraryGLFW = {
5151
this.title = title;
5252
this.monitor = monitor;
5353
this.share = share;
54-
this.attributes = GLFW.hints;
54+
this.attributes = Object.assign({}, GLFW.hints);
5555
this.inputModes = {
5656
0x00033001:0x00034001, // GLFW_CURSOR (GLFW_CURSOR_NORMAL)
5757
0x00033002:0, // GLFW_STICKY_KEYS
@@ -1062,6 +1062,10 @@ var LibraryGLFW = {
10621062
}
10631063
},
10641064

1065+
defaultWindowHints: () => {
1066+
GLFW.hints = Object.assign({}, GLFW.defaultHints);
1067+
},
1068+
10651069
createWindow: (width, height, title, monitor, share) => {
10661070
var i, id;
10671071
for (i = 0; i < GLFW.windows.length && GLFW.windows[i] !== null; i++) {
@@ -1190,7 +1194,7 @@ var LibraryGLFW = {
11901194
if (GLFW.windows) return 1; // GL_TRUE
11911195

11921196
GLFW.initialTime = GLFW.getTime();
1193-
GLFW.hints = GLFW.defaultHints;
1197+
GLFW.defaultWindowHints();
11941198
GLFW.windows = new Array()
11951199
GLFW.active = null;
11961200
GLFW.scale = _emscripten_get_device_pixel_ratio();
@@ -1386,9 +1390,7 @@ var LibraryGLFW = {
13861390

13871391
glfwSetGammaRamp: (monitor, ramp) => { throw "glfwSetGammaRamp not implemented."; },
13881392

1389-
glfwDefaultWindowHints: () => {
1390-
GLFW.hints = GLFW.defaultHints;
1391-
},
1393+
glfwDefaultWindowHints: () => GLFW.defaultWindowHints(),
13921394

13931395
glfwWindowHint: (target, hint) => {
13941396
GLFW.hints[target] = hint;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2023 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <GLFW/glfw3.h>
9+
#include <assert.h>
10+
#include <emscripten/html5.h>
11+
12+
static void checkDefaultWindowHints() {
13+
int ok = EM_ASM_INT({
14+
var res = 1;
15+
for (var k in TEST_GLFW3_DEFAULTS_HINTS) {
16+
if (GLFW.defaultHints[k] !== TEST_GLFW3_DEFAULTS_HINTS[k])
17+
res = 0;
18+
}
19+
return res;
20+
});
21+
assert(ok == 1);
22+
}
23+
24+
int main() {
25+
26+
EM_ASM(
27+
TEST_GLFW3_DEFAULTS_HINTS = {};
28+
TEST_GLFW3_DEFAULTS_HINTS[0x00020001] = 0;
29+
TEST_GLFW3_DEFAULTS_HINTS[0x00020002] = 0;
30+
TEST_GLFW3_DEFAULTS_HINTS[0x00020003] = 1;
31+
TEST_GLFW3_DEFAULTS_HINTS[0x00020004] = 1;
32+
TEST_GLFW3_DEFAULTS_HINTS[0x00020005] = 1;
33+
TEST_GLFW3_DEFAULTS_HINTS[0x0002000A] = 0;
34+
TEST_GLFW3_DEFAULTS_HINTS[0x0002200C] = 0;
35+
36+
TEST_GLFW3_DEFAULTS_HINTS[0x00021001] = 8;
37+
TEST_GLFW3_DEFAULTS_HINTS[0x00021002] = 8;
38+
TEST_GLFW3_DEFAULTS_HINTS[0x00021003] = 8;
39+
TEST_GLFW3_DEFAULTS_HINTS[0x00021004] = 8;
40+
TEST_GLFW3_DEFAULTS_HINTS[0x00021005] = 24;
41+
TEST_GLFW3_DEFAULTS_HINTS[0x00021006] = 8;
42+
TEST_GLFW3_DEFAULTS_HINTS[0x00021007] = 0;
43+
TEST_GLFW3_DEFAULTS_HINTS[0x00021008] = 0;
44+
TEST_GLFW3_DEFAULTS_HINTS[0x00021009] = 0;
45+
TEST_GLFW3_DEFAULTS_HINTS[0x0002100A] = 0;
46+
TEST_GLFW3_DEFAULTS_HINTS[0x0002100B] = 0;
47+
TEST_GLFW3_DEFAULTS_HINTS[0x0002100C] = 0;
48+
TEST_GLFW3_DEFAULTS_HINTS[0x0002100D] = 0;
49+
TEST_GLFW3_DEFAULTS_HINTS[0x0002100E] = 0;
50+
TEST_GLFW3_DEFAULTS_HINTS[0x0002100F] = 0;
51+
52+
TEST_GLFW3_DEFAULTS_HINTS[0x00022001] = 0x00030001;
53+
TEST_GLFW3_DEFAULTS_HINTS[0x00022002] = 1;
54+
TEST_GLFW3_DEFAULTS_HINTS[0x00022003] = 0;
55+
TEST_GLFW3_DEFAULTS_HINTS[0x00022004] = 0;
56+
TEST_GLFW3_DEFAULTS_HINTS[0x00022005] = 0;
57+
TEST_GLFW3_DEFAULTS_HINTS[0x00022006] = 0;
58+
TEST_GLFW3_DEFAULTS_HINTS[0x00022007] = 0;
59+
TEST_GLFW3_DEFAULTS_HINTS[0x00022008] = 0;
60+
);
61+
62+
assert(glfwInit() == GL_TRUE);
63+
64+
// Use case: after glfwInit, default window hints are correct
65+
{
66+
checkDefaultWindowHints();
67+
}
68+
69+
// Use case: updates a window hint
70+
// Expected results: window hint is properly updated
71+
// default window hints are not affected
72+
{
73+
// GLFW_DEPTH_BITS
74+
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 24);
75+
glfwWindowHint(GLFW_DEPTH_BITS, 16);
76+
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 16);
77+
checkDefaultWindowHints();
78+
}
79+
80+
// Use case: resets window hints to default
81+
// Expected results: previously changed window hint is back to its default value
82+
// default window hints are not affected
83+
{
84+
glfwDefaultWindowHints();
85+
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 24);
86+
checkDefaultWindowHints();
87+
}
88+
89+
// Use case: change window hint, create window, then change window hint
90+
// Expected results: the window hint set at creation time (which is now a
91+
// window attribute that can be read with glfwGetWindowAttrib)
92+
// does not change
93+
{
94+
glfwDefaultWindowHints();
95+
glfwWindowHint(GLFW_DEPTH_BITS, 16);
96+
GLFWwindow* window = glfwCreateWindow(640, 480, "test_glfw3_default_hints.c", NULL, NULL);
97+
assert(glfwGetWindowAttrib(window, GLFW_DEPTH_BITS) == 16);
98+
glfwWindowHint(GLFW_DEPTH_BITS, 24);
99+
assert(glfwGetWindowAttrib(window, GLFW_DEPTH_BITS) == 16);
100+
}
101+
102+
glfwTerminate();
103+
104+
return 0;
105+
}

test/test_browser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,10 @@ def in_html(expected):
29992999

30003000
in_html('200')
30013001

3002+
@requires_graphics_hardware
3003+
def test_glfw3_default_hints(self):
3004+
self.btest_exit('test_glfw3_default_hints.c', args=['-sUSE_GLFW=3', '-lglfw', '-lGL'])
3005+
30023006
@requires_graphics_hardware
30033007
@parameterized({
30043008
'no_gl': (['-DCLIENT_API=GLFW_NO_API'],),

0 commit comments

Comments
 (0)