@@ -51,6 +51,20 @@ static void checkWindowSize(GLFWwindow *window, int expectedWidth, int expectedH
51
51
assert (fbw == (int ) (expectedWidth * ratio ) && fbh == (int ) (expectedHeight * ratio ));
52
52
}
53
53
54
+ static void checkCanvasSize (int expectedWidth , int expectedHeight ) {
55
+ int w , h ;
56
+ emscripten_get_canvas_element_size ("#canvas" , & w , & h );
57
+ printf ("canvas size => %d == %d && %d == %d\n" , w , expectedWidth , h , expectedHeight );
58
+ assert (w == expectedWidth && h == expectedHeight );
59
+ }
60
+
61
+ static void checkCanvasFramebufferSize (int expectedWidth , int expectedHeight ) {
62
+ double fbw , fbh ;
63
+ emscripten_get_element_css_size ("#canvas" , & fbw , & fbh );
64
+ printf ("canvas framebufferSize => %d == %d && %d == %d\n" , (int ) fbw , (int ) expectedWidth , (int ) fbh , expectedHeight );
65
+ assert ((int ) fbw == expectedWidth && (int ) fbh == expectedHeight );
66
+ }
67
+
54
68
static bool getGLFWIsHiDPIAware () {
55
69
return EM_ASM_INT (return GLFW .isHiDPIAware () ? 1 : 0 ) != 0 ;
56
70
}
@@ -75,13 +89,17 @@ int main() {
75
89
// Expected outcome is window size and frame buffer size are the same
76
90
{
77
91
printf ("Use case #1\n" );
92
+ checkCanvasSize (300 , 150 ); // 300x150 is the default canvas size
93
+ checkCanvasFramebufferSize (300 , 150 );
78
94
window = glfwCreateWindow (640 , 480 , "test_glfw3_hi_dpi_aware.c | #1" , NULL , NULL );
79
95
assert (window != NULL );
80
96
checkHiDPIAware (window , false);
81
97
checkWindowSize (window , 640 , 480 , 1.0 );
82
98
glfwSetWindowSize (window , 600 , 400 );
83
99
checkWindowSize (window , 600 , 400 , 1.0 );
84
100
glfwDestroyWindow (window );
101
+ checkCanvasSize (300 , 150 ); // we make sure that the glfw code resets the canvas how it was
102
+ checkCanvasFramebufferSize (300 , 150 );
85
103
}
86
104
87
105
// Use case 2: GLFW is NOT Hi DPI Aware | devicePixelRatio is 2.0
@@ -174,6 +192,24 @@ int main() {
174
192
glfwDestroyWindow (window );
175
193
}
176
194
195
+ // Use case 8: GLFW is Hi DPI Aware | devicePixelRatio is 2.0 | canvas has css override
196
+ // Expected outcome is that the framebuffer size is adjusted according to the canvas size
197
+ {
198
+ printf ("Use case #8\n" );
199
+ setDevicePixelRatio (2.0 );
200
+ glfwWindowHint (GLFW_SCALE_TO_MONITOR , GLFW_TRUE );
201
+ emscripten_set_element_css_size ("#canvas" , 700 , 525 );
202
+ checkCanvasSize (300 , 150 ); // default canvas size
203
+ checkCanvasFramebufferSize (700 , 525 ); // css override
204
+ window = glfwCreateWindow (640 , 480 , "test_glfw3_hi_dpi_aware.c | #8" , NULL , NULL );
205
+ assert (window != NULL );
206
+ checkHiDPIAware (window , true);
207
+ checkWindowSize (window , 700 , 525 , 2.0 ); // canvas size overrides window size
208
+ glfwDestroyWindow (window );
209
+ checkCanvasSize (300 , 150 );
210
+ checkCanvasFramebufferSize (700 , 525 );
211
+ }
212
+
177
213
glfwTerminate ();
178
214
179
215
return 0 ;
0 commit comments