[go: nahoru, domu]

Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Not getting any resize events #105

Open
musteresel opened this issue Mar 2, 2020 · 3 comments
Open

Not getting any resize events #105

musteresel opened this issue Mar 2, 2020 · 3 comments

Comments

@musteresel
Copy link

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
Copy link
Member

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
Copy link
Author

I've tried:

  • Resizing the browser window (e.g. Chrome Dev Tools -> Responsive Layout and changing size)
  • Changing the canvas size manually (Chrome Dev Tools)
  • Changing the canvas size via Javascript

All of this in

  • the "default" generated html output (-o test.html)
  • a simple minimal html with just the canvas (and js loading the code, of course)
  • a simple minimal html with the canvas and CSS stylesheets

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
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants