New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canvas resize event is sent only once #66

Open
Beuc opened this Issue Nov 30, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@Beuc
Contributor

Beuc commented Nov 30, 2018

Hi!

This port detects if the canvas is CSS-resized by assigning it dimensions and checking right after if the effective dimensions are the same. If not, this sets window->external_size forever and resize events are emitted.

This fails:

  • when the window didn't need to be resized on start-up (e.g. due to reaching a maximum CSS size already)

  • more importantly, when calling SDL_CreateRenderer, which triggers SDL_RecreateWindow:

_SDL_CreateRenderer
_GLES2_CreateRenderer
_SDL_RecreateWindow
_Emscripten_CreateWindow

When the test is run for the second time, the canvas was already resized once and the initial and CSS-resized dimensions match. window->external_size is then reset to 0 and resize events are not emitted anymore.

We could either try to find a better detection algorithm (fixing both cases), or avoid zero-ing window->external_size if it was already to 1 (fixing the second case).

What do you think?

@Beuc

This comment has been minimized.

Contributor

Beuc commented Nov 30, 2018

window->external_size seems reset to 0 when calling the function anyway.
What is preventing us from making window->external_size always set to 1 by the way?
AFAICS SDL2 cancels resize events when the dimensions didn't change already.

@Daft-Freak

This comment has been minimized.

Member

Daft-Freak commented Dec 1, 2018

window->external_size seems reset to 0 when calling the function anyway.

Yeah, the window gets destroyed.

What is preventing us from making window->external_size always set to 1 by the way?
AFAICS SDL2 cancels resize events when the dimensions didn't change already.

It's for HiDPI where we need to set the CSS size of the canvas to the window size and the canvas size to the window size * pixel ratio, but not if the user has already set a size through CSS. I guess we could always set it to 1 if the SDL_WINDOW_ALLOW_HIGHDPI isn't specified. Setting the canvas size to something like 1x1 before doing the check may also work for most cases...

@Beuc

This comment has been minimized.

Contributor

Beuc commented Dec 1, 2018

Your idea appears to work without graphical artifact :)

diff --git a/src/video/emscripten/SDL_emscriptenvideo.c b/src/video/emscripten/SDL_emscriptenvideo.c
index 897d47096..d1acf7c19 100644
--- a/src/video/emscripten/SDL_emscriptenvideo.c
+++ b/src/video/emscripten/SDL_emscriptenvideo.c
@@ -206,11 +206,13 @@ Emscripten_CreateWindow(_THIS, SDL_Window * window)
     scaled_w = SDL_floor(window->w * wdata->pixel_ratio);
     scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
 
-    emscripten_set_canvas_size(scaled_w, scaled_h);
-
+    emscripten_set_canvas_size(1, 1);
     emscripten_get_element_css_size(NULL, &css_w, &css_h);
 
-    wdata->external_size = SDL_floor(css_w) != scaled_w || SDL_floor(css_h) != scaled_h;
+    wdata->external_size = SDL_floor(css_w) != 1 || SDL_floor(css_h) != 1;
+    printf("external_size: %d\n", wdata->external_size);
+
+    emscripten_set_canvas_size(scaled_w, scaled_h);
 
     if ((window->flags & SDL_WINDOW_RESIZABLE) && wdata->external_size) {
         /* external css has resized us */

Incidentally SDL_RecreateWindow is also called when using SDL_CreateWindowAndRenderer combo function.

Daft-Freak added a commit that referenced this issue Dec 8, 2018

Emscripten: use a fake size for external sizing check
The check would fail if the canvas happened to be the correct size
already. (#66, mentioned in #58)

Daft-Freak added a commit that referenced this issue Dec 8, 2018

Emscripten: use a fake size for external sizing check
The check would fail if the canvas happened to be the correct size
already. (#66, mentioned in #58)
@Daft-Freak

This comment has been minimized.

Member

Daft-Freak commented Dec 8, 2018

I think you can avoid the recreating by creating the window with SDL_WINDOW_OPENGL, but then you're assuming that a GL based renderer will be used... Anyway, I pushed that change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment