0

I've followed this tutorial to create a widget in which i can render using SDL, at the end of the tutorial the author mentions that it should be possible to use 'SDL_PollEvent' in the update function so I've created this update function with following content:

    SDL_Event sdl_event;
    while(SDL_PollEvent(&sdl_event))
    {
        switch(sdl_event.type){
            case SDL_KEYDOWN:
                LOG_CRITICAL("KeyDown");
                break;

            case SDL_KEYUP:
                LOG_CRITICAL("KeyUp");
                break;

            default:
                LOG_CRITICAL("Other Event");
                break;
        }
    }

And even when i click or hold down a button LOG_CRITICAL does not run. Is there something I'm doing wrong or is there another way to handle input?

I've tried running another thread to handle the input however apparently you can only handle events in the main thread.

Edit: It started to work when i changed SDL_PollEvent to SDL_WaitEvent. My rendering is done in a different thread so rendering was still hapenning but it was impossible to close or resize the window (i assume qt's thread is frozen because of SDL_WaitEvent).

I could also change while (SDL_WaitEvent) to if (SDL_WaitEvent) and the window would only freeze when i was clicking a button but there has to be a "better" way to do it?

Daniel
  • 85
  • 5
  • I don't think author is correct in that aspect (and i wouldn't trust author who advertises obvious memory leak in their article, sorry). You'll have to get events from Qt and pass them down to your context (or insert them to SDL queue, and make sure `PumpEvents` never gets called, e.g. relying on `SDL_PeepEvents`). – keltar Apr 16 '23 at 05:22
  • @keltar I gave up on using SDL for inputs and used 'keyPressEvent' from Qt instead. Is the obvious memory leak you're talking about the fact that he didn't use SDL_Quit? – Daniel Apr 16 '23 at 14:01
  • No, `new SDL_Rect` in update function, leaking 16 bytes per frame and serving no purpose as temporary on-stack rect would be more than enough. – keltar Apr 16 '23 at 16:03

0 Answers0