Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL2 not receiving window events

I've been using SDL2 on my Macbook with Xcode for a little bit, and I'm very pleased. Lately I tried to receive a focus lost event, but it wasn't working. After some tinkering, I found out that no window events were being received, except a window event that had a type of 512 that showed up 2-5 times a second at random intervals. The WindowEventIDs are in an enum, not in hex format, so it shouldn't be some hex number. I ran a search through the SDL2 framework for 512 and found nothing. Other events, like SDL_QUIT and SDL_KEYDOWN work just fine. Anybody know what's going on?

Here is my event loop:

SDL_Event event;

bool running = false;
while(running) {
    while(SDL_PollEvent(&event)) {
        if(event.type == SDL_QUIT)
            running = false;

        else if(event.type == SDL_KEYDOWN) {
            cout << event.key.type << endl;
        }
        else if(event.type == SDL_WINDOWEVENT) {
            cout << event.window.type << endl;
        }
    }

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
}
like image 714
Biostreem Avatar asked Aug 26 '13 23:08

Biostreem


1 Answers

You need to check event.window.event rather than event.window.type, since type is more general and has some other usage (code 512 is related to SDL_WINDOW_INPUT_FOCUS and it triggered whenever you moved cursor to or from window). So your code could be like this:

while(SDL_PollEvent(&event)) {
    switch(event.type)
    {
    case SDL_WINDOWEVENT:
        switch(event.window.event)
        {
        case SDL_WINDOWEVENT_ENTER:
            cout << "entered" << endl;
            break;

        case SDL_WINDOWEVENT_LEAVE:
            cout << "left" << endl;
            break;
        }
        break;

    case SDL_KEYDOWN:
        cout << "key pressed: " << event.key.keysym.sym << endl;
        break;
    }
}
like image 120
Mars Avatar answered Nov 16 '22 17:11

Mars