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

Creating, Deleting and Recreating a SDL GL Context stops Mouse events #92

Open
Evengard opened this issue Sep 4, 2019 · 5 comments
Open

Comments

@Evengard
Copy link

Evengard commented Sep 4, 2019

This seems to be a regression since v17, as the bug didn't existed in v17.
In v18, when doing

ctx = SDL_GL_CreateContext(wnd);
SDL_GL_DeleteContext(ctx);
ctx = SDL_GL_CreateContext(wnd);

this stops mouse events from being dispatched via SDL_PollEvent. They just doesn't appear no matter what do I do.
The bug is nonexistant in non-EMSCRIPTEN environment (i.e. when built with SDL on Windows)

@Daft-Freak
Copy link
Member

Hmm, I couldn't reproduce with this:

// emcc -s USE_SDL=2
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include "SDL.h"

#include <stdio.h>

static int run = 1;

SDL_Window *window;

void loop() {
  SDL_Event event;
  while (SDL_PollEvent(&event)) {
    switch(event.type) {

    case SDL_MOUSEMOTION:
      printf("motion %i %i\n", event.motion.x, event.motion.y);
      break;

    case SDL_QUIT:
      run = 0;
      break;
    }
  }
}

int main(int argc, char *argv[]) {
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    return (1);
  }

  window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);

  SDL_GLContext ctx = SDL_GL_CreateContext(window);
  SDL_GL_DeleteContext(ctx);
  ctx = SDL_GL_CreateContext(window);

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

  return (0);
}

Also, if it helps narrow this down, these are the non-upstream changes between 17 and 18: version_17...3a222fc (the rest is 2.0.7 -> 2.0.9)

@Evengard
Copy link
Author

Evengard commented Sep 5, 2019

I'll try to test with only theese changes later. My app is a lot bigger and is kinda legacy stuff, hard to pinpoint the exact cause, but removing the SDL_GLContext recreation fixed it. So I guess this is somewhere around there.

Also, the actual delete and recreate happens in the main loop. And also, the mouse is set as relative.

@six809
Copy link

six809 commented Feb 22, 2020

I'm also having this problem, though I don't set up contexts directly, I use renderers. If you destroy one and recreate another one for the same window, mouse events vanish. Modifying your example:

// emcc -s USE_SDL=2
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include "SDL.h"

#include <stdio.h>

static int run = 1;

SDL_Window *window;
SDL_Renderer *renderer;

void loop() {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
                switch(event.type) {

                case SDL_MOUSEMOTION:
                        printf("motion %i %i\n", event.motion.x, event.motion.y);
                        break;

                case SDL_QUIT:
                        run = 0;
                        break;
                }
        }
}

int main(int argc, char *argv[]) {
        if (SDL_Init(SDL_INIT_VIDEO) < 0) {
                return (1);
        }

        window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);

        renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);

        // Comment these out and it works...
        SDL_DestroyRenderer(renderer);
        renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);

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

        return (0);
}

@Daft-Freak
Copy link
Member

Hmm, just destroying the renderer is enough... Digging down a bit, it looks like Emscripten removes ALL event handlers from a canvas when you destroy a GL context: https://github.com/emscripten-core/emscripten/blob/master/src/library_webgl.js#L999.

@six809
Copy link

six809 commented Feb 22, 2020

Ah, I guess that's this, then: emscripten-core/emscripten#9803

Seems I can work around it myself by just not recreating the renderer. I thought I was doing that because I had to on window resize, but it seems to work fine in Linux, emscripten, Windows (well, Wine). Not tested macosx yet.

Edit: I was wrong. It is needed when resizing, or you get some strange scaling effects.

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

3 participants