emscripten-ports / SDL2

Other
166 stars 64 forks source link

Not getting any resize events #105

Open musteresel opened 4 years ago

musteresel commented 4 years ago

I might be doing something wrong, but I'm not getting any resize events. Neither with the "default" html output nor with a minimal html template with an own canvas.

How can I get resize events?

//  em++ SDLresize.cpp -s USE_SDL=2 -o test.html
#if __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#else
bool quit = false;
#endif
#include <SDL2/SDL.h>

SDL_Window * window = nullptr;

bool Setup() {
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    SDL_Log("SDL_Init failed");
    return false;
  }
  window = SDL_CreateWindow("Window",
                            SDL_WINDOWPOS_UNDEFINED,
                            SDL_WINDOWPOS_UNDEFINED,
                            640, 480,
                            SDL_WINDOW_RESIZABLE);
  if (window == nullptr) {
    SDL_Log("SDL_CreateWindow failed");
    SDL_Quit();
    return false;
  }
  return true;
}

void Teardown() {
  if (window != nullptr) {
    SDL_DestroyWindow(window);
  }
  SDL_Quit();
#if __EMSCRIPTEN__
  emscripten_cancel_main_loop();
#else
  quit = true;
#endif
}

void Tick() {
  SDL_Event e;
  while (SDL_PollEvent(&e) != 0) {
    switch (e.type) {
      case SDL_QUIT: {
        Teardown();
        return;
      }
      case SDL_WINDOWEVENT: {
        switch (e.window.event) {
          case SDL_WINDOWEVENT_RESIZED:
          case SDL_WINDOWEVENT_SIZE_CHANGED: {
            SDL_Log("SIZE CHANGED!"); // Never called when using emscripten
            break;
          }
        }
      }
    }
  }
}

int main() {
  if (Setup()) {
#if __EMSCRIPTEN__
    emscripten_set_main_loop(Tick, -1, false);
#else
    while (! quit) {
      Tick();
      SDL_Delay(100);
    }
#endif
  }
}

Emscripten version: 1.38.28 SDL2 port version: 17

Daft-Freak commented 4 years ago

Are you doing anything in your html to change the size of the canvas? Resize events are fired when the browser window resizes, but if the canvas stays the same size SDL filters the events out.

musteresel commented 4 years ago

I've tried:

All of this in

When I changed the size of the canvas via Javascript, and then changed the size of the browser window, it seemed to resize the canvas back to its original size (before changing via js).

Never did I get any resize related event in the C code (everything else works, I can also set the size from the C side, draw on the canvas, react to mouse / key events and so on)

Do you have an example which should generate a resize event?

Daft-Freak commented 4 years ago

Here's some test code:

#include <SDL.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

static SDL_Window* sdlWindow = NULL;
static int run = 1;

void one_loop() {
  SDL_Event event;
  while (SDL_PollEvent(&event)) {
    if (event.type == SDL_WINDOWEVENT) {
      switch (event.window.event) {
        case SDL_WINDOWEVENT_RESIZED:
          SDL_Log("Window %d resized to %dx%d", event.window.windowID, event.window.data1, event.window.data2);
          break;
        }
      }
      if (event.type == SDL_QUIT)
        run = 0;
  }
}

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  sdlWindow = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_RESIZABLE);

#ifdef __EMSCRIPTEN__
  emscripten_set_main_loop(one_loop, 0, 1);
#else
  while (run)
    one_loop();
#endif

  return 0;
}

emcc ./testresize.c -s USE_SDL=2 -o testresize.html

And width: 100% on the canvas.

If I remember correctly we're checking at "window creation" if CSS is causing the canvas size to change and the resize code only works if it is.