Skip to content

Commit

Permalink
Fix glfw3 default hints being modified (#20770)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypujante authored Nov 30, 2023
1 parent 9de528b commit 7d639f7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/library_glfw.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var LibraryGLFW = {
this.title = title;
this.monitor = monitor;
this.share = share;
this.attributes = GLFW.hints;
this.attributes = Object.assign({}, GLFW.hints);
this.inputModes = {
0x00033001:0x00034001, // GLFW_CURSOR (GLFW_CURSOR_NORMAL)
0x00033002:0, // GLFW_STICKY_KEYS
Expand Down Expand Up @@ -1062,6 +1062,10 @@ var LibraryGLFW = {
}
},

defaultWindowHints: () => {
GLFW.hints = Object.assign({}, GLFW.defaultHints);
},

createWindow: (width, height, title, monitor, share) => {
var i, id;
for (i = 0; i < GLFW.windows.length && GLFW.windows[i] !== null; i++) {
Expand Down Expand Up @@ -1190,7 +1194,7 @@ var LibraryGLFW = {
if (GLFW.windows) return 1; // GL_TRUE

GLFW.initialTime = GLFW.getTime();
GLFW.hints = GLFW.defaultHints;
GLFW.defaultWindowHints();
GLFW.windows = new Array()
GLFW.active = null;
GLFW.scale = _emscripten_get_device_pixel_ratio();
Expand Down Expand Up @@ -1386,9 +1390,7 @@ var LibraryGLFW = {

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

glfwDefaultWindowHints: () => {
GLFW.hints = GLFW.defaultHints;
},
glfwDefaultWindowHints: () => GLFW.defaultWindowHints(),

glfwWindowHint: (target, hint) => {
GLFW.hints[target] = hint;
Expand Down
105 changes: 105 additions & 0 deletions test/browser/test_glfw3_default_hints.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2023 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <GLFW/glfw3.h>
#include <assert.h>
#include <emscripten/html5.h>

static void checkDefaultWindowHints() {
int ok = EM_ASM_INT({
var res = 1;
for (var k in TEST_GLFW3_DEFAULTS_HINTS) {
if (GLFW.defaultHints[k] !== TEST_GLFW3_DEFAULTS_HINTS[k])
res = 0;
}
return res;
});
assert(ok == 1);
}

int main() {

EM_ASM(
TEST_GLFW3_DEFAULTS_HINTS = {};
TEST_GLFW3_DEFAULTS_HINTS[0x00020001] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00020002] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00020003] = 1;
TEST_GLFW3_DEFAULTS_HINTS[0x00020004] = 1;
TEST_GLFW3_DEFAULTS_HINTS[0x00020005] = 1;
TEST_GLFW3_DEFAULTS_HINTS[0x0002000A] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002200C] = 0;

TEST_GLFW3_DEFAULTS_HINTS[0x00021001] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021002] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021003] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021004] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021005] = 24;
TEST_GLFW3_DEFAULTS_HINTS[0x00021006] = 8;
TEST_GLFW3_DEFAULTS_HINTS[0x00021007] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00021008] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00021009] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100A] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100B] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100C] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100D] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100E] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x0002100F] = 0;

TEST_GLFW3_DEFAULTS_HINTS[0x00022001] = 0x00030001;
TEST_GLFW3_DEFAULTS_HINTS[0x00022002] = 1;
TEST_GLFW3_DEFAULTS_HINTS[0x00022003] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022004] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022005] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022006] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022007] = 0;
TEST_GLFW3_DEFAULTS_HINTS[0x00022008] = 0;
);

assert(glfwInit() == GL_TRUE);

// Use case: after glfwInit, default window hints are correct
{
checkDefaultWindowHints();
}

// Use case: updates a window hint
// Expected results: window hint is properly updated
// default window hints are not affected
{
// GLFW_DEPTH_BITS
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 24);
glfwWindowHint(GLFW_DEPTH_BITS, 16);
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 16);
checkDefaultWindowHints();
}

// Use case: resets window hints to default
// Expected results: previously changed window hint is back to its default value
// default window hints are not affected
{
glfwDefaultWindowHints();
assert(EM_ASM_INT(return GLFW.hints[0x00021005];) == 24);
checkDefaultWindowHints();
}

// Use case: change window hint, create window, then change window hint
// Expected results: the window hint set at creation time (which is now a
// window attribute that can be read with glfwGetWindowAttrib)
// does not change
{
glfwDefaultWindowHints();
glfwWindowHint(GLFW_DEPTH_BITS, 16);
GLFWwindow* window = glfwCreateWindow(640, 480, "test_glfw3_default_hints.c", NULL, NULL);
assert(glfwGetWindowAttrib(window, GLFW_DEPTH_BITS) == 16);
glfwWindowHint(GLFW_DEPTH_BITS, 24);
assert(glfwGetWindowAttrib(window, GLFW_DEPTH_BITS) == 16);
}

glfwTerminate();

return 0;
}
4 changes: 4 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2999,6 +2999,10 @@ def in_html(expected):

in_html('200')

@requires_graphics_hardware
def test_glfw3_default_hints(self):
self.btest_exit('test_glfw3_default_hints.c', args=['-sUSE_GLFW=3', '-lglfw', '-lGL'])

@requires_graphics_hardware
@parameterized({
'no_gl': (['-DCLIENT_API=GLFW_NO_API'],),
Expand Down

0 comments on commit 7d639f7

Please sign in to comment.