Emscripten: Store canvas id in WindowData
authorCharlie Birks <charlie@daftgames.net>
Thu, 09 May 2019 12:09:34 +0100
changeset 1272419c373658809
parent 12723 24883e864f7e
child 12725 5ef84902d1a0
Emscripten: Store canvas id in WindowData
Also replace all hardcoded uses of "#canvas" or NULL
src/video/emscripten/SDL_emscriptenevents.c
src/video/emscripten/SDL_emscriptenvideo.c
src/video/emscripten/SDL_emscriptenvideo.h
     1.1 --- a/src/video/emscripten/SDL_emscriptenevents.c	Thu Apr 25 14:17:07 2019 -0700
     1.2 +++ b/src/video/emscripten/SDL_emscriptenevents.c	Thu May 09 12:09:34 2019 +0100
     1.3 @@ -317,7 +317,7 @@
     1.4  
     1.5      /* rescale (in case canvas is being scaled)*/
     1.6      double client_w, client_h, xscale, yscale;
     1.7 -    emscripten_get_element_css_size(NULL, &client_w, &client_h);
     1.8 +    emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
     1.9      xscale = window_data->window->w / client_w;
    1.10      yscale = window_data->window->h / client_h;
    1.11  
    1.12 @@ -374,7 +374,7 @@
    1.13      SDL_SendMouseButton(window_data->window, 0, sdl_button_state, sdl_button);
    1.14  
    1.15      /* Do not consume the event if the mouse is outside of the canvas. */
    1.16 -    emscripten_get_element_css_size(NULL, &css_w, &css_h);
    1.17 +    emscripten_get_element_css_size(window_data->canvas_id, &css_w, &css_h);
    1.18      if (mouseEvent->canvasX < 0 || mouseEvent->canvasX >= css_w ||
    1.19          mouseEvent->canvasY < 0 || mouseEvent->canvasY >= css_h) {
    1.20          return 0;
    1.21 @@ -394,7 +394,7 @@
    1.22      if (!isPointerLocked) {
    1.23          /* rescale (in case canvas is being scaled)*/
    1.24          double client_w, client_h;
    1.25 -        emscripten_get_element_css_size(NULL, &client_w, &client_h);
    1.26 +        emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
    1.27  
    1.28          mx = mx * (window_data->window->w / client_w);
    1.29          my = my * (window_data->window->h / client_h);
    1.30 @@ -440,7 +440,7 @@
    1.31           return 0;
    1.32      }
    1.33  
    1.34 -    emscripten_get_element_css_size(NULL, &client_w, &client_h);
    1.35 +    emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
    1.36  
    1.37      for (i = 0; i < touchEvent->numTouches; i++) {
    1.38          SDL_FingerID id;
    1.39 @@ -587,14 +587,14 @@
    1.40              double h = window_data->window->h;
    1.41  
    1.42              if(window_data->external_size) {
    1.43 -                emscripten_get_element_css_size(NULL, &w, &h);
    1.44 +                emscripten_get_element_css_size(window_data->canvas_id, &w, &h);
    1.45              }
    1.46  
    1.47 -            emscripten_set_canvas_element_size(NULL, w * window_data->pixel_ratio, h * window_data->pixel_ratio);
    1.48 +            emscripten_set_canvas_element_size(window_data->canvas_id, w * window_data->pixel_ratio, h * window_data->pixel_ratio);
    1.49  
    1.50              /* set_canvas_size unsets this */
    1.51              if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
    1.52 -                emscripten_set_element_css_size(NULL, w, h);
    1.53 +                emscripten_set_element_css_size(window_data->canvas_id, w, h);
    1.54              }
    1.55  
    1.56              if (force) {
    1.57 @@ -619,7 +619,7 @@
    1.58      if(window_data->fullscreen_resize)
    1.59      {
    1.60          double css_w, css_h;
    1.61 -        emscripten_get_element_css_size(NULL, &css_w, &css_h);
    1.62 +        emscripten_get_element_css_size(window_data->canvas_id, &css_w, &css_h);
    1.63          SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
    1.64      }
    1.65  
    1.66 @@ -640,23 +640,23 @@
    1.67      const char *keyElement;
    1.68  
    1.69      /* There is only one window and that window is the canvas */
    1.70 -    emscripten_set_mousemove_callback("#canvas", data, 0, Emscripten_HandleMouseMove);
    1.71 +    emscripten_set_mousemove_callback(data->canvas_id, data, 0, Emscripten_HandleMouseMove);
    1.72  
    1.73 -    emscripten_set_mousedown_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
    1.74 +    emscripten_set_mousedown_callback(data->canvas_id, data, 0, Emscripten_HandleMouseButton);
    1.75      emscripten_set_mouseup_callback("#document", data, 0, Emscripten_HandleMouseButton);
    1.76  
    1.77 -    emscripten_set_mouseenter_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
    1.78 -    emscripten_set_mouseleave_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
    1.79 +    emscripten_set_mouseenter_callback(data->canvas_id, data, 0, Emscripten_HandleMouseFocus);
    1.80 +    emscripten_set_mouseleave_callback(data->canvas_id, data, 0, Emscripten_HandleMouseFocus);
    1.81  
    1.82 -    emscripten_set_wheel_callback("#canvas", data, 0, Emscripten_HandleWheel);
    1.83 +    emscripten_set_wheel_callback(data->canvas_id, data, 0, Emscripten_HandleWheel);
    1.84  
    1.85      emscripten_set_focus_callback("#window", data, 0, Emscripten_HandleFocus);
    1.86      emscripten_set_blur_callback("#window", data, 0, Emscripten_HandleFocus);
    1.87  
    1.88 -    emscripten_set_touchstart_callback("#canvas", data, 0, Emscripten_HandleTouch);
    1.89 -    emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
    1.90 -    emscripten_set_touchmove_callback("#canvas", data, 0, Emscripten_HandleTouch);
    1.91 -    emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
    1.92 +    emscripten_set_touchstart_callback(data->canvas_id, data, 0, Emscripten_HandleTouch);
    1.93 +    emscripten_set_touchend_callback(data->canvas_id, data, 0, Emscripten_HandleTouch);
    1.94 +    emscripten_set_touchmove_callback(data->canvas_id, data, 0, Emscripten_HandleTouch);
    1.95 +    emscripten_set_touchcancel_callback(data->canvas_id, data, 0, Emscripten_HandleTouch);
    1.96  
    1.97      emscripten_set_pointerlockchange_callback("#document", data, 0, Emscripten_HandlePointerLockChange);
    1.98  
    1.99 @@ -681,23 +681,23 @@
   1.100      const char *target;
   1.101  
   1.102      /* only works due to having one window */
   1.103 -    emscripten_set_mousemove_callback("#canvas", NULL, 0, NULL);
   1.104 +    emscripten_set_mousemove_callback(data->canvas_id, NULL, 0, NULL);
   1.105  
   1.106 -    emscripten_set_mousedown_callback("#canvas", NULL, 0, NULL);
   1.107 +    emscripten_set_mousedown_callback(data->canvas_id, NULL, 0, NULL);
   1.108      emscripten_set_mouseup_callback("#document", NULL, 0, NULL);
   1.109  
   1.110 -    emscripten_set_mouseenter_callback("#canvas", NULL, 0, NULL);
   1.111 -    emscripten_set_mouseleave_callback("#canvas", NULL, 0, NULL);
   1.112 +    emscripten_set_mouseenter_callback(data->canvas_id, NULL, 0, NULL);
   1.113 +    emscripten_set_mouseleave_callback(data->canvas_id, NULL, 0, NULL);
   1.114  
   1.115 -    emscripten_set_wheel_callback("#canvas", NULL, 0, NULL);
   1.116 +    emscripten_set_wheel_callback(data->canvas_id, NULL, 0, NULL);
   1.117  
   1.118      emscripten_set_focus_callback("#window", NULL, 0, NULL);
   1.119      emscripten_set_blur_callback("#window", NULL, 0, NULL);
   1.120  
   1.121 -    emscripten_set_touchstart_callback("#canvas", NULL, 0, NULL);
   1.122 -    emscripten_set_touchend_callback("#canvas", NULL, 0, NULL);
   1.123 -    emscripten_set_touchmove_callback("#canvas", NULL, 0, NULL);
   1.124 -    emscripten_set_touchcancel_callback("#canvas", NULL, 0, NULL);
   1.125 +    emscripten_set_touchstart_callback(data->canvas_id, NULL, 0, NULL);
   1.126 +    emscripten_set_touchend_callback(data->canvas_id, NULL, 0, NULL);
   1.127 +    emscripten_set_touchmove_callback(data->canvas_id, NULL, 0, NULL);
   1.128 +    emscripten_set_touchcancel_callback(data->canvas_id, NULL, 0, NULL);
   1.129  
   1.130      emscripten_set_pointerlockchange_callback("#document", NULL, 0, NULL);
   1.131  
     2.1 --- a/src/video/emscripten/SDL_emscriptenvideo.c	Thu Apr 25 14:17:07 2019 -0700
     2.2 +++ b/src/video/emscripten/SDL_emscriptenvideo.c	Thu May 09 12:09:34 2019 +0100
     2.3 @@ -196,6 +196,8 @@
     2.4          return SDL_OutOfMemory();
     2.5      }
     2.6  
     2.7 +    wdata->canvas_id = SDL_strdup("#canvas");
     2.8 +
     2.9      if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
    2.10          wdata->pixel_ratio = emscripten_get_device_pixel_ratio();
    2.11      } else {
    2.12 @@ -206,8 +208,8 @@
    2.13      scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
    2.14  
    2.15      /* set a fake size to check if there is any CSS sizing the canvas */
    2.16 -    emscripten_set_canvas_element_size(NULL, 1, 1);
    2.17 -    emscripten_get_element_css_size(NULL, &css_w, &css_h);
    2.18 +    emscripten_set_canvas_element_size(wdata->canvas_id, 1, 1);
    2.19 +    emscripten_get_element_css_size(wdata->canvas_id, &css_w, &css_h);
    2.20  
    2.21      wdata->external_size = SDL_floor(css_w) != 1 || SDL_floor(css_h) != 1;
    2.22  
    2.23 @@ -218,14 +220,13 @@
    2.24  
    2.25          SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
    2.26      }
    2.27 -
    2.28 -    emscripten_set_canvas_element_size(NULL, scaled_w, scaled_h);
    2.29 +    emscripten_set_canvas_element_size(wdata->canvas_id, scaled_w, scaled_h);
    2.30  
    2.31      /* if the size is not being controlled by css, we need to scale down for hidpi */
    2.32      if (!wdata->external_size) {
    2.33          if (wdata->pixel_ratio != 1.0f) {
    2.34              /*scale canvas down*/
    2.35 -            emscripten_set_element_css_size(NULL, window->w, window->h);
    2.36 +            emscripten_set_element_css_size(wdata->canvas_id, window->w, window->h);
    2.37          }
    2.38      }
    2.39  
    2.40 @@ -269,11 +270,11 @@
    2.41          if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
    2.42              data->pixel_ratio = emscripten_get_device_pixel_ratio();
    2.43          }
    2.44 -        emscripten_set_canvas_element_size(NULL, window->w * data->pixel_ratio, window->h * data->pixel_ratio);
    2.45 +        emscripten_set_canvas_element_size(data->canvas_id, window->w * data->pixel_ratio, window->h * data->pixel_ratio);
    2.46  
    2.47          /*scale canvas down*/
    2.48          if (!data->external_size && data->pixel_ratio != 1.0f) {
    2.49 -            emscripten_set_element_css_size(NULL, window->w, window->h);
    2.50 +            emscripten_set_element_css_size(data->canvas_id, window->w, window->h);
    2.51          }
    2.52      }
    2.53  }
    2.54 @@ -295,7 +296,8 @@
    2.55  #endif
    2.56  
    2.57          /* We can't destroy the canvas, so resize it to zero instead */
    2.58 -        emscripten_set_canvas_element_size(NULL, 0, 0);
    2.59 +        emscripten_set_canvas_element_size(data->canvas_id, 0, 0);
    2.60 +        SDL_free(data->canvas_id);
    2.61  
    2.62          SDL_free(window->driverdata);
    2.63          window->driverdata = NULL;
    2.64 @@ -332,7 +334,7 @@
    2.65              data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
    2.66              data->fullscreen_resize = is_desktop_fullscreen;
    2.67  
    2.68 -            res = emscripten_request_fullscreen_strategy(NULL, 1, &strategy);
    2.69 +            res = emscripten_request_fullscreen_strategy(data->canvas_id, 1, &strategy);
    2.70              if(res != EMSCRIPTEN_RESULT_SUCCESS && res != EMSCRIPTEN_RESULT_DEFERRED) {
    2.71                  /* unset flags, fullscreen failed */
    2.72                  window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
     3.1 --- a/src/video/emscripten/SDL_emscriptenvideo.h	Thu Apr 25 14:17:07 2019 -0700
     3.2 +++ b/src/video/emscripten/SDL_emscriptenvideo.h	Thu May 09 12:09:34 2019 +0100
     3.3 @@ -40,6 +40,8 @@
     3.4      SDL_Window *window;
     3.5      SDL_Surface *surface;
     3.6  
     3.7 +    char *canvas_id;
     3.8 +
     3.9      float pixel_ratio;
    3.10  
    3.11      SDL_bool external_size;