'Why isn't sdl window responding to when I tried to click the minimize button or tried to drag the window?
I initialized and created an SDL window, with the code below. When I run it the window appears but, I can't drag the window and can't minimize it as the minimize button is also not responsive.
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindow("Game", 100, 100, 1020, 768, 0);
SDL_Event e;
bool canRun = true;
while(canRun){
}
return 0;
}
But if I add the code for events inside the while loop then the window becomes responsive to clicks and then I am able to click and drag the window.
SDL_Event e;
bool canRun = true;
while (canRun) {
while (SDL_PollEvent(&e)) {
}
}
How does this event code make the window responsive, even though I haven't added any event for closing, minimizing, or dragging the window?
Solution 1:[1]
The window behavior sounds familiar, so I'm assuming you are running on Windows; other platforms probably exhibit similar behavior.
Basically a window will be unresponsive unless it's message loop is run. It needs to process certain messages for e.g. the window sizing to work.
However, when using many UI libraries/frameworks, the application code does not need to handle all the events.
In the SDL case on Windows, SDL_PollEvent() ends up calling WIN_PumpEvents() in src/video/windows/SDL_windowsevents.c.
That has the 'magic' trio of Windows APIs calls to make a window work:
PeekMessage(), TranslateMessage(), and DispatchMEssage().
From DispatchMessage(), the default window procedure WIN_WindowProc() gets called. If you browse it's source code, you can see default handling for many windows messages, which enable the functionality for the window that you can witness without code supplied by yourself.
In cases where the application level needs to be involved, the window procedure function then passes the events to you:
case WM_SHOWWINDOW:
{
if (wParam) {
SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
} else {
SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
}
}
break;
I did not read the code of SDL_SendWindowEvent(), but I'd guess it will result in you receiving the event from SDL_PollEvent().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | genpfault |
