1

I tried code that opens a simple window in C:

main.c:

#include <SDL2/SDL.h>
#undef main
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>

int main(int argc, char* argv[])
{

    // returns zero on success else non-zero
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        printf("error initializing SDL: %s\n", SDL_GetError());
    }
    SDL_Window* win = SDL_CreateWindow("GAME",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        1280, 720, 0);
    while (1)
        ;
    return 0;
}

But as the title suggests, the window goes unresponsive when trying to close the window. Unlike a lot of the other posts, this doesn't involve SDL_Delay(), but still has the same effect.

genpfault
  • 51,148
  • 11
  • 85
  • 139
justtab
  • 23
  • 4
  • 5
    `while (1) ;` is an infinite loop that does nothing. When you don't process messages your window is marked unresponsive. – Retired Ninja May 14 '23 at 22:56
  • 3
    You need to be calling something like `SDL_PollEvent()` in your infinite loop. And responding to those events. – pmacfarlane May 14 '23 at 23:02
  • You will only see `while (1) ;` used in microcontrollers as either the end of processing the program or where you have configured interrupts to drive the program operations. On a desktop system, you need to process inputs (events) within your `while (1) ;` loop (and many API, like SDL have an event loop you can use). See [Lazy Foo' Production SDL2 Tutorial](https://lazyfoo.net/SDL_tutorials/) – David C. Rankin May 15 '23 at 05:31
  • I included stdbool.h, replaced while (1) with "while ( SDL_PollEvent(SDLK_0) == false )" so whenever I press zero, it should close the window, but instead it closes the window immediately. – justtab May 16 '23 at 00:59
  • Side note: `#undef main` does not look like the right thing to do. Does renaming `main` to `SDL_main` fix the need to use `#undef main` with you? – Andreas Wenzel May 16 '23 at 16:42
  • I suggest that you study the documentation for [`SDL_PollEvent`](https://wiki.libsdl.org/SDL2/SDL_PollEvent). It takes a pointer to an object of type [`SDL_Event`](https://wiki.libsdl.org/SDL2/SDL_Event) as a function argument. However, you seem to be passing something completely different. – Andreas Wenzel May 16 '23 at 19:06
  • If you want to determine whether `SDL_PollEvent` found an event with the `SDLK_0` keystroke, then you should pass `SDL_PollEvent` a pointer to an `SDL_Event` object and then inspect that object's `type` member. If that member is equal to `SDL_KEYDOWN`, then you can inspect the `key` member, which is of type [`SDL_KeyboardEvent`](https://wiki.libsdl.org/SDL2/SDL_KeyboardEvent). If the sub-member `keysym` of the `key` member has the value `SDLK_0`, then you know that the event is the user pressing the `0` key. – Andreas Wenzel May 16 '23 at 21:30

0 Answers0